@glasshome/ui 1.41.0 → 1.43.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/SPEC.md +5 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -3
- package/dist/lib/anchor-placement.d.ts +34 -0
- package/dist/lib/balance-rows.d.ts +10 -4
- package/dist/lib/behavior/anchored.d.ts +26 -0
- package/dist/lib/behavior/button-root.d.ts +7 -0
- package/dist/lib/behavior/collection.d.ts +32 -0
- package/dist/lib/behavior/controllable.d.ts +8 -0
- package/dist/lib/behavior/dismiss.d.ts +13 -0
- package/dist/lib/behavior/focus-scope.d.ts +24 -0
- package/dist/lib/behavior/hide-outside.d.ts +7 -0
- package/dist/lib/behavior/layer-stack.d.ts +34 -0
- package/dist/lib/behavior/menu.d.ts +59 -0
- package/dist/lib/behavior/modal-root.d.ts +68 -0
- package/dist/lib/behavior/polymorphic.d.ts +6 -0
- package/dist/lib/behavior/presence.d.ts +18 -0
- package/dist/lib/behavior/range.d.ts +44 -0
- package/dist/lib/behavior/solid.d.ts +13 -0
- package/dist/lib/color/color.d.ts +41 -0
- package/dist/lib/color/index.d.ts +2 -0
- package/dist/lib/color/intl.d.ts +40 -0
- package/dist/lib/dock-slot-classes.d.ts +2 -0
- package/dist/lib/hold.d.ts +1 -1
- package/dist/lib/locale.d.ts +3 -0
- package/dist/lib/menu-classes.d.ts +2 -0
- package/dist/lib/motion-classes.d.ts +5 -2
- package/dist/lib/overlay-classes.d.ts +4 -0
- package/dist/lib/page-cover.d.ts +8 -0
- package/dist/lib/scroll-track.d.ts +3 -2
- package/dist/lib/section-tokens.d.ts +1 -1
- package/dist/lib/segment-classes.d.ts +2 -0
- package/dist/lib/shadow-layer.d.ts +0 -43
- package/dist/lib/toggle-set.d.ts +44 -0
- package/dist/lib/visually-hidden.d.ts +3 -0
- package/dist/solid/accordion.d.ts +35 -11
- package/dist/solid/alert-dialog.d.ts +5 -8
- package/dist/solid/avatar.d.ts +8 -4
- package/dist/solid/bottom-sheet/bottom-sheet.d.ts +2 -2
- package/dist/solid/bottom-sheet/constants.d.ts +1 -1
- package/dist/solid/button.d.ts +5 -4
- package/dist/solid/charts.d.ts +1 -1
- package/dist/solid/checkbox.d.ts +14 -4
- package/dist/solid/collapsible.d.ts +33 -5
- package/dist/solid/color-disc.d.ts +1 -1
- package/dist/solid/color-slider.d.ts +1 -1
- package/dist/solid/color-wheel.d.ts +1 -1
- package/dist/solid/context-menu.d.ts +58 -24
- package/dist/solid/dialog-parts.d.ts +53 -16
- package/dist/solid/dialog.d.ts +7 -10
- package/dist/solid/dropdown-menu.d.ts +55 -24
- package/dist/solid/empty.d.ts +1 -1
- package/dist/solid/form.d.ts +5 -6
- package/dist/solid/hover-card.d.ts +65 -9
- package/dist/solid/index.d.ts +5 -2
- package/dist/solid/index.js +8671 -14317
- package/dist/solid/input-group.d.ts +6 -6
- package/dist/solid/item.d.ts +1 -1
- package/dist/solid/menu-parts.d.ts +175 -24
- package/dist/solid/option-card.d.ts +1 -4
- package/dist/solid/picker-row.d.ts +2 -0
- package/dist/solid/popover.d.ts +77 -12
- package/dist/solid/progress.d.ts +11 -3
- package/dist/solid/radio-group.d.ts +31 -6
- package/dist/solid/responsive-dialog.d.ts +3 -1
- package/dist/solid/scroll-area.d.ts +1 -1
- package/dist/solid/section-card.d.ts +2 -3
- package/dist/solid/select.d.ts +107 -10
- package/dist/solid/selection.d.ts +10 -1
- package/dist/solid/sheet.d.ts +6 -9
- package/dist/solid/sliding-indicator.d.ts +1 -1
- package/dist/solid/sonner.d.ts +1 -1
- package/dist/solid/stale-veil.d.ts +7 -0
- package/dist/solid/tabs.d.ts +28 -9
- package/dist/solid/toggle-group.d.ts +30 -5
- package/dist/solid/toggle.d.ts +11 -3
- package/dist/solid/tooltip.d.ts +71 -9
- package/dist/tokens/index.js +54 -51
- package/dist/{utils-CGN4X-2O.js → utils-3Codx-NZ.js} +848 -618
- package/package.json +6 -7
- package/src/index.ts +1 -0
- package/src/lib/anchor-placement.ts +77 -4
- package/src/lib/balance-rows.ts +115 -40
- package/src/lib/behavior/anchored.ts +106 -0
- package/src/lib/behavior/button-root.tsx +103 -0
- package/src/lib/behavior/collection.ts +218 -0
- package/src/lib/behavior/controllable.ts +22 -0
- package/src/lib/behavior/dismiss.ts +185 -0
- package/src/lib/behavior/focus-scope.ts +227 -0
- package/src/lib/behavior/hide-outside.ts +85 -0
- package/src/lib/behavior/layer-stack.ts +150 -0
- package/src/lib/behavior/menu.ts +470 -0
- package/src/lib/behavior/modal-root.tsx +452 -0
- package/src/lib/behavior/polymorphic.tsx +30 -0
- package/src/lib/behavior/presence.ts +89 -0
- package/src/lib/behavior/range.ts +212 -0
- package/src/lib/behavior/solid.ts +30 -0
- package/src/lib/clipboard.ts +4 -4
- package/src/lib/color/color.ts +746 -0
- package/src/lib/color/index.ts +2 -0
- package/src/lib/color/intl.ts +60 -0
- package/src/lib/dock-slot-classes.ts +3 -0
- package/src/lib/hold.ts +1 -1
- package/src/lib/locale.ts +62 -0
- package/src/lib/menu-classes.ts +3 -0
- package/src/lib/motion-classes.ts +33 -8
- package/src/lib/overlay-classes.ts +8 -3
- package/src/lib/page-cover.ts +47 -0
- package/src/lib/scroll-track.ts +11 -6
- package/src/lib/section-tokens.ts +1 -1
- package/src/lib/segment-classes.ts +3 -0
- package/src/lib/shadow-layer.ts +0 -136
- package/src/lib/toggle-set.ts +133 -0
- package/src/lib/visually-hidden.ts +15 -0
- package/src/solid/accordion.tsx +294 -20
- package/src/solid/alert-dialog.tsx +36 -85
- package/src/solid/area-picker.tsx +5 -3
- package/src/solid/avatar.tsx +98 -24
- package/src/solid/badge.tsx +5 -5
- package/src/solid/bottom-sheet/bottom-sheet.tsx +73 -129
- package/src/solid/bottom-sheet/constants.ts +3 -1
- package/src/solid/bottom-sheet/drag-controller.ts +11 -0
- package/src/solid/breadcrumb.tsx +4 -4
- package/src/solid/button-group.tsx +4 -4
- package/src/solid/button.tsx +12 -6
- package/src/solid/card.tsx +5 -4
- package/src/solid/charts.tsx +1 -1
- package/src/solid/checkbox.tsx +167 -44
- package/src/solid/collapsible.tsx +260 -12
- package/src/solid/color-disc.tsx +11 -12
- package/src/solid/color-slider.tsx +114 -47
- package/src/solid/color-wheel.tsx +137 -39
- package/src/solid/context-menu.tsx +287 -86
- package/src/solid/dialog-parts.tsx +135 -46
- package/src/solid/dialog.tsx +44 -76
- package/src/solid/dock-edit.tsx +5 -4
- package/src/solid/dock.tsx +6 -5
- package/src/solid/dropdown-menu.tsx +252 -107
- package/src/solid/empty.tsx +1 -1
- package/src/solid/entity-selector.tsx +5 -3
- package/src/solid/face-toggle.tsx +43 -18
- package/src/solid/form.tsx +9 -9
- package/src/solid/glyph-tile.tsx +48 -21
- package/src/solid/hover-card.tsx +288 -21
- package/src/solid/icon-picker.tsx +2 -1
- package/src/solid/image-picker.tsx +6 -5
- package/src/solid/index.ts +5 -4
- package/src/solid/input-group.tsx +7 -5
- package/src/solid/input-otp.tsx +2 -1
- package/src/solid/item.tsx +5 -5
- package/src/solid/logo.tsx +5 -4
- package/src/solid/menu-parts.tsx +619 -145
- package/src/solid/option-card.tsx +6 -10
- package/src/solid/picker-row.tsx +10 -5
- package/src/solid/picker-search.tsx +2 -1
- package/src/solid/popover.tsx +424 -71
- package/src/solid/preview-tile.tsx +1 -1
- package/src/solid/progress.tsx +84 -15
- package/src/solid/radio-group.tsx +271 -34
- package/src/solid/responsive-dialog.tsx +10 -10
- package/src/solid/schema-form.tsx +1 -6
- package/src/solid/scroll-area.tsx +1 -1
- package/src/solid/section-card.tsx +3 -4
- package/src/solid/select.tsx +950 -95
- package/src/solid/selection.tsx +142 -66
- package/src/solid/sheet.tsx +48 -80
- package/src/solid/slider.tsx +115 -65
- package/src/solid/sliding-indicator.tsx +1 -1
- package/src/solid/sonner.tsx +1 -1
- package/src/solid/spotlight.tsx +23 -11
- package/src/solid/stale-veil.tsx +20 -0
- package/src/solid/switch.tsx +8 -6
- package/src/solid/tabs.tsx +377 -52
- package/src/solid/term.tsx +3 -4
- package/src/solid/toggle-group.tsx +237 -95
- package/src/solid/toggle.tsx +43 -9
- package/src/solid/tooltip.tsx +285 -26
- package/src/styles/globals.css +64 -10
- package/src/styles/theme.css +12 -4
- package/src/tokens/material.ts +11 -2
- package/dist/lib/when-animations-settle.d.ts +0 -4
- package/dist/solid/bottom-sheet/focus-trap.d.ts +0 -6
- package/dist/solid/bottom-sheet/scroll-lock.d.ts +0 -2
- package/dist/solid/bottom-sheet/sheet-motion.d.ts +0 -1
- package/src/lib/when-animations-settle.ts +0 -12
- package/src/solid/bottom-sheet/focus-trap.ts +0 -63
- package/src/solid/bottom-sheet/scroll-lock.ts +0 -66
- package/src/solid/bottom-sheet/sheet-motion.ts +0 -4
|
@@ -0,0 +1,746 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*
|
|
12
|
+
* Ported from @internationalized/color by way of Kobalte's colors module (MIT).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { locale } from "../locale.js";
|
|
16
|
+
import type { ColorIntlTranslations } from "./intl.js";
|
|
17
|
+
|
|
18
|
+
type ColorFormat = "hex" | "hexa" | "rgb" | "rgba" | "hsl" | "hsla" | "hsb" | "hsba";
|
|
19
|
+
type ColorSpace = "rgb" | "hsl" | "hsb";
|
|
20
|
+
export type ColorChannel =
|
|
21
|
+
| "hue"
|
|
22
|
+
| "saturation"
|
|
23
|
+
| "brightness"
|
|
24
|
+
| "lightness"
|
|
25
|
+
| "red"
|
|
26
|
+
| "green"
|
|
27
|
+
| "blue"
|
|
28
|
+
| "alpha";
|
|
29
|
+
interface ColorAxes {
|
|
30
|
+
xChannel: ColorChannel;
|
|
31
|
+
yChannel: ColorChannel;
|
|
32
|
+
zChannel: ColorChannel;
|
|
33
|
+
}
|
|
34
|
+
interface ColorChannelRange {
|
|
35
|
+
minValue: number;
|
|
36
|
+
maxValue: number;
|
|
37
|
+
step: number;
|
|
38
|
+
pageSize: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A color value; every method that changes it returns a new one. */
|
|
42
|
+
export interface Color {
|
|
43
|
+
toFormat(format: ColorFormat): Color;
|
|
44
|
+
toString(format?: ColorFormat | "css"): string;
|
|
45
|
+
clone(): Color;
|
|
46
|
+
toHexInt(): number;
|
|
47
|
+
/** Throws when the channel is not in the color's space. */
|
|
48
|
+
getChannelValue(channel: ColorChannel): number;
|
|
49
|
+
/** Throws when the channel is not in the color's space. */
|
|
50
|
+
withChannelValue(channel: ColorChannel, value: number): Color;
|
|
51
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
52
|
+
getChannelName(channel: ColorChannel, translations: ColorIntlTranslations): string;
|
|
53
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
|
|
54
|
+
formatChannelValue(channel: ColorChannel): string;
|
|
55
|
+
getColorSpace(): ColorSpace;
|
|
56
|
+
getColorSpaceAxes(xyChannels: { xChannel?: ColorChannel; yChannel?: ColorChannel }): ColorAxes;
|
|
57
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
58
|
+
getColorName(translations: ColorIntlTranslations): string;
|
|
59
|
+
getHueName(translations: ColorIntlTranslations): string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type HueName = keyof ColorIntlTranslations;
|
|
63
|
+
|
|
64
|
+
const clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max);
|
|
65
|
+
|
|
66
|
+
const format = (value: number, options: Intl.NumberFormatOptions) =>
|
|
67
|
+
new Intl.NumberFormat(locale, options).format(value);
|
|
68
|
+
|
|
69
|
+
/** Parses a color from a string value. Throws an error if the string could not be parsed. */
|
|
70
|
+
export function parseColor(value: string): Color {
|
|
71
|
+
const res = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value);
|
|
72
|
+
if (res) return res;
|
|
73
|
+
throw new Error(`Invalid color value: ${value}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function normalizeHue(hue: number): number {
|
|
77
|
+
if (hue === 360) return hue;
|
|
78
|
+
return ((hue % 360) + 360) % 360;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const ORANGE_LIGHTNESS_THRESHOLD = 0.68;
|
|
82
|
+
const YELLOW_GREEN_LIGHTNESS_THRESHOLD = 0.85;
|
|
83
|
+
const MAX_DARK_LIGHTNESS = 0.55;
|
|
84
|
+
const GRAY_THRESHOLD = 0.001;
|
|
85
|
+
|
|
86
|
+
const OKLCH_HUES: [number, HueName][] = [
|
|
87
|
+
[0, "pink"],
|
|
88
|
+
[15, "red"],
|
|
89
|
+
[48, "orange"],
|
|
90
|
+
[94, "yellow"],
|
|
91
|
+
[135, "green"],
|
|
92
|
+
[175, "cyan"],
|
|
93
|
+
[264, "blue"],
|
|
94
|
+
[284, "purple"],
|
|
95
|
+
[320, "magenta"],
|
|
96
|
+
[349, "pink"],
|
|
97
|
+
];
|
|
98
|
+
|
|
99
|
+
abstract class ColorBase implements Color {
|
|
100
|
+
abstract toFormat(format: ColorFormat): Color;
|
|
101
|
+
abstract toString(format?: ColorFormat | "css"): string;
|
|
102
|
+
abstract clone(): Color;
|
|
103
|
+
abstract getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
104
|
+
abstract getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
|
|
105
|
+
abstract formatChannelValue(channel: ColorChannel): string;
|
|
106
|
+
abstract getColorSpace(): ColorSpace;
|
|
107
|
+
abstract getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
108
|
+
|
|
109
|
+
toHexInt(): number {
|
|
110
|
+
return this.toFormat("rgb").toHexInt();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getChannelValue(channel: ColorChannel): number {
|
|
114
|
+
if (channel in this) return (this as unknown as Record<ColorChannel, number>)[channel];
|
|
115
|
+
throw new Error(`Unsupported color channel: ${channel}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
withChannelValue(channel: ColorChannel, value: number): Color {
|
|
119
|
+
if (channel in this) {
|
|
120
|
+
const x = this.clone();
|
|
121
|
+
(x as unknown as Record<ColorChannel, number>)[channel] = value;
|
|
122
|
+
return x;
|
|
123
|
+
}
|
|
124
|
+
throw new Error(`Unsupported color channel: ${channel}`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
getChannelName(channel: ColorChannel, translations: ColorIntlTranslations): string {
|
|
128
|
+
return translations[channel];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
getColorSpaceAxes(xyChannels: { xChannel?: ColorChannel; yChannel?: ColorChannel }): ColorAxes {
|
|
132
|
+
const { xChannel, yChannel } = xyChannels;
|
|
133
|
+
const channels = this.getColorChannels();
|
|
134
|
+
const xCh = xChannel || channels.find((c) => c !== yChannel);
|
|
135
|
+
const yCh = yChannel || channels.find((c) => c !== xCh);
|
|
136
|
+
const zCh = channels.find((c) => c !== xCh && c !== yCh);
|
|
137
|
+
return { xChannel: xCh, yChannel: yCh, zChannel: zCh } as ColorAxes;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
getColorName(translations: ColorIntlTranslations): string {
|
|
141
|
+
let [l, c, h] = toOKLCH(this);
|
|
142
|
+
if (l > 0.999) return translations.white;
|
|
143
|
+
if (l < 0.001) return translations.black;
|
|
144
|
+
let hue: string;
|
|
145
|
+
[hue, l] = this.getOklchHue(l, c, h, translations);
|
|
146
|
+
let lightness: "" | HueName = "";
|
|
147
|
+
let chroma: "" | HueName = "";
|
|
148
|
+
if (c <= 0.1 && c >= GRAY_THRESHOLD) {
|
|
149
|
+
chroma = l >= 0.7 ? "pale" : "grayish";
|
|
150
|
+
} else if (c >= 0.15) {
|
|
151
|
+
chroma = "vibrant";
|
|
152
|
+
}
|
|
153
|
+
if (l < 0.3) lightness = "very dark";
|
|
154
|
+
else if (l < MAX_DARK_LIGHTNESS) lightness = "dark";
|
|
155
|
+
else if (l < 0.7) lightness = "";
|
|
156
|
+
else if (l < 0.85) lightness = "light";
|
|
157
|
+
else lightness = "very light";
|
|
158
|
+
const chromaText = chroma ? (translations[chroma] as string) : "";
|
|
159
|
+
const lightnessText = lightness ? (translations[lightness] as string) : "";
|
|
160
|
+
const alpha = this.getChannelValue("alpha");
|
|
161
|
+
if (alpha < 1) {
|
|
162
|
+
const percentTransparent = format(1 - alpha, { style: "percent" });
|
|
163
|
+
return translations
|
|
164
|
+
.transparentColorName(lightnessText, chromaText, hue, percentTransparent)
|
|
165
|
+
.replace(/\s+/g, " ")
|
|
166
|
+
.trim();
|
|
167
|
+
}
|
|
168
|
+
return translations.colorName(lightnessText, chromaText, hue).replace(/\s+/g, " ").trim();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
getOklchHue(
|
|
172
|
+
lightness: number,
|
|
173
|
+
c: number,
|
|
174
|
+
h: number,
|
|
175
|
+
translations: ColorIntlTranslations,
|
|
176
|
+
): [string, number] {
|
|
177
|
+
let l = lightness;
|
|
178
|
+
if (c < GRAY_THRESHOLD) return [translations.gray, l];
|
|
179
|
+
for (let i = 0; i < OKLCH_HUES.length; i++) {
|
|
180
|
+
let [hue, hueName] = OKLCH_HUES[i] as [number, HueName];
|
|
181
|
+
const [nextHue, nextHueName] = OKLCH_HUES[i + 1] ?? [360, "pink"];
|
|
182
|
+
if (h >= hue && h < nextHue) {
|
|
183
|
+
if (hueName === "orange") {
|
|
184
|
+
if (l < ORANGE_LIGHTNESS_THRESHOLD) hueName = "brown";
|
|
185
|
+
else l = l - ORANGE_LIGHTNESS_THRESHOLD + MAX_DARK_LIGHTNESS;
|
|
186
|
+
}
|
|
187
|
+
if (h > hue + (nextHue - hue) / 2 && hueName !== nextHueName) {
|
|
188
|
+
hueName = `${hueName} ${nextHueName}` as HueName;
|
|
189
|
+
} else if (hueName === "yellow" && l < YELLOW_GREEN_LIGHTNESS_THRESHOLD) {
|
|
190
|
+
hueName = "yellow green";
|
|
191
|
+
}
|
|
192
|
+
return [translations[hueName] as string, l];
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
throw new Error("Unexpected hue");
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
getHueName(translations: ColorIntlTranslations): string {
|
|
199
|
+
const [l, c, h] = toOKLCH(this);
|
|
200
|
+
const [name] = this.getOklchHue(l, c, h, translations);
|
|
201
|
+
return name;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
class RGBColor extends ColorBase {
|
|
206
|
+
static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = ["red", "green", "blue"];
|
|
207
|
+
|
|
208
|
+
constructor(
|
|
209
|
+
public red: number,
|
|
210
|
+
public green: number,
|
|
211
|
+
public blue: number,
|
|
212
|
+
public alpha: number,
|
|
213
|
+
) {
|
|
214
|
+
super();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
static parse(value: string): RGBColor | undefined {
|
|
218
|
+
let colors: (number | undefined)[] = [];
|
|
219
|
+
if (/^#[\da-f]+$/i.test(value) && [4, 5, 7, 9].includes(value.length)) {
|
|
220
|
+
const values = (value.length < 6 ? value.replace(/[^#]/gi, "$&$&") : value)
|
|
221
|
+
.slice(1)
|
|
222
|
+
.split("");
|
|
223
|
+
while (values.length > 0) colors.push(Number.parseInt(values.splice(0, 2).join(""), 16));
|
|
224
|
+
colors[3] = colors[3] !== undefined ? colors[3] / 255 : undefined;
|
|
225
|
+
}
|
|
226
|
+
const match = value.match(/^rgba?\((.*)\)$/);
|
|
227
|
+
if (match?.[1]) {
|
|
228
|
+
colors = match[1]
|
|
229
|
+
.replace(/(\d+)%$/u, (_substring, numberValue: string) =>
|
|
230
|
+
(Number(numberValue) / 100).toString(),
|
|
231
|
+
)
|
|
232
|
+
.replaceAll(/,|\//gu, " ")
|
|
233
|
+
.replaceAll(/\s{2,}/gu, " ")
|
|
234
|
+
.split(" ")
|
|
235
|
+
.map((part) => Number(part.trim()));
|
|
236
|
+
colors = colors.map((num, i) => clamp(num ?? 0, 0, i < 3 ? 255 : 1));
|
|
237
|
+
}
|
|
238
|
+
const [red, green, blue, alpha] = colors;
|
|
239
|
+
if (red === undefined || green === undefined || blue === undefined) return undefined;
|
|
240
|
+
return colors.length < 3 ? undefined : new RGBColor(red, green, blue, alpha ?? 1);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
toString(format: ColorFormat | "css" = "css"): string {
|
|
244
|
+
const hex = (n: number) => n.toString(16).padStart(2, "0");
|
|
245
|
+
switch (format) {
|
|
246
|
+
case "hex":
|
|
247
|
+
return `#${(hex(this.red) + hex(this.green) + hex(this.blue)).toUpperCase()}`;
|
|
248
|
+
case "hexa":
|
|
249
|
+
return `#${(hex(this.red) + hex(this.green) + hex(this.blue) + hex(Math.round(this.alpha * 255))).toUpperCase()}`;
|
|
250
|
+
case "css":
|
|
251
|
+
case "rgb":
|
|
252
|
+
return `rgb(${this.red}, ${this.green}, ${this.blue}${this.alpha !== 1 && this.alpha !== 100 ? ` / ${this.alpha}` : ""})`;
|
|
253
|
+
case "rgba":
|
|
254
|
+
return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
|
|
255
|
+
default:
|
|
256
|
+
return this.toFormat(format).toString(format);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
toFormat(format: ColorFormat): Color {
|
|
261
|
+
switch (format) {
|
|
262
|
+
case "hex":
|
|
263
|
+
case "hexa":
|
|
264
|
+
case "rgb":
|
|
265
|
+
case "rgba":
|
|
266
|
+
return this;
|
|
267
|
+
case "hsb":
|
|
268
|
+
case "hsba":
|
|
269
|
+
return this.toHSB();
|
|
270
|
+
case "hsl":
|
|
271
|
+
case "hsla":
|
|
272
|
+
return this.toHSL();
|
|
273
|
+
default:
|
|
274
|
+
throw new Error(`Unsupported color conversion: rgb -> ${format as string}`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
override toHexInt(): number {
|
|
279
|
+
return (this.red << 16) | (this.green << 8) | this.blue;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB */
|
|
283
|
+
private toHSB(): Color {
|
|
284
|
+
const red = this.red / 255;
|
|
285
|
+
const green = this.green / 255;
|
|
286
|
+
const blue = this.blue / 255;
|
|
287
|
+
const min = Math.min(red, green, blue);
|
|
288
|
+
const brightness = Math.max(red, green, blue);
|
|
289
|
+
const chroma = brightness - min;
|
|
290
|
+
const saturation = brightness === 0 ? 0 : chroma / brightness;
|
|
291
|
+
let hue = 0;
|
|
292
|
+
if (chroma !== 0) {
|
|
293
|
+
switch (brightness) {
|
|
294
|
+
case red:
|
|
295
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
296
|
+
break;
|
|
297
|
+
case green:
|
|
298
|
+
hue = (blue - red) / chroma + 2;
|
|
299
|
+
break;
|
|
300
|
+
case blue:
|
|
301
|
+
hue = (red - green) / chroma + 4;
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
hue /= 6;
|
|
305
|
+
}
|
|
306
|
+
return new HSBColor(
|
|
307
|
+
toFixedNumber(hue * 360, 2),
|
|
308
|
+
toFixedNumber(saturation * 100, 2),
|
|
309
|
+
toFixedNumber(brightness * 100, 2),
|
|
310
|
+
this.alpha,
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB */
|
|
315
|
+
private toHSL(): Color {
|
|
316
|
+
const red = this.red / 255;
|
|
317
|
+
const green = this.green / 255;
|
|
318
|
+
const blue = this.blue / 255;
|
|
319
|
+
const min = Math.min(red, green, blue);
|
|
320
|
+
const max = Math.max(red, green, blue);
|
|
321
|
+
const lightness = (max + min) / 2;
|
|
322
|
+
const chroma = max - min;
|
|
323
|
+
let hue: number;
|
|
324
|
+
let saturation: number;
|
|
325
|
+
if (chroma === 0) {
|
|
326
|
+
hue = saturation = 0;
|
|
327
|
+
} else {
|
|
328
|
+
saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min);
|
|
329
|
+
switch (max) {
|
|
330
|
+
case red:
|
|
331
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
332
|
+
break;
|
|
333
|
+
case green:
|
|
334
|
+
hue = (blue - red) / chroma + 2;
|
|
335
|
+
break;
|
|
336
|
+
default:
|
|
337
|
+
hue = (red - green) / chroma + 4;
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
hue /= 6;
|
|
341
|
+
}
|
|
342
|
+
return new HSLColor(
|
|
343
|
+
toFixedNumber(hue * 360, 2),
|
|
344
|
+
toFixedNumber(saturation * 100, 2),
|
|
345
|
+
toFixedNumber(lightness * 100, 2),
|
|
346
|
+
this.alpha,
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
clone(): Color {
|
|
351
|
+
return new RGBColor(this.red, this.green, this.blue, this.alpha);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange {
|
|
355
|
+
switch (channel) {
|
|
356
|
+
case "red":
|
|
357
|
+
case "green":
|
|
358
|
+
case "blue":
|
|
359
|
+
return { minValue: 0, maxValue: 255, step: 1, pageSize: 17 };
|
|
360
|
+
case "alpha":
|
|
361
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
362
|
+
default:
|
|
363
|
+
throw new Error(`Unknown color channel: ${channel}`);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions {
|
|
368
|
+
switch (channel) {
|
|
369
|
+
case "red":
|
|
370
|
+
case "green":
|
|
371
|
+
case "blue":
|
|
372
|
+
return { style: "decimal" };
|
|
373
|
+
case "alpha":
|
|
374
|
+
return { style: "percent", maximumFractionDigits: 2 };
|
|
375
|
+
default:
|
|
376
|
+
throw new Error(`Unknown color channel: ${channel}`);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
formatChannelValue(channel: ColorChannel): string {
|
|
381
|
+
const options = this.getChannelFormatOptions(channel);
|
|
382
|
+
return format(this.getChannelValue(channel), options);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
getColorSpace(): ColorSpace {
|
|
386
|
+
return "rgb";
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
390
|
+
return RGBColor.colorChannels;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const HSB_REGEX =
|
|
395
|
+
/hsb\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%)\)|hsba\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d(.\d+)?)\)/;
|
|
396
|
+
|
|
397
|
+
/** The numbers of an `hsb(...)` or `hsl(...)` match: three channels and an optional alpha. */
|
|
398
|
+
function channelsOf(m: RegExpMatchArray): [number, number, number, number | undefined] {
|
|
399
|
+
const [a = 0, b = 0, c = 0, d] = (m[1] ?? m[2] ?? "")
|
|
400
|
+
.split(",")
|
|
401
|
+
.map((n) => Number(n.trim().replace("%", "")));
|
|
402
|
+
return [a, b, c, d];
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
class HSBColor extends ColorBase {
|
|
406
|
+
static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = [
|
|
407
|
+
"hue",
|
|
408
|
+
"saturation",
|
|
409
|
+
"brightness",
|
|
410
|
+
];
|
|
411
|
+
|
|
412
|
+
constructor(
|
|
413
|
+
public hue: number,
|
|
414
|
+
public saturation: number,
|
|
415
|
+
public brightness: number,
|
|
416
|
+
public alpha: number,
|
|
417
|
+
) {
|
|
418
|
+
super();
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
static parse(value: string): HSBColor | undefined {
|
|
422
|
+
const m = value.match(HSB_REGEX);
|
|
423
|
+
if (!m) return undefined;
|
|
424
|
+
const [h, s, b, a] = channelsOf(m);
|
|
425
|
+
return new HSBColor(normalizeHue(h), clamp(s, 0, 100), clamp(b, 0, 100), clamp(a ?? 1, 0, 1));
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
toString(format: ColorFormat | "css" = "css"): string {
|
|
429
|
+
switch (format) {
|
|
430
|
+
case "css":
|
|
431
|
+
return this.toHSL().toString("css");
|
|
432
|
+
case "hex":
|
|
433
|
+
return this.toRGB().toString("hex");
|
|
434
|
+
case "hexa":
|
|
435
|
+
return this.toRGB().toString("hexa");
|
|
436
|
+
case "hsb":
|
|
437
|
+
return `hsb(${this.hue} ${toFixedNumber(this.saturation, 2)}% ${toFixedNumber(this.brightness, 2)}%)`;
|
|
438
|
+
case "hsba":
|
|
439
|
+
return `hsba(${this.hue} ${toFixedNumber(this.saturation, 2)}% ${toFixedNumber(this.brightness, 2)}% ${this.alpha})`;
|
|
440
|
+
default:
|
|
441
|
+
return this.toFormat(format).toString(format);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
toFormat(format: ColorFormat): Color {
|
|
446
|
+
switch (format) {
|
|
447
|
+
case "hsb":
|
|
448
|
+
case "hsba":
|
|
449
|
+
return this;
|
|
450
|
+
case "hsl":
|
|
451
|
+
case "hsla":
|
|
452
|
+
return this.toHSL();
|
|
453
|
+
case "rgb":
|
|
454
|
+
case "rgba":
|
|
455
|
+
return this.toRGB();
|
|
456
|
+
default:
|
|
457
|
+
throw new Error(`Unsupported color conversion: hsb -> ${format}`);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/** https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL */
|
|
462
|
+
private toHSL(): Color {
|
|
463
|
+
let saturation = this.saturation / 100;
|
|
464
|
+
const brightness = this.brightness / 100;
|
|
465
|
+
const lightness = brightness * (1 - saturation / 2);
|
|
466
|
+
saturation =
|
|
467
|
+
lightness === 0 || lightness === 1
|
|
468
|
+
? 0
|
|
469
|
+
: (brightness - lightness) / Math.min(lightness, 1 - lightness);
|
|
470
|
+
return new HSLColor(
|
|
471
|
+
toFixedNumber(this.hue, 2),
|
|
472
|
+
toFixedNumber(saturation * 100, 2),
|
|
473
|
+
toFixedNumber(lightness * 100, 2),
|
|
474
|
+
this.alpha,
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative */
|
|
479
|
+
private toRGB(): Color {
|
|
480
|
+
const hue = this.hue;
|
|
481
|
+
const saturation = this.saturation / 100;
|
|
482
|
+
const brightness = this.brightness / 100;
|
|
483
|
+
const fn = (n: number, k = (n + hue / 60) % 6) =>
|
|
484
|
+
brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
485
|
+
return new RGBColor(
|
|
486
|
+
Math.round(fn(5) * 255),
|
|
487
|
+
Math.round(fn(3) * 255),
|
|
488
|
+
Math.round(fn(1) * 255),
|
|
489
|
+
this.alpha,
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
clone(): Color {
|
|
494
|
+
return new HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange {
|
|
498
|
+
switch (channel) {
|
|
499
|
+
case "hue":
|
|
500
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
501
|
+
case "saturation":
|
|
502
|
+
case "brightness":
|
|
503
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
504
|
+
case "alpha":
|
|
505
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
506
|
+
default:
|
|
507
|
+
throw new Error(`Unknown color channel: ${channel}`);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions {
|
|
512
|
+
switch (channel) {
|
|
513
|
+
case "hue":
|
|
514
|
+
return { style: "unit", unit: "degree", unitDisplay: "narrow" };
|
|
515
|
+
case "saturation":
|
|
516
|
+
case "brightness":
|
|
517
|
+
case "alpha":
|
|
518
|
+
return { style: "percent", maximumFractionDigits: 2 };
|
|
519
|
+
default:
|
|
520
|
+
throw new Error(`Unknown color channel: ${channel}`);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
formatChannelValue(channel: ColorChannel): string {
|
|
525
|
+
const options = this.getChannelFormatOptions(channel);
|
|
526
|
+
let value = this.getChannelValue(channel);
|
|
527
|
+
if (channel === "saturation" || channel === "brightness") value /= 100;
|
|
528
|
+
return format(value, options);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
getColorSpace(): ColorSpace {
|
|
532
|
+
return "hsb";
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
536
|
+
return HSBColor.colorChannels;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const HSL_REGEX =
|
|
541
|
+
/hsl\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%)\)|hsla\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d(.\d+)?)\)/;
|
|
542
|
+
|
|
543
|
+
class HSLColor extends ColorBase {
|
|
544
|
+
static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = [
|
|
545
|
+
"hue",
|
|
546
|
+
"saturation",
|
|
547
|
+
"lightness",
|
|
548
|
+
];
|
|
549
|
+
|
|
550
|
+
constructor(
|
|
551
|
+
public hue: number,
|
|
552
|
+
public saturation: number,
|
|
553
|
+
public lightness: number,
|
|
554
|
+
public alpha: number,
|
|
555
|
+
) {
|
|
556
|
+
super();
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
static parse(value: string): HSLColor | undefined {
|
|
560
|
+
const m = value.match(HSL_REGEX);
|
|
561
|
+
if (!m) return undefined;
|
|
562
|
+
const [h, s, l, a] = channelsOf(m);
|
|
563
|
+
return new HSLColor(normalizeHue(h), clamp(s, 0, 100), clamp(l, 0, 100), clamp(a ?? 1, 0, 1));
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
toString(format: ColorFormat | "css" = "css"): string {
|
|
567
|
+
switch (format) {
|
|
568
|
+
case "hex":
|
|
569
|
+
return this.toRGB().toString("hex");
|
|
570
|
+
case "hexa":
|
|
571
|
+
return this.toRGB().toString("hexa");
|
|
572
|
+
case "hsl":
|
|
573
|
+
return `hsl(${this.hue} ${toFixedNumber(this.saturation, 2)}% ${toFixedNumber(this.lightness, 2)}%${this.alpha !== 1 && this.alpha !== 100 ? ` / ${this.alpha}` : ""})`;
|
|
574
|
+
case "css":
|
|
575
|
+
case "hsla":
|
|
576
|
+
return `hsla(${this.hue} ${toFixedNumber(this.saturation, 2)}% ${toFixedNumber(this.lightness, 2)}% / ${this.alpha})`;
|
|
577
|
+
default:
|
|
578
|
+
return this.toFormat(format).toString(format);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
toFormat(format: ColorFormat): Color {
|
|
583
|
+
switch (format) {
|
|
584
|
+
case "hsl":
|
|
585
|
+
case "hsla":
|
|
586
|
+
return this;
|
|
587
|
+
case "hsb":
|
|
588
|
+
case "hsba":
|
|
589
|
+
return this.toHSB();
|
|
590
|
+
case "rgb":
|
|
591
|
+
case "rgba":
|
|
592
|
+
return this.toRGB();
|
|
593
|
+
default:
|
|
594
|
+
throw new Error(`Unsupported color conversion: hsl -> ${format}`);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV */
|
|
599
|
+
private toHSB(): Color {
|
|
600
|
+
let saturation = this.saturation / 100;
|
|
601
|
+
const lightness = this.lightness / 100;
|
|
602
|
+
const brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
603
|
+
saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
|
|
604
|
+
return new HSBColor(
|
|
605
|
+
toFixedNumber(this.hue, 2),
|
|
606
|
+
toFixedNumber(saturation * 100, 2),
|
|
607
|
+
toFixedNumber(brightness * 100, 2),
|
|
608
|
+
this.alpha,
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/** https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative */
|
|
613
|
+
private toRGB(): Color {
|
|
614
|
+
const hue = this.hue;
|
|
615
|
+
const saturation = this.saturation / 100;
|
|
616
|
+
const lightness = this.lightness / 100;
|
|
617
|
+
const a = saturation * Math.min(lightness, 1 - lightness);
|
|
618
|
+
const fn = (n: number, k = (n + hue / 30) % 12) =>
|
|
619
|
+
lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
620
|
+
return new RGBColor(
|
|
621
|
+
Math.round(fn(0) * 255),
|
|
622
|
+
Math.round(fn(8) * 255),
|
|
623
|
+
Math.round(fn(4) * 255),
|
|
624
|
+
this.alpha,
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
clone(): Color {
|
|
629
|
+
return new HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange {
|
|
633
|
+
switch (channel) {
|
|
634
|
+
case "hue":
|
|
635
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
636
|
+
case "saturation":
|
|
637
|
+
case "lightness":
|
|
638
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
639
|
+
case "alpha":
|
|
640
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
641
|
+
default:
|
|
642
|
+
throw new Error(`Unknown color channel: ${channel}`);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions {
|
|
647
|
+
switch (channel) {
|
|
648
|
+
case "hue":
|
|
649
|
+
return { style: "unit", unit: "degree", unitDisplay: "narrow" };
|
|
650
|
+
case "saturation":
|
|
651
|
+
case "lightness":
|
|
652
|
+
case "alpha":
|
|
653
|
+
return { style: "percent", maximumFractionDigits: 2 };
|
|
654
|
+
default:
|
|
655
|
+
throw new Error(`Unknown color channel: ${channel}`);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
formatChannelValue(channel: ColorChannel): string {
|
|
660
|
+
const options = this.getChannelFormatOptions(channel);
|
|
661
|
+
let value = this.getChannelValue(channel);
|
|
662
|
+
if (channel === "saturation" || channel === "lightness") value /= 100;
|
|
663
|
+
return format(value, options);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
getColorSpace(): ColorSpace {
|
|
667
|
+
return "hsl";
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
671
|
+
return HSLColor.colorChannels;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function toOKLCH(color: Color): [number, number, number] {
|
|
676
|
+
const rgb = color.toFormat("rgb");
|
|
677
|
+
const [red, green, blue] = linearSRGB(
|
|
678
|
+
rgb.getChannelValue("red") / 255,
|
|
679
|
+
rgb.getChannelValue("green") / 255,
|
|
680
|
+
rgb.getChannelValue("blue") / 255,
|
|
681
|
+
);
|
|
682
|
+
const [x, y, z] = linearSRGBToXYZ(red, green, blue);
|
|
683
|
+
const [l, a, b] = xyzToOKLab(x, y, z);
|
|
684
|
+
return okLabToOKLCH(l, a, b);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
function okLabToOKLCH(l: number, a: number, b: number): [number, number, number] {
|
|
688
|
+
const hue = (Math.atan2(b, a) * 180) / Math.PI;
|
|
689
|
+
return [l, Math.sqrt(a ** 2 + b ** 2), hue >= 0 ? hue : hue + 360];
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
function linearSRGB(r: number, g: number, b: number): [number, number, number] {
|
|
693
|
+
return [linearComponent(r), linearComponent(g), linearComponent(b)];
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
function linearComponent(val: number): number {
|
|
697
|
+
const sign = val < 0 ? -1 : 1;
|
|
698
|
+
const abs = Math.abs(val);
|
|
699
|
+
if (abs <= 0.04045) return val / 12.92;
|
|
700
|
+
return sign * ((abs + 0.055) / 1.055) ** 2.4;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function linearSRGBToXYZ(r: number, g: number, b: number): [number, number, number] {
|
|
704
|
+
const M = [
|
|
705
|
+
506752 / 1228815,
|
|
706
|
+
87881 / 245763,
|
|
707
|
+
12673 / 70218,
|
|
708
|
+
87098 / 409605,
|
|
709
|
+
175762 / 245763,
|
|
710
|
+
12673 / 175545,
|
|
711
|
+
7918 / 409605,
|
|
712
|
+
87881 / 737289,
|
|
713
|
+
1001167 / 1053270,
|
|
714
|
+
] as const;
|
|
715
|
+
return multiplyMatrix(M, r, g, b);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
function xyzToOKLab(x: number, y: number, z: number): [number, number, number] {
|
|
719
|
+
const XYZtoLMS = [
|
|
720
|
+
0.819022437996703, 0.3619062600528904, -0.1288737815209879, 0.0329836539323885,
|
|
721
|
+
0.9292868615863434, 0.0361446663506424, 0.0481771893596242, 0.2642395317527308,
|
|
722
|
+
0.6335478284694309,
|
|
723
|
+
] as const;
|
|
724
|
+
const LMStoOKLab = [
|
|
725
|
+
0.210454268309314, 0.7936177747023054, -0.0040720430116193, 1.9779985324311684,
|
|
726
|
+
-2.42859224204858, 0.450593709617411, 0.0259040424655478, 0.7827717124575296,
|
|
727
|
+
-0.8086757549230774,
|
|
728
|
+
] as const;
|
|
729
|
+
const [a, b, c] = multiplyMatrix(XYZtoLMS, x, y, z);
|
|
730
|
+
return multiplyMatrix(LMStoOKLab, Math.cbrt(a), Math.cbrt(b), Math.cbrt(c));
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
type Matrix = readonly [number, number, number, number, number, number, number, number, number];
|
|
734
|
+
|
|
735
|
+
function multiplyMatrix(m: Matrix, x: number, y: number, z: number): [number, number, number] {
|
|
736
|
+
return [
|
|
737
|
+
m[0] * x + m[1] * y + m[2] * z,
|
|
738
|
+
m[3] * x + m[4] * y + m[5] * z,
|
|
739
|
+
m[6] * x + m[7] * y + m[8] * z,
|
|
740
|
+
];
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
function toFixedNumber(value: number, digits: number, base = 10): number {
|
|
744
|
+
const pow = base ** digits;
|
|
745
|
+
return Math.round(value * pow) / pow;
|
|
746
|
+
}
|