Skip to content

Commit d9f7f8a

Browse files
committed
add palette filter for color weight
1 parent 1843cca commit d9f7f8a

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

src/common/utils/colorHash.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ import { colorCalculateDistance } from "./colorCalculateDistance";
66
import CssCustomProperties from "./CssCustomProperties";
77

88
type ColorOrFalse = Color | false;
9+
type ColorWeight = 100 | 300 | 500 | 700 | 900;
10+
type PaletteGroup = "identity" | "semantic" | "layout" | "extra";
911

1012
interface getEnabledColorsProps {
11-
/** Include "identity colors" from palette. */
12-
includeIdentityColors?: boolean;
13-
/** Include "semantic colors" (used for info, success, warning, danger) from palette. */
14-
includeSemanticColors?: boolean;
15-
/** Include "layout colors" from palette. */
16-
includeLayoutColors?: boolean;
17-
/** Include "extra colors" (e.g. gold, silver, bronze) from palette. */
18-
includeExtraColors?: boolean;
13+
/** Specify the palette groups used to define the set of colors. */
14+
includePaletteGroup?: PaletteGroup[];
15+
/** Use only some weights of a color tint. */
16+
includeColorWeight?: ColorWeight[];
1917
/** Only keep colors in the stack with a minimal color distance to all other colors. */
2018
minimalColorDistance?: number;
2119
/** Extend color stack by values generated by mixing tints of the same weight, e.g. `yellow100` with `purple100`. */
@@ -25,19 +23,14 @@ interface getEnabledColorsProps {
2523
const getEnabledColorsFromPaletteCache = new Map<string, Color[]>();
2624

2725
export function getEnabledColorsFromPalette({
28-
includeIdentityColors = false,
29-
includeSemanticColors = false,
30-
includeLayoutColors = true,
31-
includeExtraColors = false,
26+
includePaletteGroup = ["layout"],
27+
includeColorWeight = [100, 300, 500, 700, 900],
3228
// TODO (planned for later): includeMixedColors = false,
3329
minimalColorDistance = COLORMINDISTANCE,
3430
}: getEnabledColorsProps): Color[] {
3531
const configId = JSON.stringify({
36-
includeIdentityColors,
37-
includeSemanticColors,
38-
includeLayoutColors,
39-
includeExtraColors,
40-
minimalColorDistance,
32+
includePaletteGroup,
33+
includeColorWeight,
4134
});
4235

4336
if (getEnabledColorsFromPaletteCache.has(configId)) {
@@ -47,12 +40,15 @@ export function getEnabledColorsFromPalette({
4740
const colorsFromPalette = new CssCustomProperties({
4841
selectorText: `:root`,
4942
filterName: (name: string) => {
50-
return (
51-
(includeIdentityColors && name.includes(`--${eccgui}-color-palette-identity-`)) ||
52-
(includeSemanticColors && name.includes(`--${eccgui}-color-palette-semantic-`)) ||
53-
(includeLayoutColors && name.includes(`--${eccgui}-color-palette-layout-`)) ||
54-
(includeExtraColors && name.includes(`--${eccgui}-color-palette-extra-`))
55-
);
43+
if (!name.includes(`--${eccgui}-color-palette-`)) {
44+
// only allow custom properties created for the palette
45+
return false;
46+
}
47+
// test for correct group and weight of the palette color
48+
const tint = name.substring(`--${eccgui}-color-palette-`.length).split("-");
49+
const group = tint[0] as PaletteGroup;
50+
const weight = parseInt(tint[2], 10) as ColorWeight;
51+
return includePaletteGroup.includes(group) && includeColorWeight.includes(weight);
5652
},
5753
removeDashPrefix: false,
5854
returnObject: true,

0 commit comments

Comments
 (0)