-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcolorUtils.ts
221 lines (200 loc) · 6.85 KB
/
colorUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { getExpressionRanges } from '@deephaven/components';
import { ColorUtils } from '@deephaven/utils';
export const INVALID_COLOR_BORDER_STYLE = '2px solid var(--dh-color-notice-bg)';
// Group names are extracted from var names via a regex capture group. Most of
// them work pretty well, but some need to be remapped to a more appropriate
// group.
const REASSIGN_VARIABLE_GROUPS: Record<string, string> = {
'--dh-color-black': 'gray',
'--dh-color-white': 'gray',
// Semantic
'--dh-color-border': 'General',
'--dh-color-bg': 'General',
'--dh-color-surface-bg': 'General',
'--dh-color-fg': 'General',
'--dh-color-content-bg': 'General',
'--dh-color-subdued-content-bg': 'General',
'--dh-color-dropshadow': 'General',
'--dh-color-keyboard-selected-bg': 'Misc',
'--dh-color-hover-border': 'Misc',
'--dh-color-visual-positive': 'Visual Status',
'--dh-color-visual-negative': 'Visual Status',
'--dh-color-visual-notice': 'Visual Status',
'--dh-color-visual-info': 'Visual Status',
'--dh-color-gray-bg': 'Default Background',
'--dh-color-red-bg': 'Default Background',
'--dh-color-orange-bg': 'Default Background',
'--dh-color-yellow-bg': 'Default Background',
'--dh-color-chartreuse-bg': 'Default Background',
'--dh-color-celery-bg': 'Default Background',
'--dh-color-green-bg': 'Default Background',
'--dh-color-seafoam-bg': 'Default Background',
'--dh-color-cyan-bg': 'Default Background',
'--dh-color-blue-bg': 'Default Background',
'--dh-color-indigo-bg': 'Default Background',
'--dh-color-purple-bg': 'Default Background',
'--dh-color-fuchsia-bg': 'Default Background',
'--dh-color-magenta-bg': 'Default Background',
// Editor
'--dh-color-editor-bg': 'editor',
'--dh-color-editor-fg': 'editor',
'--dh-color-editor-context-menu-bg': 'menus',
'--dh-color-editor-context-menu-fg': 'menus',
'--dh-color-editor-menu-selection-bg': 'menus',
// Grid
'--dh-color-grid-bg': 'grid',
'--dh-color-grid-number-positive': 'Data Types',
'--dh-color-grid-number-negative': 'Data Types',
'--dh-color-grid-number-zero': 'Data Types',
'--dh-color-grid-date': 'Data Types',
'--dh-color-grid-string-null': 'Data Types',
} as const;
const SWATCH_LABEL = {
'--dh-color-black': '',
'--dh-color-action-active-bg': 'Action.active',
'--dh-color-action-down-bg': 'Action:active',
'--dh-color-action-hover-bg': 'Action:hover',
'--dh-color-action-active-hover-bg': 'Action.active:hover',
'--dh-color-action-disabled-bg': 'Action:disabled',
};
// Mappings of variable groups to rename
const RENAME_VARIABLE_GROUPS = {
palette: {
black: 'gray',
white: 'gray',
},
editor: {
line: 'editor',
comment: 'code',
string: 'code',
number: 'code',
delimiter: 'code',
identifier: 'code',
keyword: 'code',
operator: 'code',
storage: 'code',
predefined: 'code',
selection: 'state',
focus: 'state',
},
chart: {
axis: 'Chart',
bg: 'Chart',
grid: 'Chart',
plot: 'Chart',
title: 'Chart',
active: 'Data',
trend: 'Data',
area: 'Data',
range: 'Data',
line: 'Deprecated',
},
grid: { data: 'Data Bars', context: 'Context Menu' },
semantic: {},
component: {},
} satisfies Record<string, Record<string, string>>;
/**
* Return black or white contrast color.
*
* Note that this should be sufficient for styleguide swatch examples, but it
* may not completely align with how Spectrum determines contrast colors, hence
* leaving this here instead of promoting to `ColorUtils`.
*/
export function contrastColor(color: string): 'black' | 'white' {
const rgba = ColorUtils.parseRgba(ColorUtils.asRgbOrRgbaString(color) ?? '');
if (rgba == null || rgba.a < 0.5) {
return 'white';
}
const { r, g, b } = rgba;
return ColorUtils.getBrightness([r, g, b]) >= 128 ? 'black' : 'white';
}
/** Extract an array of { name, value } pairs for css variables in a given string */
export function extractColorVars(
styleText: string
): { name: string; value: string }[] {
const computedStyle = getComputedStyle(document.documentElement);
const varNames = styleText
.split(/[\n;]/g)
// Non-minified css will have leading 2 spaces in front of each css variable
// declaration. Minified has no prefix except for the first line which will
// have ':root{' prefix.
.map(line => /^(?:\s{2}|:root\{)?(--dh-color-(?:[^:]+))/.exec(line)?.[1])
.filter((match): match is string => Boolean(match));
return varNames
.map(varName => {
const value = computedStyle.getPropertyValue(varName);
// Chart colorway consists of multiple colors, so split into separate
// swatches for illustration. Note that this assumes the colors are hsl
// values. We'll need to make this more robust if we ever change the
// default themes to use non-hsl.
if (varName === '--dh-color-chart-colorway') {
const colorwayColors = getExpressionRanges(value ?? '').map(
([start, end]) => value.substring(start, end + 1)
);
return colorwayColors.map((varExp, i) => ({
name: `${varName}-${i}`,
value: varExp,
}));
}
return {
name: varName,
value,
};
})
.flat();
}
/** Group color data based on capture group value */
export function buildColorGroups(
groupKey: keyof typeof RENAME_VARIABLE_GROUPS,
styleText: string,
captureGroupI: number,
reassignVarGroups: Record<string, string> = REASSIGN_VARIABLE_GROUPS
): Record<
string,
{ isLabel?: boolean; name: string; value: string; note?: string }[]
> {
const groupRemap: Record<string, string> = RENAME_VARIABLE_GROUPS[groupKey];
const swatchData = extractColorVars(styleText);
const groupData = swatchData.reduce(
(acc, { name, value }) => {
// Skip true black/white
if (/^--dh-color-true-(.*?)$/.test(name)) {
return acc;
}
// Skip gray light/mid/dark as they will be marked via notes on the gray
// numbered palette
if (/^--dh-color-gray-(light|mid|dark)$/.test(name)) {
return acc;
}
const match = /^--dh-color-([^-]+)(?:-([^-]+))?/.exec(name);
let group =
reassignVarGroups[name] ??
match?.[captureGroupI] ??
match?.[1] ??
'???';
group = groupRemap[group] ?? group;
if (acc[group] == null) {
acc[group] = [];
}
// Add a spacer for black / white
if (name in SWATCH_LABEL) {
acc[group].push({
isLabel: true,
name: SWATCH_LABEL[name as keyof typeof SWATCH_LABEL],
value: '',
});
}
// Skip gray light/mid/dark as we are planning to remove them
if (/^--dh-color-gray-(light|mid|dark)$/.test(name)) {
return acc;
}
acc[group].push({ name, value });
return acc;
},
{} as Record<
string,
{ isLabel?: boolean; name: string; value: string; note?: string }[]
>
);
return groupData;
}