import { AgentPubKeyB64 , deserializeHash } from '@holochain-open-dev/core-types' ; export type HSL = [ number , number , number ] ; export function CSSifyHSL ( hslArray ) { const [ h , s , l ] = hslArray ; return `hsl(${ h } ${ s }% ${ l }%)` ; } export interface FolkColors { primary : HSL ; hexagon : HSL ; selection : HSL ; lookingSelection : HSL ; lookingCursor : HSL ; } function arrayBufferToHex ( buffer ) { let hexString = '' ; for ( const byte of buffer ) { hexString += byte . toString ( 16 ) ; } return hexString ; } function rgbToHsl ( r , g , b ) { ( r /= 255 ) , ( g /= 255 ) , ( b /= 255 ) ; let max = Math . max ( r , g , b ) , min = Math . min ( r , g , b ) ; let h , s , l = ( max + min ) / 2 ; if ( max == min ) { h = s = 0 ; } else { let d = max - min ; s = l > 0.5 ? d / ( 2 - max - min ) : d / ( max + min ) ; switch ( max ) { case r : h = ( g - b ) / d + ( g < b ? 6 : 0 ) ; break ; case g : h = ( b - r ) / d + 2 ; break ; case b : h = ( r - g ) / d + 4 ; break ; } h /= 6 ; } return [ h * 360 , s * 100 , l * 100 ] ; } function clamp ( value , min , max ) { return Math . min ( Math . max ( value , min ) , max ) ; } export function getFolkColors ( pubKey ) { const hexColor = '#' + arrayBufferToHex ( deserializeHash ( pubKey ) ) . slice ( - 6 ) ; const r = parseInt ( hexColor . substr ( 1 , 2 ) , 16 ) ; const g = parseInt ( hexColor . substr ( 3 , 2 ) , 16 ) ; const b = parseInt ( hexColor . substr ( 5 , 2 ) , 16 ) ; let hsl = rgbToHsl ( r , g , b ) ; hsl [ 1 ] = clamp ( hsl [ 1 ] , 10 , 90 ) ; const [ h , s , _ ] = hsl ; return { primary : [ h , s , 50 ] , hexagon : [ h , s , 25 ] , selection : [ h , s , 90 ] , lookingSelection : [ h , s , 80 ] , lookingCursor : [ h , s + 10 , 40 ] , } ; }