@@ -6,10 +6,10 @@ import { colorCalculateDistance } from "./colorCalculateDistance";
66import CssCustomProperties from "./CssCustomProperties" ;
77
88type ColorOrFalse = Color | false ;
9- type ColorWeight = 100 | 300 | 500 | 700 | 900 ;
10- type PaletteGroup = "identity" | "semantic" | "layout" | "extra" ;
9+ export type ColorWeight = 100 | 300 | 500 | 700 | 900 ;
10+ export type PaletteGroup = "identity" | "semantic" | "layout" | "extra" ;
1111
12- interface getEnabledColorsProps {
12+ export interface getEnabledColorsProps {
1313 /** Specify the palette groups used to define the set of colors. */
1414 includePaletteGroup ?: PaletteGroup [ ] ;
1515 /** Use only some weights of a color tint. */
@@ -21,20 +21,43 @@ interface getEnabledColorsProps {
2121}
2222
2323const getEnabledColorsFromPaletteCache = new Map < string , Color [ ] > ( ) ;
24+ const getEnabledColorPropertiesFromPaletteCache = new Map < string , [ string , string ] [ ] > ( ) ;
2425
25- export function getEnabledColorsFromPalette ( {
26+ export function getEnabledColorsFromPalette ( props : getEnabledColorsProps ) : Color [ ] {
27+ const configId = JSON . stringify ( {
28+ includePaletteGroup : props . includePaletteGroup ,
29+ includeColorWeight : props . includeColorWeight ,
30+ } ) ;
31+
32+ if ( getEnabledColorsFromPaletteCache . has ( configId ) ) {
33+ return getEnabledColorsFromPaletteCache . get ( configId ) ! ;
34+ }
35+
36+ const colorPropertiesFromPalette = Object . values ( getEnabledColorPropertiesFromPalette ( props ) ) ;
37+
38+ getEnabledColorsFromPaletteCache . set (
39+ configId ,
40+ colorPropertiesFromPalette . map ( ( color ) => {
41+ return Color ( color [ 1 ] ) ;
42+ } )
43+ ) ;
44+
45+ return getEnabledColorsFromPaletteCache . get ( configId ) ! ;
46+ }
47+
48+ export function getEnabledColorPropertiesFromPalette ( {
2649 includePaletteGroup = [ "layout" ] ,
2750 includeColorWeight = [ 100 , 300 , 500 , 700 , 900 ] ,
28- // TODO (planned for later): includeMixedColors = false,
51+ // (planned for later): includeMixedColors = false,
2952 minimalColorDistance = COLORMINDISTANCE ,
30- } : getEnabledColorsProps ) : Color [ ] {
53+ } : getEnabledColorsProps ) : [ string , string ] [ ] {
3154 const configId = JSON . stringify ( {
3255 includePaletteGroup,
3356 includeColorWeight,
3457 } ) ;
3558
36- if ( getEnabledColorsFromPaletteCache . has ( configId ) ) {
37- return getEnabledColorsFromPaletteCache . get ( configId ) ! ;
59+ if ( getEnabledColorPropertiesFromPaletteCache . has ( configId ) ) {
60+ return getEnabledColorPropertiesFromPaletteCache . get ( configId ) ! ;
3861 }
3962
4063 const colorsFromPalette = new CssCustomProperties ( {
@@ -50,18 +73,18 @@ export function getEnabledColorsFromPalette({
5073 const weight = parseInt ( tint [ 2 ] , 10 ) as ColorWeight ;
5174 return includePaletteGroup . includes ( group ) && includeColorWeight . includes ( weight ) ;
5275 } ,
53- removeDashPrefix : false ,
76+ removeDashPrefix : true ,
5477 returnObject : true ,
5578 } ) . customProperties ( ) ;
5679
57- const colorsFromPaletteValues = Object . values ( colorsFromPalette ) as string [ ] ;
80+ const colorsFromPaletteValues = Object . entries ( colorsFromPalette ) as [ string , string ] [ ] ;
5881
5982 const colorsFromPaletteWithEnoughDistance =
6083 minimalColorDistance > 0
61- ? colorsFromPaletteValues . reduce ( ( enoughDistance : string [ ] , color : string ) => {
84+ ? colorsFromPaletteValues . reduce ( ( enoughDistance : [ string , string ] [ ] , color : [ string , string ] ) => {
6285 if ( enoughDistance . includes ( color ) ) {
6386 return enoughDistance . filter ( ( checkColor ) => {
64- const distance = colorCalculateDistance ( { color1 : color , color2 : checkColor } ) ;
87+ const distance = colorCalculateDistance ( { color1 : color [ 1 ] , color2 : checkColor [ 1 ] } ) ;
6588 return checkColor === color || ( distance && minimalColorDistance <= distance ) ;
6689 } ) ;
6790 } else {
@@ -70,14 +93,9 @@ export function getEnabledColorsFromPalette({
7093 } , colorsFromPaletteValues )
7194 : colorsFromPaletteValues ;
7295
73- getEnabledColorsFromPaletteCache . set (
74- configId ,
75- colorsFromPaletteWithEnoughDistance . map ( ( color : string ) => {
76- return Color ( color ) ;
77- } )
78- ) ;
96+ getEnabledColorPropertiesFromPaletteCache . set ( configId , colorsFromPaletteWithEnoughDistance ) ;
7997
80- return getEnabledColorsFromPaletteCache . get ( configId ) ! ;
98+ return getEnabledColorPropertiesFromPaletteCache . get ( configId ) ! ;
8199}
82100
83101function getColorcode ( text : string ) : ColorOrFalse {
@@ -148,7 +166,7 @@ export function textToColorHash({
148166}
149167
150168function stringToIntegerHash ( inputString : string ) : number {
151- /* this function is idempotend , meaning it retrieves the same result for the same input
169+ /* this function is idempotent , meaning it retrieves the same result for the same input
152170 no matter how many times it's called */
153171 // Convert the string to a hash code
154172 let hashCode = 0 ;
0 commit comments