@drawback/ui 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -2
- package/dist/i18n-BZnCrD-C.d.mts +265 -0
- package/dist/index.d.mts +588 -0
- package/dist/index.mjs +4821 -0
- package/dist/locales/de.d.mts +3 -0
- package/dist/locales/de.mjs +231 -0
- package/package.json +69 -4
- package/src/app/default-editor.tsx +371 -0
- package/src/components/color-picker/color-picker.tsx +341 -0
- package/src/components/color-picker/colors.ts +135 -0
- package/src/components/font-picker/font-picker.tsx +298 -0
- package/src/components/icons.tsx +222 -0
- package/src/components/primitives.tsx +565 -0
- package/src/components/style-icons.tsx +172 -0
- package/src/features/canvas-commands/canvas-commands.tsx +173 -0
- package/src/features/context-menu/context-menu.tsx +354 -0
- package/src/features/controls/controls.tsx +281 -0
- package/src/features/export/export-dialog.tsx +164 -0
- package/src/features/layers/layers-tree.ts +335 -0
- package/src/features/layers/layers.tsx +858 -0
- package/src/features/links/links.tsx +268 -0
- package/src/features/main-menu/canvas-properties.tsx +121 -0
- package/src/features/main-menu/main-menu.tsx +215 -0
- package/src/features/minimap/minimap.tsx +69 -0
- package/src/features/properties/linear-properties.tsx +169 -0
- package/src/features/properties/properties.tsx +573 -0
- package/src/features/shortcuts/shortcuts-dialog.tsx +120 -0
- package/src/features/toolbar/toolbar.tsx +216 -0
- package/src/hooks/use-latest.ts +16 -0
- package/src/index.ts +138 -0
- package/src/lib/commands.ts +14 -0
- package/src/lib/download.ts +8 -0
- package/src/lib/i18n.ts +350 -0
- package/src/lib/locales/de.ts +231 -0
- package/src/lib/plugins.tsx +92 -0
- package/src/lib/registry.ts +140 -0
- package/src/stores/ui-context.tsx +253 -0
- package/src/styles.css +1366 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { useEffect, useEffectEvent, useRef, useState, type ReactNode } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
COLOR_GRID,
|
|
4
|
+
COLOR_SHADES,
|
|
5
|
+
DEFAULT_SHADE,
|
|
6
|
+
familyColor,
|
|
7
|
+
familyOf,
|
|
8
|
+
hasShades,
|
|
9
|
+
isDarkColor,
|
|
10
|
+
normalizeHex,
|
|
11
|
+
type ColorKind,
|
|
12
|
+
} from './colors.js';
|
|
13
|
+
import { useUi } from '../../stores/ui-context.js';
|
|
14
|
+
import { Choice, IconButton, Popover, cx } from '../primitives.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The browser's EyeDropper (Chromium only): a magnifier that picks a color from anywhere on
|
|
18
|
+
* screen. `undefined` where unsupported, so the button is hidden there.
|
|
19
|
+
*/
|
|
20
|
+
function eyeDropperOpener(): (() => Promise<string | undefined>) | undefined {
|
|
21
|
+
const EyeDropper: unknown =
|
|
22
|
+
typeof window === 'undefined' ? undefined : Reflect.get(window, 'EyeDropper');
|
|
23
|
+
|
|
24
|
+
if (typeof EyeDropper !== 'function') {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return async () => {
|
|
29
|
+
const dropper = new (EyeDropper as new () => { open(): Promise<{ sRGBHex?: unknown }> })();
|
|
30
|
+
const result = await dropper.open();
|
|
31
|
+
return typeof result.sRGBHex === 'string' ? result.sRGBHex : undefined;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A color chip; transparent shows a checkerboard, `undefined` (mixed values) a hatch. */
|
|
36
|
+
export function Swatch({ color }: { readonly color: string | undefined }): ReactNode {
|
|
37
|
+
return (
|
|
38
|
+
<span
|
|
39
|
+
className={cx(
|
|
40
|
+
'nx-swatch',
|
|
41
|
+
color === 'transparent' && 'nx-swatch-transparent',
|
|
42
|
+
color === undefined && 'nx-swatch-mixed',
|
|
43
|
+
)}
|
|
44
|
+
style={color === undefined || color === 'transparent' ? undefined : { background: color }}
|
|
45
|
+
/>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface ColorPickerProps {
|
|
50
|
+
readonly label: string;
|
|
51
|
+
/** Current color; `undefined` when the selection has mixed values. */
|
|
52
|
+
readonly value: string | undefined;
|
|
53
|
+
/** Which shade the grid offers: dark for strokes, light for backgrounds. */
|
|
54
|
+
readonly kind: ColorKind;
|
|
55
|
+
readonly onChange: (value: string) => void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Excalidraw-style color picker: the color grid (keys q–t, a–g, z–b), the active color's
|
|
60
|
+
* shades (Shift+1–5; none for transparent, white and black) and a hex code input with a
|
|
61
|
+
* pipette that copies a color from the screen (EyeDropper, where the browser has it).
|
|
62
|
+
*/
|
|
63
|
+
export function ColorPicker({ label, value, kind, onChange }: ColorPickerProps): ReactNode {
|
|
64
|
+
const { t } = useUi();
|
|
65
|
+
const active = familyOf(value);
|
|
66
|
+
const shades =
|
|
67
|
+
active !== undefined && hasShades(active.family) ? COLOR_SHADES[active.family] : [];
|
|
68
|
+
// Picking a family keeps the shade of the current color, like Excalidraw.
|
|
69
|
+
const shade = active?.shade ?? DEFAULT_SHADE[kind];
|
|
70
|
+
const hex = value === undefined ? undefined : normalizeHex(value);
|
|
71
|
+
// What the user is typing; `undefined` shows the current color.
|
|
72
|
+
const [draft, setDraft] = useState<string | undefined>(undefined);
|
|
73
|
+
const shown = draft ?? (hex ?? '').slice(1);
|
|
74
|
+
const eyeDropper = eyeDropperOpener();
|
|
75
|
+
|
|
76
|
+
const onHotkey = useEffectEvent((event: globalThis.KeyboardEvent): void => {
|
|
77
|
+
const target = event.target;
|
|
78
|
+
const typing =
|
|
79
|
+
(target instanceof HTMLInputElement && target.type !== 'color') ||
|
|
80
|
+
target instanceof HTMLTextAreaElement ||
|
|
81
|
+
(target instanceof HTMLElement && target.isContentEditable);
|
|
82
|
+
|
|
83
|
+
if (typing || event.ctrlKey || event.metaKey || event.altKey || event.repeat) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Shift+1–5 pick a shade; `code` because Shift turns `key` into "!" etc. on most layouts.
|
|
88
|
+
const digit = /^(?:Digit|Numpad)([1-5])$/.exec(event.code)?.[1];
|
|
89
|
+
const color =
|
|
90
|
+
event.shiftKey && digit !== undefined
|
|
91
|
+
? shades[Number(digit) - 1]
|
|
92
|
+
: event.shiftKey
|
|
93
|
+
? undefined
|
|
94
|
+
: (() => {
|
|
95
|
+
const cell = COLOR_GRID.find((entry) => entry.key === event.key.toLowerCase());
|
|
96
|
+
return cell === undefined ? undefined : familyColor(cell.family, shade);
|
|
97
|
+
})();
|
|
98
|
+
|
|
99
|
+
if (color !== undefined) {
|
|
100
|
+
event.preventDefault();
|
|
101
|
+
event.stopPropagation();
|
|
102
|
+
onChange(color);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// While the picker is open its hotkeys win over canvas shortcuts (e.g. "a" for arrows),
|
|
107
|
+
// wherever focus is, except while typing in a text field.
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
const onKey = (event: globalThis.KeyboardEvent): void => onHotkey(event);
|
|
110
|
+
document.addEventListener('keydown', onKey, true);
|
|
111
|
+
return () => document.removeEventListener('keydown', onKey, true);
|
|
112
|
+
}, []);
|
|
113
|
+
|
|
114
|
+
const root = useRef<HTMLDivElement>(null);
|
|
115
|
+
const activeFamily = active?.family;
|
|
116
|
+
|
|
117
|
+
// Keep keyboard focus on the active color as hotkeys change it (unless typing a hex code).
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
const focused = document.activeElement;
|
|
120
|
+
|
|
121
|
+
if (
|
|
122
|
+
root.current !== null &&
|
|
123
|
+
focused instanceof HTMLElement &&
|
|
124
|
+
root.current.contains(focused) &&
|
|
125
|
+
!(focused instanceof HTMLInputElement)
|
|
126
|
+
) {
|
|
127
|
+
root.current.querySelector<HTMLElement>('.nx-color-grid [aria-checked="true"]')?.focus();
|
|
128
|
+
}
|
|
129
|
+
}, [activeFamily]);
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
<div
|
|
133
|
+
ref={root}
|
|
134
|
+
className="nx-color-picker"
|
|
135
|
+
>
|
|
136
|
+
<h4 className="nx-color-picker-title">{label}</h4>
|
|
137
|
+
<span className="nx-caption">{t('color.colors')}</span>
|
|
138
|
+
<div
|
|
139
|
+
className="nx-color-grid"
|
|
140
|
+
role="radiogroup"
|
|
141
|
+
aria-label={t('color.colors')}
|
|
142
|
+
>
|
|
143
|
+
{COLOR_GRID.map(({ key, family }) => {
|
|
144
|
+
const color = familyColor(family, shade);
|
|
145
|
+
return (
|
|
146
|
+
<button
|
|
147
|
+
key={key}
|
|
148
|
+
type="button"
|
|
149
|
+
role="radio"
|
|
150
|
+
aria-checked={active?.family === family}
|
|
151
|
+
aria-label={`${t(`color.name.${family}`)} (${key.toUpperCase()})`}
|
|
152
|
+
title={`${t(`color.name.${family}`)} — ${key.toUpperCase()}`}
|
|
153
|
+
className={cx('nx-color-cell', isDarkColor(color) && 'nx-on-dark')}
|
|
154
|
+
data-testid={`color-${family}`}
|
|
155
|
+
data-autofocus={active?.family === family ? '' : undefined}
|
|
156
|
+
onClick={() => onChange(color)}
|
|
157
|
+
>
|
|
158
|
+
<Swatch color={color} />
|
|
159
|
+
<span className="nx-color-key">{key}</span>
|
|
160
|
+
</button>
|
|
161
|
+
);
|
|
162
|
+
})}
|
|
163
|
+
</div>
|
|
164
|
+
<span className="nx-caption">{t('color.shades')}</span>
|
|
165
|
+
{shades.length === 0 ? (
|
|
166
|
+
<p
|
|
167
|
+
className="nx-color-no-shades"
|
|
168
|
+
data-testid="color-no-shades"
|
|
169
|
+
>
|
|
170
|
+
{t('color.noShades')}
|
|
171
|
+
</p>
|
|
172
|
+
) : (
|
|
173
|
+
<div
|
|
174
|
+
className="nx-color-shades"
|
|
175
|
+
role="radiogroup"
|
|
176
|
+
aria-label={t('color.shades')}
|
|
177
|
+
>
|
|
178
|
+
{shades.map((color, index) => (
|
|
179
|
+
<button
|
|
180
|
+
key={color}
|
|
181
|
+
type="button"
|
|
182
|
+
role="radio"
|
|
183
|
+
aria-checked={active?.shade === index}
|
|
184
|
+
aria-label={`${t('color.shade', { index: index + 1 })} (Shift+${index + 1})`}
|
|
185
|
+
title={`${t('color.shade', { index: index + 1 })} — Shift+${index + 1}`}
|
|
186
|
+
className={cx('nx-color-cell', isDarkColor(color) && 'nx-on-dark')}
|
|
187
|
+
data-testid={`color-shade-${index + 1}`}
|
|
188
|
+
onClick={() => onChange(color)}
|
|
189
|
+
>
|
|
190
|
+
<Swatch color={color} />
|
|
191
|
+
<span className="nx-color-key">⇧{index + 1}</span>
|
|
192
|
+
</button>
|
|
193
|
+
))}
|
|
194
|
+
</div>
|
|
195
|
+
)}
|
|
196
|
+
<span className="nx-caption">{t('color.hex')}</span>
|
|
197
|
+
<div className="nx-color-hex">
|
|
198
|
+
<span aria-hidden>#</span>
|
|
199
|
+
<input
|
|
200
|
+
type="text"
|
|
201
|
+
value={shown}
|
|
202
|
+
spellCheck={false}
|
|
203
|
+
maxLength={7}
|
|
204
|
+
aria-label={t('color.hex')}
|
|
205
|
+
data-testid="color-hex"
|
|
206
|
+
placeholder={value === undefined ? t('properties.mixed') : ''}
|
|
207
|
+
onChange={(event) => {
|
|
208
|
+
setDraft(event.target.value.replace(/^#/, ''));
|
|
209
|
+
const next = normalizeHex(event.target.value);
|
|
210
|
+
|
|
211
|
+
if (next !== undefined && event.target.value.replace(/^#/, '').length === 6) {
|
|
212
|
+
onChange(next);
|
|
213
|
+
}
|
|
214
|
+
}}
|
|
215
|
+
onKeyDown={(event) => {
|
|
216
|
+
if (event.key === 'Enter') {
|
|
217
|
+
const next = normalizeHex(shown);
|
|
218
|
+
|
|
219
|
+
if (next !== undefined) {
|
|
220
|
+
onChange(next);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
setDraft(undefined);
|
|
224
|
+
}
|
|
225
|
+
}}
|
|
226
|
+
onBlur={() => {
|
|
227
|
+
const next = normalizeHex(shown);
|
|
228
|
+
|
|
229
|
+
if (next !== undefined && next !== hex) {
|
|
230
|
+
onChange(next);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Invalid input falls back to the current color.
|
|
234
|
+
setDraft(undefined);
|
|
235
|
+
}}
|
|
236
|
+
/>
|
|
237
|
+
{eyeDropper !== undefined && (
|
|
238
|
+
<IconButton
|
|
239
|
+
icon="pipette"
|
|
240
|
+
label={t('color.picker')}
|
|
241
|
+
testId="color-eyedropper"
|
|
242
|
+
onClick={() => {
|
|
243
|
+
// Magnifier to pick a color from anywhere on screen; Escape cancels (AbortError).
|
|
244
|
+
eyeDropper()
|
|
245
|
+
.then((picked) => {
|
|
246
|
+
const next = picked === undefined ? undefined : normalizeHex(picked);
|
|
247
|
+
|
|
248
|
+
if (next !== undefined) {
|
|
249
|
+
onChange(next);
|
|
250
|
+
}
|
|
251
|
+
})
|
|
252
|
+
.catch(() => undefined);
|
|
253
|
+
}}
|
|
254
|
+
/>
|
|
255
|
+
)}
|
|
256
|
+
</div>
|
|
257
|
+
</div>
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export interface ColorFieldProps {
|
|
262
|
+
readonly label: string;
|
|
263
|
+
/** Current color; `undefined` when mixed. */
|
|
264
|
+
readonly value: string | undefined;
|
|
265
|
+
/** Quick picks next to the picker button (e.g. `STROKE_COLORS`, `FILL_COLORS`). */
|
|
266
|
+
readonly palette: readonly string[];
|
|
267
|
+
readonly onChange: (value: string) => void;
|
|
268
|
+
/** Which shades the picker grid offers (default `'stroke'`). */
|
|
269
|
+
readonly kind?: ColorKind;
|
|
270
|
+
readonly testId?: string;
|
|
271
|
+
readonly disabled?: boolean;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Quick-pick swatches, then the current color as a button that opens the {@link ColorPicker}
|
|
276
|
+
* (Excalidraw's stroke and background controls).
|
|
277
|
+
*/
|
|
278
|
+
export function ColorField({
|
|
279
|
+
label,
|
|
280
|
+
value,
|
|
281
|
+
palette,
|
|
282
|
+
onChange,
|
|
283
|
+
kind = 'stroke',
|
|
284
|
+
testId,
|
|
285
|
+
disabled,
|
|
286
|
+
}: ColorFieldProps): ReactNode {
|
|
287
|
+
const { t } = useUi();
|
|
288
|
+
const [open, setOpen] = useState(false);
|
|
289
|
+
const anchor = useRef<HTMLButtonElement>(null);
|
|
290
|
+
return (
|
|
291
|
+
<div
|
|
292
|
+
className="nx-color-field"
|
|
293
|
+
data-testid={testId}
|
|
294
|
+
>
|
|
295
|
+
<Choice
|
|
296
|
+
label={label}
|
|
297
|
+
value={value}
|
|
298
|
+
disabled={disabled}
|
|
299
|
+
options={palette.map((color) => ({
|
|
300
|
+
value: color,
|
|
301
|
+
label: color === 'transparent' ? t('value.none') : color,
|
|
302
|
+
swatch: color,
|
|
303
|
+
}))}
|
|
304
|
+
onChange={onChange}
|
|
305
|
+
/>
|
|
306
|
+
<span
|
|
307
|
+
className="nx-color-divider"
|
|
308
|
+
aria-hidden
|
|
309
|
+
/>
|
|
310
|
+
<button
|
|
311
|
+
ref={anchor}
|
|
312
|
+
type="button"
|
|
313
|
+
className="nx-choice-option nx-swatch-option nx-color-current"
|
|
314
|
+
aria-label={t('color.open', { label })}
|
|
315
|
+
title={t('color.open', { label })}
|
|
316
|
+
aria-haspopup="dialog"
|
|
317
|
+
aria-expanded={open}
|
|
318
|
+
disabled={disabled}
|
|
319
|
+
data-testid="color-picker-toggle"
|
|
320
|
+
onClick={() => setOpen(!open)}
|
|
321
|
+
>
|
|
322
|
+
<Swatch color={value} />
|
|
323
|
+
</button>
|
|
324
|
+
<Popover
|
|
325
|
+
open={open}
|
|
326
|
+
onClose={() => setOpen(false)}
|
|
327
|
+
anchor={anchor}
|
|
328
|
+
placement="right-start"
|
|
329
|
+
label={label}
|
|
330
|
+
testId="color-picker"
|
|
331
|
+
>
|
|
332
|
+
<ColorPicker
|
|
333
|
+
label={label}
|
|
334
|
+
value={value}
|
|
335
|
+
kind={kind}
|
|
336
|
+
onChange={onChange}
|
|
337
|
+
/>
|
|
338
|
+
</Popover>
|
|
339
|
+
</div>
|
|
340
|
+
);
|
|
341
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Color palette for the color picker, laid out like Excalidraw's: a 5×3 grid of color families
|
|
3
|
+
* picked with the keys q–t / a–g / z–b, each family (except transparent, white and black) with five
|
|
4
|
+
* shades picked with Shift+1–5. Shades are the open-color scale (MIT).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type ShadedFamily =
|
|
8
|
+
| 'gray'
|
|
9
|
+
| 'bronze'
|
|
10
|
+
| 'cyan'
|
|
11
|
+
| 'blue'
|
|
12
|
+
| 'violet'
|
|
13
|
+
| 'grape'
|
|
14
|
+
| 'pink'
|
|
15
|
+
| 'green'
|
|
16
|
+
| 'teal'
|
|
17
|
+
| 'yellow'
|
|
18
|
+
| 'orange'
|
|
19
|
+
| 'red';
|
|
20
|
+
export type ColorFamily = 'transparent' | 'white' | 'black' | ShadedFamily;
|
|
21
|
+
|
|
22
|
+
/** Lightest to darkest. */
|
|
23
|
+
export const COLOR_SHADES: Readonly<Record<ShadedFamily, readonly string[]>> = {
|
|
24
|
+
gray: ['#f8f9fa', '#e9ecef', '#ced4da', '#868e96', '#343a40'],
|
|
25
|
+
bronze: ['#f8f1ee', '#eaddd7', '#d2bab0', '#a18072', '#846358'],
|
|
26
|
+
cyan: ['#e3fafc', '#99e9f2', '#3bc9db', '#15aabf', '#0c8599'],
|
|
27
|
+
blue: ['#e7f5ff', '#a5d8ff', '#4dabf7', '#228be6', '#1971c2'],
|
|
28
|
+
violet: ['#f3f0ff', '#d0bfff', '#9775fa', '#7950f2', '#6741d9'],
|
|
29
|
+
grape: ['#f8f0fc', '#eebefa', '#da77f2', '#be4bdb', '#9c36b5'],
|
|
30
|
+
pink: ['#fff0f6', '#fcc2d7', '#f783ac', '#e64980', '#c2255c'],
|
|
31
|
+
green: ['#ebfbee', '#b2f2bb', '#69db7c', '#40c057', '#2f9e44'],
|
|
32
|
+
teal: ['#e6fcf5', '#96f2d7', '#38d9a9', '#12b886', '#099268'],
|
|
33
|
+
yellow: ['#fff9db', '#ffec99', '#ffd43b', '#fab005', '#f08c00'],
|
|
34
|
+
orange: ['#fff4e6', '#ffd8a8', '#ffa94d', '#fd7e14', '#e8590c'],
|
|
35
|
+
red: ['#fff5f5', '#ffc9c9', '#ff8787', '#fa5252', '#e03131'],
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const SOLID: Readonly<Record<'transparent' | 'white' | 'black', string>> = {
|
|
39
|
+
transparent: 'transparent',
|
|
40
|
+
white: '#ffffff',
|
|
41
|
+
black: '#1e1e1e',
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** The picker grid, row by row, with each cell's hotkey. */
|
|
45
|
+
export const COLOR_GRID: readonly { readonly key: string; readonly family: ColorFamily }[] = [
|
|
46
|
+
{ key: 'q', family: 'transparent' },
|
|
47
|
+
{ key: 'w', family: 'white' },
|
|
48
|
+
{ key: 'e', family: 'gray' },
|
|
49
|
+
{ key: 'r', family: 'black' },
|
|
50
|
+
{ key: 't', family: 'bronze' },
|
|
51
|
+
{ key: 'a', family: 'cyan' },
|
|
52
|
+
{ key: 's', family: 'blue' },
|
|
53
|
+
{ key: 'd', family: 'violet' },
|
|
54
|
+
{ key: 'f', family: 'grape' },
|
|
55
|
+
{ key: 'g', family: 'pink' },
|
|
56
|
+
{ key: 'z', family: 'green' },
|
|
57
|
+
{ key: 'x', family: 'teal' },
|
|
58
|
+
{ key: 'c', family: 'yellow' },
|
|
59
|
+
{ key: 'v', family: 'orange' },
|
|
60
|
+
{ key: 'b', family: 'red' },
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
/** Shade index the grid shows for strokes (darkest) and backgrounds (light). */
|
|
64
|
+
export const DEFAULT_SHADE = { stroke: 4, background: 1 } as const;
|
|
65
|
+
export type ColorKind = keyof typeof DEFAULT_SHADE;
|
|
66
|
+
|
|
67
|
+
/** Quick picks shown next to the picker button, as in Excalidraw. */
|
|
68
|
+
export const STROKE_COLORS = ['#1e1e1e', '#e03131', '#2f9e44', '#1971c2', '#f08c00'];
|
|
69
|
+
export const FILL_COLORS = ['transparent', '#ffc9c9', '#b2f2bb', '#a5d8ff', '#ffec99'];
|
|
70
|
+
|
|
71
|
+
export function hasShades(family: ColorFamily): family is ShadedFamily {
|
|
72
|
+
return family in COLOR_SHADES;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The color a grid cell stands for at a shade index (solid families ignore it). */
|
|
76
|
+
export function familyColor(family: ColorFamily, shade: number): string {
|
|
77
|
+
return hasShades(family)
|
|
78
|
+
? (COLOR_SHADES[family][shade] ?? COLOR_SHADES[family][0] ?? '#000000')
|
|
79
|
+
: SOLID[family];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The family (and shade index, for shaded families) a color belongs to, if it is in the palette. */
|
|
83
|
+
export function familyOf(
|
|
84
|
+
color: string | undefined,
|
|
85
|
+
): { readonly family: ColorFamily; readonly shade: number | undefined } | undefined {
|
|
86
|
+
if (color === undefined) {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const value = color.trim().toLowerCase();
|
|
91
|
+
|
|
92
|
+
for (const [family, solid] of Object.entries(SOLID)) {
|
|
93
|
+
if (value === solid) {
|
|
94
|
+
return { family: family as ColorFamily, shade: undefined };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
for (const [family, shades] of Object.entries(COLOR_SHADES)) {
|
|
99
|
+
const shade = shades.indexOf(value);
|
|
100
|
+
|
|
101
|
+
if (shade >= 0) {
|
|
102
|
+
return { family: family as ShadedFamily, shade };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** `#rgb`/`#rrggbb` (the `#` optional) as lower-case `#rrggbb`; `undefined` when invalid. */
|
|
110
|
+
export function normalizeHex(input: string): string | undefined {
|
|
111
|
+
const match = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(input.trim());
|
|
112
|
+
|
|
113
|
+
if (match === null) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const digits = (match[1] ?? '').toLowerCase();
|
|
118
|
+
return `#${digits.length === 3 ? [...digits].map((digit) => digit + digit).join('') : digits}`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Whether light text reads better on `color` (hex colors only; others count as light). */
|
|
122
|
+
export function isDarkColor(color: string): boolean {
|
|
123
|
+
const hex = normalizeHex(color);
|
|
124
|
+
|
|
125
|
+
if (hex === undefined) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const channel = (offset: number): number => {
|
|
130
|
+
const value = Number.parseInt(hex.slice(offset, offset + 2), 16) / 255;
|
|
131
|
+
return value <= 0.03928 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
|
|
132
|
+
};
|
|
133
|
+
const luminance = 0.2126 * channel(1) + 0.7152 * channel(3) + 0.0722 * channel(5);
|
|
134
|
+
return luminance < 0.35;
|
|
135
|
+
}
|