@react-stately/color 3.9.4 → 3.10.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.
Files changed (233) hide show
  1. package/dist/import.mjs +7 -9
  2. package/dist/main.js +14 -16
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +7 -9
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +12 -0
  7. package/package.json +17 -20
  8. package/src/index.ts +12 -16
  9. package/dist/Color.main.js +0 -766
  10. package/dist/Color.main.js.map +0 -1
  11. package/dist/Color.mjs +0 -759
  12. package/dist/Color.module.js +0 -759
  13. package/dist/Color.module.js.map +0 -1
  14. package/dist/ar-AE.main.js +0 -42
  15. package/dist/ar-AE.main.js.map +0 -1
  16. package/dist/ar-AE.mjs +0 -44
  17. package/dist/ar-AE.module.js +0 -44
  18. package/dist/ar-AE.module.js.map +0 -1
  19. package/dist/bg-BG.main.js +0 -42
  20. package/dist/bg-BG.main.js.map +0 -1
  21. package/dist/bg-BG.mjs +0 -44
  22. package/dist/bg-BG.module.js +0 -44
  23. package/dist/bg-BG.module.js.map +0 -1
  24. package/dist/cs-CZ.main.js +0 -42
  25. package/dist/cs-CZ.main.js.map +0 -1
  26. package/dist/cs-CZ.mjs +0 -44
  27. package/dist/cs-CZ.module.js +0 -44
  28. package/dist/cs-CZ.module.js.map +0 -1
  29. package/dist/da-DK.main.js +0 -42
  30. package/dist/da-DK.main.js.map +0 -1
  31. package/dist/da-DK.mjs +0 -44
  32. package/dist/da-DK.module.js +0 -44
  33. package/dist/da-DK.module.js.map +0 -1
  34. package/dist/de-DE.main.js +0 -42
  35. package/dist/de-DE.main.js.map +0 -1
  36. package/dist/de-DE.mjs +0 -44
  37. package/dist/de-DE.module.js +0 -44
  38. package/dist/de-DE.module.js.map +0 -1
  39. package/dist/el-GR.main.js +0 -42
  40. package/dist/el-GR.main.js.map +0 -1
  41. package/dist/el-GR.mjs +0 -44
  42. package/dist/el-GR.module.js +0 -44
  43. package/dist/el-GR.module.js.map +0 -1
  44. package/dist/en-US.main.js +0 -42
  45. package/dist/en-US.main.js.map +0 -1
  46. package/dist/en-US.mjs +0 -44
  47. package/dist/en-US.module.js +0 -44
  48. package/dist/en-US.module.js.map +0 -1
  49. package/dist/es-ES.main.js +0 -42
  50. package/dist/es-ES.main.js.map +0 -1
  51. package/dist/es-ES.mjs +0 -44
  52. package/dist/es-ES.module.js +0 -44
  53. package/dist/es-ES.module.js.map +0 -1
  54. package/dist/et-EE.main.js +0 -42
  55. package/dist/et-EE.main.js.map +0 -1
  56. package/dist/et-EE.mjs +0 -44
  57. package/dist/et-EE.module.js +0 -44
  58. package/dist/et-EE.module.js.map +0 -1
  59. package/dist/fi-FI.main.js +0 -42
  60. package/dist/fi-FI.main.js.map +0 -1
  61. package/dist/fi-FI.mjs +0 -44
  62. package/dist/fi-FI.module.js +0 -44
  63. package/dist/fi-FI.module.js.map +0 -1
  64. package/dist/fr-FR.main.js +0 -42
  65. package/dist/fr-FR.main.js.map +0 -1
  66. package/dist/fr-FR.mjs +0 -44
  67. package/dist/fr-FR.module.js +0 -44
  68. package/dist/fr-FR.module.js.map +0 -1
  69. package/dist/he-IL.main.js +0 -42
  70. package/dist/he-IL.main.js.map +0 -1
  71. package/dist/he-IL.mjs +0 -44
  72. package/dist/he-IL.module.js +0 -44
  73. package/dist/he-IL.module.js.map +0 -1
  74. package/dist/hr-HR.main.js +0 -42
  75. package/dist/hr-HR.main.js.map +0 -1
  76. package/dist/hr-HR.mjs +0 -44
  77. package/dist/hr-HR.module.js +0 -44
  78. package/dist/hr-HR.module.js.map +0 -1
  79. package/dist/hu-HU.main.js +0 -42
  80. package/dist/hu-HU.main.js.map +0 -1
  81. package/dist/hu-HU.mjs +0 -44
  82. package/dist/hu-HU.module.js +0 -44
  83. package/dist/hu-HU.module.js.map +0 -1
  84. package/dist/intlStrings.main.js +0 -108
  85. package/dist/intlStrings.main.js.map +0 -1
  86. package/dist/intlStrings.mjs +0 -110
  87. package/dist/intlStrings.module.js +0 -110
  88. package/dist/intlStrings.module.js.map +0 -1
  89. package/dist/it-IT.main.js +0 -42
  90. package/dist/it-IT.main.js.map +0 -1
  91. package/dist/it-IT.mjs +0 -44
  92. package/dist/it-IT.module.js +0 -44
  93. package/dist/it-IT.module.js.map +0 -1
  94. package/dist/ja-JP.main.js +0 -42
  95. package/dist/ja-JP.main.js.map +0 -1
  96. package/dist/ja-JP.mjs +0 -44
  97. package/dist/ja-JP.module.js +0 -44
  98. package/dist/ja-JP.module.js.map +0 -1
  99. package/dist/ko-KR.main.js +0 -42
  100. package/dist/ko-KR.main.js.map +0 -1
  101. package/dist/ko-KR.mjs +0 -44
  102. package/dist/ko-KR.module.js +0 -44
  103. package/dist/ko-KR.module.js.map +0 -1
  104. package/dist/lt-LT.main.js +0 -42
  105. package/dist/lt-LT.main.js.map +0 -1
  106. package/dist/lt-LT.mjs +0 -44
  107. package/dist/lt-LT.module.js +0 -44
  108. package/dist/lt-LT.module.js.map +0 -1
  109. package/dist/lv-LV.main.js +0 -42
  110. package/dist/lv-LV.main.js.map +0 -1
  111. package/dist/lv-LV.mjs +0 -44
  112. package/dist/lv-LV.module.js +0 -44
  113. package/dist/lv-LV.module.js.map +0 -1
  114. package/dist/nb-NO.main.js +0 -42
  115. package/dist/nb-NO.main.js.map +0 -1
  116. package/dist/nb-NO.mjs +0 -44
  117. package/dist/nb-NO.module.js +0 -44
  118. package/dist/nb-NO.module.js.map +0 -1
  119. package/dist/nl-NL.main.js +0 -42
  120. package/dist/nl-NL.main.js.map +0 -1
  121. package/dist/nl-NL.mjs +0 -44
  122. package/dist/nl-NL.module.js +0 -44
  123. package/dist/nl-NL.module.js.map +0 -1
  124. package/dist/pl-PL.main.js +0 -42
  125. package/dist/pl-PL.main.js.map +0 -1
  126. package/dist/pl-PL.mjs +0 -44
  127. package/dist/pl-PL.module.js +0 -44
  128. package/dist/pl-PL.module.js.map +0 -1
  129. package/dist/pt-BR.main.js +0 -42
  130. package/dist/pt-BR.main.js.map +0 -1
  131. package/dist/pt-BR.mjs +0 -44
  132. package/dist/pt-BR.module.js +0 -44
  133. package/dist/pt-BR.module.js.map +0 -1
  134. package/dist/pt-PT.main.js +0 -42
  135. package/dist/pt-PT.main.js.map +0 -1
  136. package/dist/pt-PT.mjs +0 -44
  137. package/dist/pt-PT.module.js +0 -44
  138. package/dist/pt-PT.module.js.map +0 -1
  139. package/dist/ro-RO.main.js +0 -42
  140. package/dist/ro-RO.main.js.map +0 -1
  141. package/dist/ro-RO.mjs +0 -44
  142. package/dist/ro-RO.module.js +0 -44
  143. package/dist/ro-RO.module.js.map +0 -1
  144. package/dist/ru-RU.main.js +0 -42
  145. package/dist/ru-RU.main.js.map +0 -1
  146. package/dist/ru-RU.mjs +0 -44
  147. package/dist/ru-RU.module.js +0 -44
  148. package/dist/ru-RU.module.js.map +0 -1
  149. package/dist/sk-SK.main.js +0 -42
  150. package/dist/sk-SK.main.js.map +0 -1
  151. package/dist/sk-SK.mjs +0 -44
  152. package/dist/sk-SK.module.js +0 -44
  153. package/dist/sk-SK.module.js.map +0 -1
  154. package/dist/sl-SI.main.js +0 -42
  155. package/dist/sl-SI.main.js.map +0 -1
  156. package/dist/sl-SI.mjs +0 -44
  157. package/dist/sl-SI.module.js +0 -44
  158. package/dist/sl-SI.module.js.map +0 -1
  159. package/dist/sr-SP.main.js +0 -42
  160. package/dist/sr-SP.main.js.map +0 -1
  161. package/dist/sr-SP.mjs +0 -44
  162. package/dist/sr-SP.module.js +0 -44
  163. package/dist/sr-SP.module.js.map +0 -1
  164. package/dist/sv-SE.main.js +0 -42
  165. package/dist/sv-SE.main.js.map +0 -1
  166. package/dist/sv-SE.mjs +0 -44
  167. package/dist/sv-SE.module.js +0 -44
  168. package/dist/sv-SE.module.js.map +0 -1
  169. package/dist/tr-TR.main.js +0 -42
  170. package/dist/tr-TR.main.js.map +0 -1
  171. package/dist/tr-TR.mjs +0 -44
  172. package/dist/tr-TR.module.js +0 -44
  173. package/dist/tr-TR.module.js.map +0 -1
  174. package/dist/types.d.ts +0 -198
  175. package/dist/types.d.ts.map +0 -1
  176. package/dist/uk-UA.main.js +0 -42
  177. package/dist/uk-UA.main.js.map +0 -1
  178. package/dist/uk-UA.mjs +0 -44
  179. package/dist/uk-UA.module.js +0 -44
  180. package/dist/uk-UA.module.js.map +0 -1
  181. package/dist/useColor.main.js +0 -36
  182. package/dist/useColor.main.js.map +0 -1
  183. package/dist/useColor.mjs +0 -31
  184. package/dist/useColor.module.js +0 -31
  185. package/dist/useColor.module.js.map +0 -1
  186. package/dist/useColorAreaState.main.js +0 -133
  187. package/dist/useColorAreaState.main.js.map +0 -1
  188. package/dist/useColorAreaState.mjs +0 -128
  189. package/dist/useColorAreaState.module.js +0 -128
  190. package/dist/useColorAreaState.module.js.map +0 -1
  191. package/dist/useColorChannelFieldState.main.js +0 -65
  192. package/dist/useColorChannelFieldState.main.js.map +0 -1
  193. package/dist/useColorChannelFieldState.mjs +0 -60
  194. package/dist/useColorChannelFieldState.module.js +0 -60
  195. package/dist/useColorChannelFieldState.module.js.map +0 -1
  196. package/dist/useColorFieldState.main.js +0 -142
  197. package/dist/useColorFieldState.main.js.map +0 -1
  198. package/dist/useColorFieldState.mjs +0 -137
  199. package/dist/useColorFieldState.module.js +0 -137
  200. package/dist/useColorFieldState.module.js.map +0 -1
  201. package/dist/useColorPickerState.main.js +0 -27
  202. package/dist/useColorPickerState.main.js.map +0 -1
  203. package/dist/useColorPickerState.mjs +0 -22
  204. package/dist/useColorPickerState.module.js +0 -22
  205. package/dist/useColorPickerState.module.js.map +0 -1
  206. package/dist/useColorSliderState.main.js +0 -93
  207. package/dist/useColorSliderState.main.js.map +0 -1
  208. package/dist/useColorSliderState.mjs +0 -88
  209. package/dist/useColorSliderState.module.js +0 -88
  210. package/dist/useColorSliderState.module.js.map +0 -1
  211. package/dist/useColorWheelState.main.js +0 -135
  212. package/dist/useColorWheelState.main.js.map +0 -1
  213. package/dist/useColorWheelState.mjs +0 -130
  214. package/dist/useColorWheelState.module.js +0 -130
  215. package/dist/useColorWheelState.module.js.map +0 -1
  216. package/dist/zh-CN.main.js +0 -42
  217. package/dist/zh-CN.main.js.map +0 -1
  218. package/dist/zh-CN.mjs +0 -44
  219. package/dist/zh-CN.module.js +0 -44
  220. package/dist/zh-CN.module.js.map +0 -1
  221. package/dist/zh-TW.main.js +0 -42
  222. package/dist/zh-TW.main.js.map +0 -1
  223. package/dist/zh-TW.mjs +0 -44
  224. package/dist/zh-TW.module.js +0 -44
  225. package/dist/zh-TW.module.js.map +0 -1
  226. package/src/Color.ts +0 -792
  227. package/src/useColor.ts +0 -28
  228. package/src/useColorAreaState.ts +0 -202
  229. package/src/useColorChannelFieldState.ts +0 -74
  230. package/src/useColorFieldState.ts +0 -197
  231. package/src/useColorPickerState.ts +0 -29
  232. package/src/useColorSliderState.ts +0 -109
  233. package/src/useColorWheelState.ts +0 -196
package/src/Color.ts DELETED
@@ -1,792 +0,0 @@
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
-
13
- import {clamp, toFixedNumber} from '@react-stately/utils';
14
- import {ColorAxes, ColorChannel, ColorChannelRange, ColorFormat, ColorSpace, Color as IColor} from '@react-types/color';
15
- // @ts-ignore
16
- import intlMessages from '../intl/*.json';
17
- import {LocalizedStringDictionary, LocalizedStringFormatter} from '@internationalized/string';
18
- import {NumberFormatter} from '@internationalized/number';
19
-
20
- let dictionary = new LocalizedStringDictionary(intlMessages);
21
-
22
- /** Parses a color from a string value. Throws an error if the string could not be parsed. */
23
- export function parseColor(value: string): IColor {
24
- let res = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value);
25
- if (res) {
26
- return res;
27
- }
28
-
29
- throw new Error('Invalid color value: ' + value);
30
- }
31
-
32
- export function normalizeColor(v: string | IColor): IColor {
33
- if (typeof v === 'string') {
34
- return parseColor(v);
35
- } else {
36
- return v;
37
- }
38
- }
39
-
40
- /** Returns a list of color channels for a given color space. */
41
- export function getColorChannels(colorSpace: ColorSpace): [ColorChannel, ColorChannel, ColorChannel] {
42
- switch (colorSpace) {
43
- case 'rgb':
44
- return RGBColor.colorChannels;
45
- case 'hsl':
46
- return HSLColor.colorChannels;
47
- case 'hsb':
48
- return HSBColor.colorChannels;
49
- }
50
- }
51
-
52
- /**
53
- * Returns the hue value normalized to the range of 0 to 360.
54
- */
55
- export function normalizeHue(hue: number): number {
56
- if (hue === 360) {
57
- return hue;
58
- }
59
-
60
- return ((hue % 360) + 360) % 360;
61
- }
62
-
63
- // Lightness threshold between orange and brown.
64
- const ORANGE_LIGHTNESS_THRESHOLD = 0.68;
65
- // Lightness threshold between pure yellow and "yellow green".
66
- const YELLOW_GREEN_LIGHTNESS_THRESHOLD = 0.85;
67
- // The maximum lightness considered to be "dark".
68
- const MAX_DARK_LIGHTNESS = 0.55;
69
- // The chroma threshold between gray and color.
70
- const GRAY_THRESHOLD = 0.001;
71
- const OKLCH_HUES: [number, string][] = [
72
- [0, 'pink'],
73
- [15, 'red'],
74
- [48, 'orange'],
75
- [94, 'yellow'],
76
- [135, 'green'],
77
- [175, 'cyan'],
78
- [264, 'blue'],
79
- [284, 'purple'],
80
- [320, 'magenta'],
81
- [349, 'pink']
82
- ];
83
-
84
- abstract class Color implements IColor {
85
- abstract toFormat(format: ColorFormat): IColor;
86
- abstract toString(format: ColorFormat | 'css'): string;
87
- abstract clone(): IColor;
88
- abstract getChannelRange(channel: ColorChannel): ColorChannelRange;
89
- abstract getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
90
- abstract formatChannelValue(channel: ColorChannel, locale: string): string;
91
-
92
- toHexInt(): number {
93
- return this.toFormat('rgb').toHexInt();
94
- }
95
-
96
- getChannelValue(channel: ColorChannel): number {
97
- if (channel in this) {
98
- return this[channel];
99
- }
100
-
101
- throw new Error('Unsupported color channel: ' + channel);
102
- }
103
-
104
- withChannelValue(channel: ColorChannel, value: number): IColor {
105
- if (channel in this) {
106
- let x = this.clone();
107
- x[channel] = value;
108
- return x;
109
- }
110
-
111
- throw new Error('Unsupported color channel: ' + channel);
112
- }
113
-
114
- getChannelName(channel: ColorChannel, locale: string) {
115
- let strings = LocalizedStringDictionary.getGlobalDictionaryForPackage('@react-stately/color') || dictionary;
116
- return strings.getStringForLocale(channel, locale);
117
- }
118
-
119
- abstract getColorSpace(): ColorSpace;
120
-
121
- getColorSpaceAxes(xyChannels: {xChannel?: ColorChannel, yChannel?: ColorChannel}): ColorAxes {
122
- let {xChannel, yChannel} = xyChannels;
123
- let xCh = xChannel || this.getColorChannels().find(c => c !== yChannel)!;
124
- let yCh = yChannel || this.getColorChannels().find(c => c !== xCh)!;
125
- let zCh = this.getColorChannels().find(c => c !== xCh && c !== yCh)!;
126
-
127
- return {xChannel: xCh, yChannel: yCh, zChannel: zCh};
128
- }
129
-
130
- abstract getColorChannels(): [ColorChannel, ColorChannel, ColorChannel]
131
-
132
- getColorName(locale: string): string {
133
- // Convert to oklch color space, which has perceptually uniform lightness across all hues.
134
- let [l, c, h] = toOKLCH(this);
135
-
136
- let strings = LocalizedStringDictionary.getGlobalDictionaryForPackage('@react-stately/color') || dictionary;
137
- if (l > 0.999) {
138
- return strings.getStringForLocale('white', locale);
139
- }
140
-
141
- if (l < 0.001) {
142
- return strings.getStringForLocale('black', locale);
143
- }
144
-
145
- let hue: string;
146
- [hue, l] = this.getOklchHue(l, c, h, locale);
147
-
148
- let lightness = '';
149
- let chroma = '';
150
- if (c <= 0.1 && c >= GRAY_THRESHOLD) {
151
- if (l >= 0.7) {
152
- chroma = 'pale';
153
- } else {
154
- chroma = 'grayish';
155
- }
156
- } else if (c >= 0.15) {
157
- chroma = 'vibrant';
158
- }
159
-
160
- if (l < 0.3) {
161
- lightness = 'very dark';
162
- } else if (l < MAX_DARK_LIGHTNESS) {
163
- lightness = 'dark';
164
- } else if (l < 0.7) {
165
- // none
166
- } else if (l < 0.85) {
167
- lightness = 'light';
168
- } else {
169
- lightness = 'very light';
170
- }
171
-
172
- if (chroma) {
173
- chroma = strings.getStringForLocale(chroma, locale);
174
- }
175
-
176
- if (lightness) {
177
- lightness = strings.getStringForLocale(lightness, locale);
178
- }
179
-
180
- let alpha = this.getChannelValue('alpha');
181
- let formatter = new LocalizedStringFormatter(locale, strings);
182
- if (alpha < 1) {
183
- let percentTransparent = new NumberFormatter(locale, {style: 'percent'}).format(1 - alpha);
184
- return formatter.format('transparentColorName', {
185
- lightness,
186
- chroma,
187
- hue,
188
- percentTransparent
189
- }).replace(/\s+/g, ' ').trim();
190
- } else {
191
- return formatter.format('colorName', {
192
- lightness,
193
- chroma,
194
- hue
195
- }).replace(/\s+/g, ' ').trim();
196
- }
197
- }
198
-
199
- private getOklchHue(l: number, c: number, h: number, locale: string): [string, number] {
200
- let strings = LocalizedStringDictionary.getGlobalDictionaryForPackage('@react-stately/color') || dictionary;
201
- if (c < GRAY_THRESHOLD) {
202
- return [strings.getStringForLocale('gray', locale), l];
203
- }
204
-
205
- for (let i = 0; i < OKLCH_HUES.length; i++) {
206
- let [hue, hueName] = OKLCH_HUES[i];
207
- let [nextHue, nextHueName] = OKLCH_HUES[i + 1] || [360, 'pink'];
208
- if (h >= hue && h < nextHue) {
209
- // Split orange hue into brown/orange depending on lightness.
210
- if (hueName === 'orange') {
211
- if (l < ORANGE_LIGHTNESS_THRESHOLD) {
212
- hueName = 'brown';
213
- } else {
214
- // Adjust lightness.
215
- l = (l - ORANGE_LIGHTNESS_THRESHOLD) + MAX_DARK_LIGHTNESS;
216
- }
217
- }
218
-
219
- // If the hue is at least halfway to the next hue, add the next hue name as well.
220
- if (h > hue + (nextHue - hue) / 2 && hueName !== nextHueName) {
221
- hueName = `${hueName} ${nextHueName}`;
222
- } else if (hueName === 'yellow' && l < YELLOW_GREEN_LIGHTNESS_THRESHOLD) {
223
- // Yellow shifts toward green at lower lightnesses.
224
- hueName = 'yellow green';
225
- }
226
-
227
- let name = strings.getStringForLocale(hueName, locale).toLocaleLowerCase(locale);
228
- return [name, l];
229
- }
230
- }
231
-
232
- throw new Error('Unexpected hue');
233
- }
234
-
235
- getHueName(locale: string): string {
236
- let [l, c, h] = toOKLCH(this);
237
- let [name] = this.getOklchHue(l, c, h, locale);
238
- return name;
239
- }
240
- }
241
-
242
- class RGBColor extends Color {
243
- constructor(private red: number, private green: number, private blue: number, private alpha: number) {
244
- super();
245
- }
246
-
247
- static parse(value: string) {
248
- let colors: Array<number | undefined> = [];
249
- // matching #rgb, #rgba, #rrggbb, #rrggbbaa
250
- if (/^#[\da-f]+$/i.test(value) && [4, 5, 7, 9].includes(value.length)) {
251
- const values = (value.length < 6 ? value.replace(/[^#]/gi, '$&$&') : value).slice(1).split('');
252
- while (values.length > 0) {
253
- colors.push(parseInt(values.splice(0, 2).join(''), 16));
254
- }
255
- colors[3] = colors[3] !== undefined ? colors[3] / 255 : undefined;
256
- }
257
-
258
- // matching rgb(rrr, ggg, bbb), rgba(rrr, ggg, bbb, 0.a)
259
- const match = value.match(/^rgba?\((.*)\)$/);
260
- if (match?.[1]) {
261
- colors = match[1].split(',').map(value => Number(value.trim()));
262
- colors = colors.map((num, i) => {
263
- return clamp(num ?? 0, 0, i < 3 ? 255 : 1);
264
- });
265
- }
266
- if (colors[0] === undefined || colors[1] === undefined || colors[2] === undefined) {
267
- return undefined;
268
- }
269
-
270
- return colors.length < 3 ? undefined : new RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1);
271
- }
272
-
273
- toString(format: ColorFormat | 'css' = 'css') {
274
- switch (format) {
275
- case 'hex':
276
- return '#' + (this.red.toString(16).padStart(2, '0') + this.green.toString(16).padStart(2, '0') + this.blue.toString(16).padStart(2, '0')).toUpperCase();
277
- case 'hexa':
278
- return '#' + (this.red.toString(16).padStart(2, '0') + this.green.toString(16).padStart(2, '0') + this.blue.toString(16).padStart(2, '0') + Math.round(this.alpha * 255).toString(16).padStart(2, '0')).toUpperCase();
279
- case 'rgb':
280
- return `rgb(${this.red}, ${this.green}, ${this.blue})`;
281
- case 'css':
282
- case 'rgba':
283
- return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
284
- default:
285
- return this.toFormat(format).toString(format);
286
- }
287
- }
288
-
289
- toFormat(format: ColorFormat): IColor {
290
- switch (format) {
291
- case 'hex':
292
- case 'hexa':
293
- case 'rgb':
294
- case 'rgba':
295
- return this;
296
- case 'hsb':
297
- case 'hsba':
298
- return this.toHSB();
299
- case 'hsl':
300
- case 'hsla':
301
- return this.toHSL();
302
- default:
303
- throw new Error('Unsupported color conversion: rgb -> ' + format);
304
- }
305
- }
306
-
307
- toHexInt(): number {
308
- return this.red << 16 | this.green << 8 | this.blue;
309
- }
310
-
311
- /**
312
- * Converts an RGB color value to HSB.
313
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
314
- * @returns An HSBColor object.
315
- */
316
- private toHSB(): IColor {
317
- const red = this.red / 255;
318
- const green = this.green / 255;
319
- const blue = this.blue / 255;
320
- const min = Math.min(red, green, blue);
321
- const brightness = Math.max(red, green, blue);
322
- const chroma = brightness - min;
323
- const saturation = brightness === 0 ? 0 : chroma / brightness;
324
- let hue = 0; // achromatic
325
-
326
- if (chroma !== 0) {
327
- switch (brightness) {
328
- case red:
329
- hue = (green - blue) / chroma + (green < blue ? 6 : 0);
330
- break;
331
- case green:
332
- hue = (blue - red) / chroma + 2;
333
- break;
334
- case blue:
335
- hue = (red - green) / chroma + 4;
336
- break;
337
- }
338
-
339
- hue /= 6;
340
- }
341
-
342
- return new HSBColor(
343
- toFixedNumber(hue * 360, 2),
344
- toFixedNumber(saturation * 100, 2),
345
- toFixedNumber(brightness * 100, 2),
346
- this.alpha
347
- );
348
- }
349
-
350
- /**
351
- * Converts an RGB color value to HSL.
352
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
353
- * @returns An HSLColor object.
354
- */
355
- private toHSL(): IColor {
356
- const red = this.red / 255;
357
- const green = this.green / 255;
358
- const blue = this.blue / 255;
359
- const min = Math.min(red, green, blue);
360
- const max = Math.max(red, green, blue);
361
- const lightness = (max + min) / 2;
362
- const chroma = max - min;
363
- let hue: number;
364
- let saturation: number;
365
-
366
- if (chroma === 0) {
367
- hue = saturation = 0; // achromatic
368
- } else {
369
- saturation = chroma / (lightness < .5 ? max + min : 2 - max - min);
370
-
371
- switch (max) {
372
- case red:
373
- hue = (green - blue) / chroma + (green < blue ? 6 : 0);
374
- break;
375
- case green:
376
- hue = (blue - red) / chroma + 2;
377
- break;
378
- case blue:
379
- default:
380
- hue = (red - green) / chroma + 4;
381
- break;
382
- }
383
-
384
- hue /= 6;
385
- }
386
-
387
- return new HSLColor(
388
- toFixedNumber(hue * 360, 2),
389
- toFixedNumber(saturation * 100, 2),
390
- toFixedNumber(lightness * 100, 2),
391
- this.alpha);
392
- }
393
-
394
- clone(): IColor {
395
- return new RGBColor(this.red, this.green, this.blue, this.alpha);
396
- }
397
-
398
- getChannelRange(channel: ColorChannel): ColorChannelRange {
399
- switch (channel) {
400
- case 'red':
401
- case 'green':
402
- case 'blue':
403
- return {minValue: 0x0, maxValue: 0xFF, step: 0x1, pageSize: 0x11};
404
- case 'alpha':
405
- return {minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1};
406
- default:
407
- throw new Error('Unknown color channel: ' + channel);
408
- }
409
- }
410
-
411
- getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions {
412
- switch (channel) {
413
- case 'red':
414
- case 'green':
415
- case 'blue':
416
- return {style: 'decimal'};
417
- case 'alpha':
418
- return {style: 'percent'};
419
- default:
420
- throw new Error('Unknown color channel: ' + channel);
421
- }
422
- }
423
-
424
- formatChannelValue(channel: ColorChannel, locale: string) {
425
- let options = this.getChannelFormatOptions(channel);
426
- let value = this.getChannelValue(channel);
427
- return new NumberFormatter(locale, options).format(value);
428
- }
429
-
430
- getColorSpace(): ColorSpace {
431
- return 'rgb';
432
- }
433
-
434
- static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = ['red', 'green', 'blue'];
435
- getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
436
- return RGBColor.colorChannels;
437
- }
438
- }
439
-
440
- // X = <negative/positive number with/without decimal places>
441
- // before/after a comma, 0 or more whitespaces are allowed
442
- // - hsb(X, X%, X%)
443
- // - hsba(X, X%, X%, X)
444
- const HSB_REGEX = /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+)?)\)/;
445
-
446
- class HSBColor extends Color {
447
- constructor(private hue: number, private saturation: number, private brightness: number, private alpha: number) {
448
- super();
449
- }
450
-
451
- static parse(value: string): HSBColor | void {
452
- let m: RegExpMatchArray | null;
453
- if ((m = value.match(HSB_REGEX))) {
454
- const [h, s, b, a] = (m[1] ?? m[2]).split(',').map(n => Number(n.trim().replace('%', '')));
455
- return new HSBColor(normalizeHue(h), clamp(s, 0, 100), clamp(b, 0, 100), clamp(a ?? 1, 0, 1));
456
- }
457
- }
458
-
459
- toString(format: ColorFormat | 'css' = 'css') {
460
- switch (format) {
461
- case 'css':
462
- return this.toHSL().toString('css');
463
- case 'hex':
464
- return this.toRGB().toString('hex');
465
- case 'hexa':
466
- return this.toRGB().toString('hexa');
467
- case 'hsb':
468
- return `hsb(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%)`;
469
- case 'hsba':
470
- return `hsba(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%, ${this.alpha})`;
471
- default:
472
- return this.toFormat(format).toString(format);
473
- }
474
- }
475
-
476
- toFormat(format: ColorFormat): IColor {
477
- switch (format) {
478
- case 'hsb':
479
- case 'hsba':
480
- return this;
481
- case 'hsl':
482
- case 'hsla':
483
- return this.toHSL();
484
- case 'rgb':
485
- case 'rgba':
486
- return this.toRGB();
487
- default:
488
- throw new Error('Unsupported color conversion: hsb -> ' + format);
489
- }
490
- }
491
-
492
- /**
493
- * Converts a HSB color to HSL.
494
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
495
- * @returns An HSLColor object.
496
- */
497
- private toHSL(): IColor {
498
- let saturation = this.saturation / 100;
499
- let brightness = this.brightness / 100;
500
- let lightness = brightness * (1 - saturation / 2);
501
- saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
502
-
503
- return new HSLColor(
504
- toFixedNumber(this.hue, 2),
505
- toFixedNumber(saturation * 100, 2),
506
- toFixedNumber(lightness * 100, 2),
507
- this.alpha
508
- );
509
- }
510
-
511
- /**
512
- * Converts a HSV color value to RGB.
513
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
514
- * @returns An RGBColor object.
515
- */
516
- private toRGB(): IColor {
517
- let hue = this.hue;
518
- let saturation = this.saturation / 100;
519
- let brightness = this.brightness / 100;
520
- let fn = (n: number, k = (n + hue / 60) % 6) => brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
521
- return new RGBColor(
522
- Math.round(fn(5) * 255),
523
- Math.round(fn(3) * 255),
524
- Math.round(fn(1) * 255),
525
- this.alpha
526
- );
527
- }
528
-
529
- clone(): IColor {
530
- return new HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
531
- }
532
-
533
- getChannelRange(channel: ColorChannel): ColorChannelRange {
534
- switch (channel) {
535
- case 'hue':
536
- return {minValue: 0, maxValue: 360, step: 1, pageSize: 15};
537
- case 'saturation':
538
- case 'brightness':
539
- return {minValue: 0, maxValue: 100, step: 1, pageSize: 10};
540
- case 'alpha':
541
- return {minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1};
542
- default:
543
- throw new Error('Unknown color channel: ' + channel);
544
- }
545
- }
546
-
547
- getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions {
548
- switch (channel) {
549
- case 'hue':
550
- return {style: 'unit', unit: 'degree', unitDisplay: 'narrow'};
551
- case 'saturation':
552
- case 'brightness':
553
- case 'alpha':
554
- return {style: 'percent'};
555
- default:
556
- throw new Error('Unknown color channel: ' + channel);
557
- }
558
- }
559
-
560
- formatChannelValue(channel: ColorChannel, locale: string) {
561
- let options = this.getChannelFormatOptions(channel);
562
- let value = this.getChannelValue(channel);
563
- if (channel === 'saturation' || channel === 'brightness') {
564
- value /= 100;
565
- }
566
- return new NumberFormatter(locale, options).format(value);
567
- }
568
-
569
- getColorSpace(): ColorSpace {
570
- return 'hsb';
571
- }
572
-
573
- static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = ['hue', 'saturation', 'brightness'];
574
- getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
575
- return HSBColor.colorChannels;
576
- }
577
- }
578
-
579
- // X = <negative/positive number with/without decimal places>
580
- // before/after a comma, 0 or more whitespaces are allowed
581
- // - hsl(X, X%, X%)
582
- // - hsla(X, X%, X%, X)
583
- const HSL_REGEX = /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+)?)\)/;
584
-
585
- class HSLColor extends Color {
586
- constructor(private hue: number, private saturation: number, private lightness: number, private alpha: number) {
587
- super();
588
- }
589
-
590
- static parse(value: string): HSLColor | void {
591
- let m: RegExpMatchArray | null;
592
- if ((m = value.match(HSL_REGEX))) {
593
- const [h, s, l, a] = (m[1] ?? m[2]).split(',').map(n => Number(n.trim().replace('%', '')));
594
- return new HSLColor(normalizeHue(h), clamp(s, 0, 100), clamp(l, 0, 100), clamp(a ?? 1, 0, 1));
595
- }
596
- }
597
-
598
- toString(format: ColorFormat | 'css' = 'css') {
599
- switch (format) {
600
- case 'hex':
601
- return this.toRGB().toString('hex');
602
- case 'hexa':
603
- return this.toRGB().toString('hexa');
604
- case 'hsl':
605
- return `hsl(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%)`;
606
- case 'css':
607
- case 'hsla':
608
- return `hsla(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%, ${this.alpha})`;
609
- default:
610
- return this.toFormat(format).toString(format);
611
- }
612
- }
613
- toFormat(format: ColorFormat): IColor {
614
- switch (format) {
615
- case 'hsl':
616
- case 'hsla':
617
- return this;
618
- case 'hsb':
619
- case 'hsba':
620
- return this.toHSB();
621
- case 'rgb':
622
- case 'rgba':
623
- return this.toRGB();
624
- default:
625
- throw new Error('Unsupported color conversion: hsl -> ' + format);
626
- }
627
- }
628
-
629
- /**
630
- * Converts a HSL color to HSB.
631
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
632
- * @returns An HSBColor object.
633
- */
634
- private toHSB(): IColor {
635
- let saturation = this.saturation / 100;
636
- let lightness = this.lightness / 100;
637
- let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
638
- saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
639
- return new HSBColor(
640
- toFixedNumber(this.hue, 2),
641
- toFixedNumber(saturation * 100, 2),
642
- toFixedNumber(brightness * 100, 2),
643
- this.alpha
644
- );
645
- }
646
-
647
- /**
648
- * Converts a HSL color to RGB.
649
- * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
650
- * @returns An RGBColor object.
651
- */
652
- private toRGB(): IColor {
653
- let hue = this.hue;
654
- let saturation = this.saturation / 100;
655
- let lightness = this.lightness / 100;
656
- let a = saturation * Math.min(lightness, 1 - lightness);
657
- let fn = (n: number, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
658
- return new RGBColor(
659
- Math.round(fn(0) * 255),
660
- Math.round(fn(8) * 255),
661
- Math.round(fn(4) * 255),
662
- this.alpha
663
- );
664
- }
665
-
666
- clone(): IColor {
667
- return new HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
668
- }
669
-
670
- getChannelRange(channel: ColorChannel): ColorChannelRange {
671
- switch (channel) {
672
- case 'hue':
673
- return {minValue: 0, maxValue: 360, step: 1, pageSize: 15};
674
- case 'saturation':
675
- case 'lightness':
676
- return {minValue: 0, maxValue: 100, step: 1, pageSize: 10};
677
- case 'alpha':
678
- return {minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1};
679
- default:
680
- throw new Error('Unknown color channel: ' + channel);
681
- }
682
- }
683
-
684
- getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions {
685
- switch (channel) {
686
- case 'hue':
687
- return {style: 'unit', unit: 'degree', unitDisplay: 'narrow'};
688
- case 'saturation':
689
- case 'lightness':
690
- case 'alpha':
691
- return {style: 'percent'};
692
- default:
693
- throw new Error('Unknown color channel: ' + channel);
694
- }
695
- }
696
-
697
- formatChannelValue(channel: ColorChannel, locale: string) {
698
- let options = this.getChannelFormatOptions(channel);
699
- let value = this.getChannelValue(channel);
700
- if (channel === 'saturation' || channel === 'lightness') {
701
- value /= 100;
702
- }
703
- return new NumberFormatter(locale, options).format(value);
704
- }
705
-
706
- getColorSpace(): ColorSpace {
707
- return 'hsl';
708
- }
709
-
710
- static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = ['hue', 'saturation', 'lightness'];
711
- getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
712
- return HSLColor.colorChannels;
713
- }
714
- }
715
-
716
- // https://www.w3.org/TR/css-color-4/#color-conversion-code
717
- function toOKLCH(color: Color) {
718
- let rgb = color.toFormat('rgb');
719
- let red = rgb.getChannelValue('red') / 255;
720
- let green = rgb.getChannelValue('green') / 255;
721
- let blue = rgb.getChannelValue('blue') / 255;
722
- [red, green, blue] = lin_sRGB(red, green, blue);
723
- let [x, y, z] = lin_sRGB_to_XYZ(red, green, blue);
724
- let [l, a, b] = XYZ_to_OKLab(x, y, z);
725
- return OKLab_to_OKLCH(l, a, b);
726
- }
727
-
728
- function OKLab_to_OKLCH(l: number, a: number, b: number): [number, number, number] {
729
- var hue = Math.atan2(b, a) * 180 / Math.PI;
730
- return [
731
- l,
732
- Math.sqrt(a ** 2 + b ** 2), // Chroma
733
- hue >= 0 ? hue : hue + 360 // Hue, in degrees [0 to 360)
734
- ];
735
- }
736
-
737
- function lin_sRGB(r: number, g: number, b: number): [number, number, number] {
738
- // convert an array of sRGB values
739
- // where in-gamut values are in the range [0 - 1]
740
- // to linear light (un-companded) form.
741
- // https://en.wikipedia.org/wiki/SRGB
742
- // Extended transfer function:
743
- // for negative values, linear portion is extended on reflection of axis,
744
- // then reflected power function is used.
745
- return [lin_sRGB_component(r), lin_sRGB_component(g), lin_sRGB_component(b)];
746
- }
747
-
748
- function lin_sRGB_component(val: number) {
749
- let sign = val < 0 ? -1 : 1;
750
- let abs = Math.abs(val);
751
-
752
- if (abs <= 0.04045) {
753
- return val / 12.92;
754
- }
755
-
756
- return sign * (Math.pow((abs + 0.055) / 1.055, 2.4));
757
- }
758
-
759
- function lin_sRGB_to_XYZ(r: number, g: number, b: number) {
760
- // convert an array of linear-light sRGB values to CIE XYZ
761
- // using sRGB's own white, D65 (no chromatic adaptation)
762
- const M = [
763
- 506752 / 1228815, 87881 / 245763, 12673 / 70218,
764
- 87098 / 409605, 175762 / 245763, 12673 / 175545,
765
- 7918 / 409605, 87881 / 737289, 1001167 / 1053270
766
- ];
767
- return multiplyMatrix(M, r, g, b);
768
- }
769
-
770
- function XYZ_to_OKLab(x: number, y: number, z: number) {
771
- // Given XYZ relative to D65, convert to OKLab
772
- const XYZtoLMS = [
773
- 0.8190224379967030, 0.3619062600528904, -0.1288737815209879,
774
- 0.0329836539323885, 0.9292868615863434, 0.0361446663506424,
775
- 0.0481771893596242, 0.2642395317527308, 0.6335478284694309
776
- ];
777
- const LMStoOKLab = [
778
- 0.2104542683093140, 0.7936177747023054, -0.0040720430116193,
779
- 1.9779985324311684, -2.4285922420485799, 0.4505937096174110,
780
- 0.0259040424655478, 0.7827717124575296, -0.8086757549230774
781
- ];
782
-
783
- let [a, b, c] = multiplyMatrix(XYZtoLMS, x, y, z);
784
- return multiplyMatrix(LMStoOKLab, Math.cbrt(a), Math.cbrt(b), Math.cbrt(c));
785
- }
786
-
787
- function multiplyMatrix(m: number[], x: number, y: number, z: number): [number, number, number] {
788
- let a = m[0] * x + m[1] * y + m[2] * z;
789
- let b = m[3] * x + m[4] * y + m[5] * z;
790
- let c = m[6] * x + m[7] * y + m[8] * z;
791
- return [a, b, c];
792
- }