@ledvance/base 1.2.83 → 1.2.84

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 (165) hide show
  1. package/.babelrc +31 -31
  2. package/.eslintignore +5 -5
  3. package/.eslintrc.js +27 -27
  4. package/.prettierrc.js +1 -1
  5. package/.versionrc +5 -5
  6. package/localazy.json +848 -848
  7. package/localazy.keys.json +4 -4
  8. package/package.json +74 -74
  9. package/rn-cli.config.js +8 -8
  10. package/src/api/native.d.ts +71 -71
  11. package/src/api/native.ts +423 -423
  12. package/src/api/nativeEventEmitter.d.ts +3 -3
  13. package/src/api/nativeEventEmitter.ts +125 -125
  14. package/src/components/AdvanceCard.d.ts +25 -25
  15. package/src/components/AdvanceCard.tsx +117 -117
  16. package/src/components/AdvanceList.d.ts +9 -9
  17. package/src/components/AdvanceList.tsx +69 -69
  18. package/src/components/ApplyForDeviceItem.d.ts +6 -6
  19. package/src/components/ApplyForDeviceItem.tsx +83 -83
  20. package/src/components/ApplyForDeviceList.d.ts +7 -7
  21. package/src/components/ApplyForDeviceList.tsx +61 -61
  22. package/src/components/ApplyForText.d.ts +6 -6
  23. package/src/components/ApplyForText.tsx +52 -52
  24. package/src/components/Card.d.ts +10 -10
  25. package/src/components/Card.tsx +40 -40
  26. package/src/components/Cell.d.ts +21 -21
  27. package/src/components/Cell.tsx +69 -69
  28. package/src/components/CircularProgress.d.ts +8 -8
  29. package/src/components/CircularProgress.tsx +42 -42
  30. package/src/components/ColorAdjustView.d.ts +12 -12
  31. package/src/components/ColorAdjustView.tsx +64 -64
  32. package/src/components/ColorExtractor.d.ts +73 -73
  33. package/src/components/ColorExtractor.tsx +374 -374
  34. package/src/components/ColorTempAdjustView.d.ts +13 -13
  35. package/src/components/ColorTempAdjustView.tsx +57 -57
  36. package/src/components/ColorsLine.d.ts +7 -7
  37. package/src/components/ColorsLine.tsx +47 -47
  38. package/src/components/CustomListDialog.d.ts +15 -15
  39. package/src/components/CustomListDialog.tsx +66 -66
  40. package/src/components/DeleteButton.d.ts +10 -10
  41. package/src/components/DeleteButton.tsx +40 -40
  42. package/src/components/Dialog.d.ts +10 -10
  43. package/src/components/Dialog.tsx +38 -38
  44. package/src/components/DrawToolView.d.ts +46 -46
  45. package/src/components/DrawToolView.tsx +262 -262
  46. package/src/components/FanAdjustView.d.ts +23 -23
  47. package/src/components/FanAdjustView.tsx +141 -141
  48. package/src/components/InfoText.d.ts +11 -11
  49. package/src/components/InfoText.tsx +45 -45
  50. package/src/components/LampAdjustView.d.ts +10 -10
  51. package/src/components/LampAdjustView.tsx +75 -75
  52. package/src/components/LampAdjustView2.d.ts +24 -24
  53. package/src/components/LampAdjustView2.tsx +104 -104
  54. package/src/components/LinearGradientLine.d.ts +9 -9
  55. package/src/components/LinearGradientLine.tsx +63 -63
  56. package/src/components/LoadingView.tsx +39 -39
  57. package/src/components/MoodColorsLine.d.ts +9 -9
  58. package/src/components/MoodColorsLine.tsx +38 -38
  59. package/src/components/Page.d.ts +21 -21
  60. package/src/components/Page.tsx +104 -104
  61. package/src/components/Popup.d.ts +7 -7
  62. package/src/components/Popup.tsx +71 -71
  63. package/src/components/Segmented.d.ts +14 -14
  64. package/src/components/Segmented.tsx +76 -76
  65. package/src/components/ShowSelect.d.ts +1 -1
  66. package/src/components/ShowSelect.tsx +158 -158
  67. package/src/components/SocketItem.d.ts +13 -13
  68. package/src/components/SocketItem.tsx +87 -87
  69. package/src/components/Spacer.d.ts +7 -7
  70. package/src/components/Spacer.tsx +13 -13
  71. package/src/components/Stepper.d.ts +19 -19
  72. package/src/components/Stepper.tsx +130 -130
  73. package/src/components/StripAdjustView.d.ts +16 -16
  74. package/src/components/StripAdjustView.tsx +129 -129
  75. package/src/components/StripLightView.d.ts +14 -14
  76. package/src/components/StripLightView.tsx +24 -24
  77. package/src/components/Summary.tsx +116 -116
  78. package/src/components/Tag.d.ts +8 -8
  79. package/src/components/Tag.tsx +56 -56
  80. package/src/components/TextButton.d.ts +9 -9
  81. package/src/components/TextButton.tsx +31 -31
  82. package/src/components/TextField.d.ts +9 -9
  83. package/src/components/TextField.tsx +92 -92
  84. package/src/components/TextFieldStyleButton.d.ts +8 -8
  85. package/src/components/TextFieldStyleButton.tsx +85 -85
  86. package/src/components/UATabTitle.d.ts +6 -6
  87. package/src/components/UATabTitle.tsx +31 -31
  88. package/src/components/UATabs.d.ts +12 -12
  89. package/src/components/UATabs.tsx +77 -77
  90. package/src/components/connect.d.ts +10 -10
  91. package/src/components/connect.tsx +20 -20
  92. package/src/components/ldvColorBrightness.d.ts +1 -1
  93. package/src/components/ldvColorBrightness.tsx +18 -18
  94. package/src/components/ldvColorSlider.d.ts +14 -14
  95. package/src/components/ldvColorSlider.tsx +131 -131
  96. package/src/components/ldvItemView.d.ts +12 -12
  97. package/src/components/ldvItemView.tsx +43 -43
  98. package/src/components/ldvPickerView.d.ts +13 -13
  99. package/src/components/ldvPickerView.tsx +97 -97
  100. package/src/components/ldvPresetView.d.ts +2 -2
  101. package/src/components/ldvPresetView.tsx +86 -86
  102. package/src/components/ldvSaturation.d.ts +2 -2
  103. package/src/components/ldvSaturation.tsx +22 -22
  104. package/src/components/ldvSlider.d.ts +15 -15
  105. package/src/components/ldvSlider.tsx +103 -103
  106. package/src/components/ldvSwitch.d.ts +10 -10
  107. package/src/components/ldvSwitch.tsx +52 -52
  108. package/src/components/ldvTemperatureSlider.d.ts +13 -13
  109. package/src/components/ldvTemperatureSlider.tsx +139 -139
  110. package/src/components/ldvTopBar.d.ts +10 -10
  111. package/src/components/ldvTopBar.tsx +76 -76
  112. package/src/components/ldvTopName.d.ts +9 -9
  113. package/src/components/ldvTopName.tsx +63 -63
  114. package/src/components/segmentControl.d.ts +1 -1
  115. package/src/components/segmentControl.tsx +65 -65
  116. package/src/components/weekSelect.d.ts +7 -7
  117. package/src/components/weekSelect.tsx +98 -98
  118. package/src/composeLayout.d.ts +30 -30
  119. package/src/composeLayout.tsx +261 -261
  120. package/src/config/dark-theme.ts +61 -61
  121. package/src/config/light-theme.ts +61 -61
  122. package/src/hooks/Hooks.d.ts +2 -2
  123. package/src/hooks/Hooks.ts +8 -8
  124. package/src/i18n/index.d.ts +16 -16
  125. package/src/i18n/index.ts +37 -37
  126. package/src/i18n/strings.d.ts +15953 -15953
  127. package/src/i18n/strings.ts +23723 -23723
  128. package/src/main.tsx +9 -9
  129. package/src/models/GlobalParams.d.ts +14 -14
  130. package/src/models/GlobalParams.ts +14 -14
  131. package/src/models/TuyaApi.d.ts +29 -29
  132. package/src/models/TuyaApi.ts +92 -92
  133. package/src/models/combine.d.ts +26 -26
  134. package/src/models/combine.ts +17 -17
  135. package/src/models/configureStore.d.ts +9 -9
  136. package/src/models/configureStore.ts +33 -33
  137. package/src/models/index.d.ts +31 -31
  138. package/src/models/index.ts +22 -22
  139. package/src/models/modules/NativePropsSlice.d.ts +61 -61
  140. package/src/models/modules/NativePropsSlice.tsx +467 -467
  141. package/src/models/modules/Result.d.ts +14 -14
  142. package/src/models/modules/Result.ts +15 -15
  143. package/src/models/modules/common.d.ts +44 -44
  144. package/src/models/modules/common.ts +161 -161
  145. package/src/res/index.d.ts +70 -70
  146. package/src/res/index.ts +73 -73
  147. package/src/utils/ColorParser.d.ts +66 -66
  148. package/src/utils/ColorParser.ts +189 -189
  149. package/src/utils/ColorUtils.d.ts +71 -71
  150. package/src/utils/ColorUtils.tsx +448 -448
  151. package/src/utils/Support.d.ts +13 -13
  152. package/src/utils/Support.ts +108 -108
  153. package/src/utils/TypeUtils.d.ts +3 -3
  154. package/src/utils/cctUtils.d.ts +1 -1
  155. package/src/utils/cctUtils.ts +110 -110
  156. package/src/utils/common.d.ts +35 -35
  157. package/src/utils/common.ts +379 -379
  158. package/src/utils/index.d.ts +26 -26
  159. package/src/utils/index.ts +191 -191
  160. package/src/utils/interface.d.ts +6 -6
  161. package/src/utils/interface.ts +50 -50
  162. package/src/utils/loopsCompare.d.ts +1 -1
  163. package/src/utils/loopsCompare.ts +62 -62
  164. package/tsconfig.json +50 -50
  165. package/update-localazy.py +52 -52
@@ -1,449 +1,449 @@
1
- const RGB_MAX = 255.0
2
- const HUE_MAX = 360.0
3
- const SV_MAX = 100.0
4
-
5
- export default class ColorUtils {
6
- static rgb2Hsl = function (r, g?: any, b?: any) {
7
- if (typeof r === 'object') {
8
- const args = r
9
- r = args.r
10
- g = args.g
11
- b = args.b
12
- }
13
- // It converts [0,255] format, to [0,1]
14
- r = (r === RGB_MAX) ? 1 : (r % RGB_MAX / RGB_MAX)
15
- g = (g === RGB_MAX) ? 1 : (g % RGB_MAX / RGB_MAX)
16
- b = (b === RGB_MAX) ? 1 : (b % RGB_MAX / RGB_MAX)
17
-
18
- const max = Math.max(r, g, b)
19
- const min = Math.min(r, g, b)
20
- let h, s, l = (max + min) / 2
21
-
22
- if (max === min) {
23
- h = s = 0 // achromatic
24
- } else {
25
- const d = max - min
26
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
27
- switch (max) {
28
- case r:
29
- h = (g - b) / d + (g < b ? 6 : 0)
30
- break
31
- case g:
32
- h = (b - r) / d + 2
33
- break
34
- case b:
35
- h = (r - g) / d + 4
36
- break
37
- }
38
- h /= 6
39
- }
40
-
41
- return {
42
- h: Math.round(h * HUE_MAX),
43
- s: Math.round(s * SV_MAX),
44
- l: Math.round(l * SV_MAX),
45
- }
46
- }
47
-
48
- static rgb2Hsv = function (r, g, b) {
49
- if (typeof r === 'object') {
50
- const args = r
51
- r = args.r
52
- g = args.g
53
- b = args.b
54
- }
55
-
56
- // It converts [0,255] format, to [0,1]
57
- r = (r === RGB_MAX) ? 1 : (r % RGB_MAX / RGB_MAX)
58
- g = (g === RGB_MAX) ? 1 : (g % RGB_MAX / RGB_MAX)
59
- b = (b === RGB_MAX) ? 1 : (b % RGB_MAX / RGB_MAX)
60
-
61
- const max = Math.max(r, g, b)
62
- const min = Math.min(r, g, b)
63
- let h, s, v = max
64
-
65
- const d = max - min
66
-
67
- s = max === 0 ? 0 : d / max
68
-
69
- if (max === min) {
70
- h = 0 // achromatic
71
- } else {
72
- switch (max) {
73
- case r:
74
- h = (g - b) / d + (g < b ? 6 : 0)
75
- break
76
- case g:
77
- h = (b - r) / d + 2
78
- break
79
- case b:
80
- h = (r - g) / d + 4
81
- break
82
- }
83
- h /= 6
84
- }
85
-
86
- return {
87
- h: Math.round(h * HUE_MAX),
88
- s: Math.round(s * SV_MAX),
89
- v: Math.round(v * SV_MAX),
90
- }
91
- }
92
-
93
- static hsl2Rgb = function (h, s, l) {
94
- if (typeof h === 'object') {
95
- const args = h
96
- h = args.h
97
- s = args.s
98
- l = args.l
99
- }
100
-
101
- var r, g, b
102
-
103
- h = _normalizeAngle(h)
104
- h = (h === HUE_MAX) ? 1 : (h % HUE_MAX / (HUE_MAX))
105
- s = (s === SV_MAX) ? 1 : (s % SV_MAX / (SV_MAX))
106
- l = (l === SV_MAX) ? 1 : (l % SV_MAX / (SV_MAX))
107
-
108
- if (s === 0) {
109
- r = g = b = l // achromatic
110
- } else {
111
- var q = l < 0.5 ? l * (1 + s) : l + s - l * s
112
- var p = 2 * l - q
113
- r = _hue2Rgb(p, q, h + 1 / 3)
114
- g = _hue2Rgb(p, q, h)
115
- b = _hue2Rgb(p, q, h - 1 / 3)
116
- }
117
-
118
- return {
119
- r: Math.round(r * RGB_MAX),
120
- g: Math.round(g * RGB_MAX),
121
- b: Math.round(b * RGB_MAX),
122
- }
123
- }
124
-
125
- static hsv2Rgb = function (h, s, v) {
126
- if (typeof h === 'object') {
127
- const args = h
128
- h = args.h
129
- s = args.s
130
- v = args.v
131
- }
132
-
133
- h = _normalizeAngle(h)
134
- h = (h === HUE_MAX) ? 1 : (h % HUE_MAX / (HUE_MAX) * 6)
135
- s = (s === SV_MAX) ? 1 : (s % SV_MAX / (SV_MAX))
136
- v = (v === SV_MAX) ? 1 : (v % SV_MAX / (SV_MAX))
137
-
138
- var i = Math.floor(h)
139
- var f = h - i
140
- var p = v * (1 - s)
141
- var q = v * (1 - f * s)
142
- var t = v * (1 - (1 - f) * s)
143
- var mod = i % 6
144
- var r = [v, q, p, p, t, v][mod]
145
- var g = [t, v, v, q, p, p][mod]
146
- var b = [p, p, t, v, v, q][mod]
147
-
148
- return {
149
- r: Math.floor(r * RGB_MAX),
150
- g: Math.floor(g * RGB_MAX),
151
- b: Math.floor(b * RGB_MAX),
152
- }
153
- }
154
-
155
- static rgb2Hex = function (r, g, b) {
156
- if (typeof r === 'object') {
157
- const args = r
158
- r = args.r
159
- g = args.g
160
- b = args.b
161
- }
162
- r = Math.round(r).toString(16)
163
- g = Math.round(g).toString(16)
164
- b = Math.round(b).toString(16)
165
-
166
- r = r.length === 1 ? '0' + r : r
167
- g = g.length === 1 ? '0' + g : g
168
- b = b.length === 1 ? '0' + b : b
169
-
170
- return '#' + r + g + b
171
- }
172
-
173
- static hex2Rgb = function (hex) {
174
- const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
175
- return result ? {
176
- r: parseInt(result[1], 16),
177
- g: parseInt(result[2], 16),
178
- b: parseInt(result[3], 16),
179
- } : null
180
- }
181
-
182
- static hsv2Hex = function (h, s?: any, v?: any) {
183
- const rgb = ColorUtils.hsv2Rgb(h, s, v)
184
- return ColorUtils.rgb2Hex(rgb.r, rgb.g, rgb.b)
185
- }
186
-
187
- static hex2Hsv = function (hex) {
188
- const rgb = ColorUtils.hex2Rgb(hex)!!
189
- return ColorUtils.rgb2Hsv(rgb.r, rgb.g, rgb.b)
190
- }
191
-
192
- static hsl2Hex = function (h, s, l) {
193
- const rgb = ColorUtils.hsl2Rgb(h, s, l)
194
- return ColorUtils.rgb2Hex(rgb.r, rgb.g, rgb.b)
195
- }
196
-
197
- static hex2Hsl = function (hex) {
198
- const rgb = ColorUtils.hex2Rgb(hex)!!
199
- return ColorUtils.rgb2Hsl(rgb.r, rgb.g, rgb.b)
200
- }
201
-
202
- static rgb2Cmyk = function (r, g, b) {
203
- if (typeof r === 'object') {
204
- const args = r
205
- r = args.r
206
- g = args.g
207
- b = args.b
208
- }
209
-
210
- const rprim = r / 255
211
- const gprim = g / 255
212
- const bprim = b / 255
213
-
214
- const k = 1 - Math.max(rprim, gprim, bprim)
215
-
216
- const c = (1 - rprim - k) / (1 - k)
217
- const m = (1 - gprim - k) / (1 - k)
218
- const y = (1 - bprim - k) / (1 - k)
219
-
220
- return {
221
- c: c.toFixed(3),
222
- m: m.toFixed(3),
223
- y: y.toFixed(3),
224
- k: k.toFixed(3),
225
- }
226
- }
227
-
228
- static cmyk2Rgb = function (c?: any, m?: any, y?: any, k?: any) {
229
- if (typeof c === 'object') {
230
- const args = c
231
- c = args.c
232
- m = args.m
233
- y = args.y
234
- k = args.k
235
- }
236
-
237
- var r = 255 * (1 - c) * (1 - k)
238
- var g = 255 * (1 - m) * (1 - k)
239
- var b = 255 * (1 - y) * (1 - k)
240
-
241
- return {
242
- r: Math.floor(r),
243
- g: Math.floor(g),
244
- b: Math.floor(b),
245
- }
246
- }
247
-
248
- static hsv2Hsl = function (h, s?: any, v?: any) {
249
- if (typeof h === 'object') {
250
- const args = h
251
- h = args.h
252
- s = args.s
253
- v = args.v
254
- }
255
-
256
- var l = (2 - s) * v / 2
257
-
258
- if (l !== 0) {
259
- if (l === SV_MAX) {
260
- s = 0
261
- } else if (l < SV_MAX / 2) {
262
- s = s * v / (l * 2)
263
- } else {
264
- s = s * v / (2 - l * 2)
265
- }
266
- }
267
-
268
- return {h: h, s: s, l: l}
269
- }
270
-
271
- static hsl2Hsv = function (h, s, l) {
272
- if (typeof h === 'object') {
273
- const args = h
274
- h = args.h
275
- s = args.s
276
- l = args.l
277
- }
278
-
279
- s = s * (l < 50 ? l : (100 - l))
280
-
281
- return {
282
- h: h,
283
- s: Math.floor(2 * s / (l + s)),
284
- v: Math.floor(l + s),
285
- }
286
- }
287
-
288
- static parseCss = function (cssString) {
289
- if (cssString.indexOf('#') > -1) {
290
- return ColorUtils.hex2Rgb(cssString)
291
- }
292
-
293
- const prefix = cssString.split('(')[0]
294
- const args = cssString.split('(')[1].split(')')[0].split(',')
295
-
296
- // Use the prefix as an array [r, g, b, a] to parse the colours
297
- return prefix.split('').reduce(function (color, param, idx) {
298
- const nextColor = color
299
- nextColor[param] = parseFloat(args[idx])
300
- return nextColor
301
- }, {})
302
- }
303
-
304
- static stringify = function (obj) {
305
- const prefix = Object.keys(obj).join('')
306
- const values = Object.keys(obj).map(function (key) {
307
- var val = obj[key]
308
- if (key === 's' || key === 'v' || key === 'l') {
309
- val = val + '%'
310
- }
311
- return val
312
- })
313
- return prefix + '(' + values.join(', ') + ')'
314
- }
315
-
316
- static hex2Decimal = function (hexColor) {
317
- if (typeof hexColor === 'string') {
318
- return parseInt(hexColor.replace('#', ''), 16)
319
- }
320
- }
321
-
322
- static decimal2Hex = function (decimalColor) {
323
- if (typeof decimalColor === 'string') {
324
- return '#' + parseInt(decimalColor).toString(16)
325
- }
326
- return '#' + decimalColor.toString(16)
327
- }
328
-
329
- static random = function () {
330
- const base = '000000'
331
- const number = Math.floor(Math.random() * 16777215).toString(16)
332
- return '#' + (base + number).substr(-6)
333
- }
334
-
335
- static rotateHue = function (hue, amount) {
336
- if (amount === void 0) {
337
- amount = 0
338
- }
339
- const aux = typeof hue === 'object'
340
- ? (hue.h + amount) % 360
341
- : (hue + amount) % 360
342
-
343
- const nextHue = aux < 0 ? (360 + aux) : aux
344
- return typeof hue === 'object'
345
- ? Object.assign(hue, {h: nextHue})
346
- : nextHue
347
- }
348
-
349
- static getColorEncoding = function (color) {
350
- if (typeof color === 'string') {
351
- try {
352
- ColorUtils.hex2Rgb(color)
353
- return 'hex'
354
- } catch (err) { /* Silent catch */
355
- }
356
- }
357
-
358
- if (typeof color !== 'object') {
359
- return 'unknown'
360
- }
361
-
362
- // Now check that the sum of the components is still a number
363
- // And different than NaN (for that the boolean check)
364
- const c = color
365
-
366
- if ((c.r + c.g + c.b) && typeof (c.r + c.g + c.b) === 'number') {
367
- return 'rgb'
368
- }
369
-
370
- if ((c.h + c.s + c.v) && typeof (c.h + c.s + c.v) === 'number') {
371
- return 'hsv'
372
- }
373
-
374
- if ((c.h + c.s + c.l) && typeof (c.h + c.s + c.l) === 'number') {
375
- return 'hsl'
376
- }
377
-
378
- if ((c.c + c.m + c.y + c.k) && typeof (c.c + c.m + c.y + c.k) === 'number') {
379
- return 'cmyk'
380
- }
381
-
382
- return 'unknown'
383
- }
384
-
385
- static any2Hsl = function (color) {
386
- const colorEncoding = ColorUtils.getColorEncoding(color)
387
-
388
- switch (colorEncoding) {
389
- case 'hsl':
390
- return color
391
- case 'rgb':
392
- return ColorUtils.rgb2Hsl(color)
393
- case 'hex':
394
- return ColorUtils.hex2Hsl(color)
395
- case 'hsv':
396
- return ColorUtils.hsv2Hsl(color)
397
- case 'cmyk':
398
- return ColorUtils.rgb2Hsl(ColorUtils.cmyk2Rgb(color))
399
- default:
400
- return 'unknown'
401
- }
402
- }
403
-
404
- static getTransformEncodingFunction = function (color, desiredEncoding) {
405
- const originalEncoding = ColorUtils.getColorEncoding(color)
406
- return ColorUtils[originalEncoding + '_to_' + desiredEncoding]
407
- }
408
-
409
- static darken = function (color, percentage) {
410
- const encoding = ColorUtils.getColorEncoding(color)
411
-
412
- if (encoding === 'unknown') {
413
- return color
414
- }
415
-
416
- // Missing transformation function between hsl and cmyk
417
- // Also, this algo is simple and precise
418
- if (encoding === 'cmyk') {
419
- const nextCmyk = color
420
- nextCmyk.k = Math.min(100, 100 * percentage + nextCmyk.k)
421
- return nextCmyk
422
- }
423
-
424
- const hsl = ColorUtils.any2Hsl(color)
425
- const nextHsl = {h: hsl.h, s: hsl.s, l: Math.round(hsl.l * (1 - percentage))}
426
-
427
- const transformFn = encoding === 'hsl'
428
- ? c => c // If HSL return as incame
429
- : ColorUtils.getTransformEncodingFunction(nextHsl, encoding)
430
-
431
- if (typeof transformFn !== 'function') {
432
- return color
433
- }
434
- return transformFn(nextHsl)
435
- }
436
- }
437
-
438
- function _normalizeAngle(degrees) {
439
- return (degrees % 360 + 360) % 360
440
- }
441
-
442
- function _hue2Rgb(p, q, t) {
443
- if (t < 0) t += 1
444
- if (t > 1) t -= 1
445
- if (t < 1 / 6) return p + (q - p) * 6 * t
446
- if (t < 1 / 2) return q
447
- if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
448
- return p
1
+ const RGB_MAX = 255.0
2
+ const HUE_MAX = 360.0
3
+ const SV_MAX = 100.0
4
+
5
+ export default class ColorUtils {
6
+ static rgb2Hsl = function (r, g?: any, b?: any) {
7
+ if (typeof r === 'object') {
8
+ const args = r
9
+ r = args.r
10
+ g = args.g
11
+ b = args.b
12
+ }
13
+ // It converts [0,255] format, to [0,1]
14
+ r = (r === RGB_MAX) ? 1 : (r % RGB_MAX / RGB_MAX)
15
+ g = (g === RGB_MAX) ? 1 : (g % RGB_MAX / RGB_MAX)
16
+ b = (b === RGB_MAX) ? 1 : (b % RGB_MAX / RGB_MAX)
17
+
18
+ const max = Math.max(r, g, b)
19
+ const min = Math.min(r, g, b)
20
+ let h, s, l = (max + min) / 2
21
+
22
+ if (max === min) {
23
+ h = s = 0 // achromatic
24
+ } else {
25
+ const d = max - min
26
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
27
+ switch (max) {
28
+ case r:
29
+ h = (g - b) / d + (g < b ? 6 : 0)
30
+ break
31
+ case g:
32
+ h = (b - r) / d + 2
33
+ break
34
+ case b:
35
+ h = (r - g) / d + 4
36
+ break
37
+ }
38
+ h /= 6
39
+ }
40
+
41
+ return {
42
+ h: Math.round(h * HUE_MAX),
43
+ s: Math.round(s * SV_MAX),
44
+ l: Math.round(l * SV_MAX),
45
+ }
46
+ }
47
+
48
+ static rgb2Hsv = function (r, g, b) {
49
+ if (typeof r === 'object') {
50
+ const args = r
51
+ r = args.r
52
+ g = args.g
53
+ b = args.b
54
+ }
55
+
56
+ // It converts [0,255] format, to [0,1]
57
+ r = (r === RGB_MAX) ? 1 : (r % RGB_MAX / RGB_MAX)
58
+ g = (g === RGB_MAX) ? 1 : (g % RGB_MAX / RGB_MAX)
59
+ b = (b === RGB_MAX) ? 1 : (b % RGB_MAX / RGB_MAX)
60
+
61
+ const max = Math.max(r, g, b)
62
+ const min = Math.min(r, g, b)
63
+ let h, s, v = max
64
+
65
+ const d = max - min
66
+
67
+ s = max === 0 ? 0 : d / max
68
+
69
+ if (max === min) {
70
+ h = 0 // achromatic
71
+ } else {
72
+ switch (max) {
73
+ case r:
74
+ h = (g - b) / d + (g < b ? 6 : 0)
75
+ break
76
+ case g:
77
+ h = (b - r) / d + 2
78
+ break
79
+ case b:
80
+ h = (r - g) / d + 4
81
+ break
82
+ }
83
+ h /= 6
84
+ }
85
+
86
+ return {
87
+ h: Math.round(h * HUE_MAX),
88
+ s: Math.round(s * SV_MAX),
89
+ v: Math.round(v * SV_MAX),
90
+ }
91
+ }
92
+
93
+ static hsl2Rgb = function (h, s, l) {
94
+ if (typeof h === 'object') {
95
+ const args = h
96
+ h = args.h
97
+ s = args.s
98
+ l = args.l
99
+ }
100
+
101
+ var r, g, b
102
+
103
+ h = _normalizeAngle(h)
104
+ h = (h === HUE_MAX) ? 1 : (h % HUE_MAX / (HUE_MAX))
105
+ s = (s === SV_MAX) ? 1 : (s % SV_MAX / (SV_MAX))
106
+ l = (l === SV_MAX) ? 1 : (l % SV_MAX / (SV_MAX))
107
+
108
+ if (s === 0) {
109
+ r = g = b = l // achromatic
110
+ } else {
111
+ var q = l < 0.5 ? l * (1 + s) : l + s - l * s
112
+ var p = 2 * l - q
113
+ r = _hue2Rgb(p, q, h + 1 / 3)
114
+ g = _hue2Rgb(p, q, h)
115
+ b = _hue2Rgb(p, q, h - 1 / 3)
116
+ }
117
+
118
+ return {
119
+ r: Math.round(r * RGB_MAX),
120
+ g: Math.round(g * RGB_MAX),
121
+ b: Math.round(b * RGB_MAX),
122
+ }
123
+ }
124
+
125
+ static hsv2Rgb = function (h, s, v) {
126
+ if (typeof h === 'object') {
127
+ const args = h
128
+ h = args.h
129
+ s = args.s
130
+ v = args.v
131
+ }
132
+
133
+ h = _normalizeAngle(h)
134
+ h = (h === HUE_MAX) ? 1 : (h % HUE_MAX / (HUE_MAX) * 6)
135
+ s = (s === SV_MAX) ? 1 : (s % SV_MAX / (SV_MAX))
136
+ v = (v === SV_MAX) ? 1 : (v % SV_MAX / (SV_MAX))
137
+
138
+ var i = Math.floor(h)
139
+ var f = h - i
140
+ var p = v * (1 - s)
141
+ var q = v * (1 - f * s)
142
+ var t = v * (1 - (1 - f) * s)
143
+ var mod = i % 6
144
+ var r = [v, q, p, p, t, v][mod]
145
+ var g = [t, v, v, q, p, p][mod]
146
+ var b = [p, p, t, v, v, q][mod]
147
+
148
+ return {
149
+ r: Math.floor(r * RGB_MAX),
150
+ g: Math.floor(g * RGB_MAX),
151
+ b: Math.floor(b * RGB_MAX),
152
+ }
153
+ }
154
+
155
+ static rgb2Hex = function (r, g, b) {
156
+ if (typeof r === 'object') {
157
+ const args = r
158
+ r = args.r
159
+ g = args.g
160
+ b = args.b
161
+ }
162
+ r = Math.round(r).toString(16)
163
+ g = Math.round(g).toString(16)
164
+ b = Math.round(b).toString(16)
165
+
166
+ r = r.length === 1 ? '0' + r : r
167
+ g = g.length === 1 ? '0' + g : g
168
+ b = b.length === 1 ? '0' + b : b
169
+
170
+ return '#' + r + g + b
171
+ }
172
+
173
+ static hex2Rgb = function (hex) {
174
+ const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
175
+ return result ? {
176
+ r: parseInt(result[1], 16),
177
+ g: parseInt(result[2], 16),
178
+ b: parseInt(result[3], 16),
179
+ } : null
180
+ }
181
+
182
+ static hsv2Hex = function (h, s?: any, v?: any) {
183
+ const rgb = ColorUtils.hsv2Rgb(h, s, v)
184
+ return ColorUtils.rgb2Hex(rgb.r, rgb.g, rgb.b)
185
+ }
186
+
187
+ static hex2Hsv = function (hex) {
188
+ const rgb = ColorUtils.hex2Rgb(hex)!!
189
+ return ColorUtils.rgb2Hsv(rgb.r, rgb.g, rgb.b)
190
+ }
191
+
192
+ static hsl2Hex = function (h, s, l) {
193
+ const rgb = ColorUtils.hsl2Rgb(h, s, l)
194
+ return ColorUtils.rgb2Hex(rgb.r, rgb.g, rgb.b)
195
+ }
196
+
197
+ static hex2Hsl = function (hex) {
198
+ const rgb = ColorUtils.hex2Rgb(hex)!!
199
+ return ColorUtils.rgb2Hsl(rgb.r, rgb.g, rgb.b)
200
+ }
201
+
202
+ static rgb2Cmyk = function (r, g, b) {
203
+ if (typeof r === 'object') {
204
+ const args = r
205
+ r = args.r
206
+ g = args.g
207
+ b = args.b
208
+ }
209
+
210
+ const rprim = r / 255
211
+ const gprim = g / 255
212
+ const bprim = b / 255
213
+
214
+ const k = 1 - Math.max(rprim, gprim, bprim)
215
+
216
+ const c = (1 - rprim - k) / (1 - k)
217
+ const m = (1 - gprim - k) / (1 - k)
218
+ const y = (1 - bprim - k) / (1 - k)
219
+
220
+ return {
221
+ c: c.toFixed(3),
222
+ m: m.toFixed(3),
223
+ y: y.toFixed(3),
224
+ k: k.toFixed(3),
225
+ }
226
+ }
227
+
228
+ static cmyk2Rgb = function (c?: any, m?: any, y?: any, k?: any) {
229
+ if (typeof c === 'object') {
230
+ const args = c
231
+ c = args.c
232
+ m = args.m
233
+ y = args.y
234
+ k = args.k
235
+ }
236
+
237
+ var r = 255 * (1 - c) * (1 - k)
238
+ var g = 255 * (1 - m) * (1 - k)
239
+ var b = 255 * (1 - y) * (1 - k)
240
+
241
+ return {
242
+ r: Math.floor(r),
243
+ g: Math.floor(g),
244
+ b: Math.floor(b),
245
+ }
246
+ }
247
+
248
+ static hsv2Hsl = function (h, s?: any, v?: any) {
249
+ if (typeof h === 'object') {
250
+ const args = h
251
+ h = args.h
252
+ s = args.s
253
+ v = args.v
254
+ }
255
+
256
+ var l = (2 - s) * v / 2
257
+
258
+ if (l !== 0) {
259
+ if (l === SV_MAX) {
260
+ s = 0
261
+ } else if (l < SV_MAX / 2) {
262
+ s = s * v / (l * 2)
263
+ } else {
264
+ s = s * v / (2 - l * 2)
265
+ }
266
+ }
267
+
268
+ return {h: h, s: s, l: l}
269
+ }
270
+
271
+ static hsl2Hsv = function (h, s, l) {
272
+ if (typeof h === 'object') {
273
+ const args = h
274
+ h = args.h
275
+ s = args.s
276
+ l = args.l
277
+ }
278
+
279
+ s = s * (l < 50 ? l : (100 - l))
280
+
281
+ return {
282
+ h: h,
283
+ s: Math.floor(2 * s / (l + s)),
284
+ v: Math.floor(l + s),
285
+ }
286
+ }
287
+
288
+ static parseCss = function (cssString) {
289
+ if (cssString.indexOf('#') > -1) {
290
+ return ColorUtils.hex2Rgb(cssString)
291
+ }
292
+
293
+ const prefix = cssString.split('(')[0]
294
+ const args = cssString.split('(')[1].split(')')[0].split(',')
295
+
296
+ // Use the prefix as an array [r, g, b, a] to parse the colours
297
+ return prefix.split('').reduce(function (color, param, idx) {
298
+ const nextColor = color
299
+ nextColor[param] = parseFloat(args[idx])
300
+ return nextColor
301
+ }, {})
302
+ }
303
+
304
+ static stringify = function (obj) {
305
+ const prefix = Object.keys(obj).join('')
306
+ const values = Object.keys(obj).map(function (key) {
307
+ var val = obj[key]
308
+ if (key === 's' || key === 'v' || key === 'l') {
309
+ val = val + '%'
310
+ }
311
+ return val
312
+ })
313
+ return prefix + '(' + values.join(', ') + ')'
314
+ }
315
+
316
+ static hex2Decimal = function (hexColor) {
317
+ if (typeof hexColor === 'string') {
318
+ return parseInt(hexColor.replace('#', ''), 16)
319
+ }
320
+ }
321
+
322
+ static decimal2Hex = function (decimalColor) {
323
+ if (typeof decimalColor === 'string') {
324
+ return '#' + parseInt(decimalColor).toString(16)
325
+ }
326
+ return '#' + decimalColor.toString(16)
327
+ }
328
+
329
+ static random = function () {
330
+ const base = '000000'
331
+ const number = Math.floor(Math.random() * 16777215).toString(16)
332
+ return '#' + (base + number).substr(-6)
333
+ }
334
+
335
+ static rotateHue = function (hue, amount) {
336
+ if (amount === void 0) {
337
+ amount = 0
338
+ }
339
+ const aux = typeof hue === 'object'
340
+ ? (hue.h + amount) % 360
341
+ : (hue + amount) % 360
342
+
343
+ const nextHue = aux < 0 ? (360 + aux) : aux
344
+ return typeof hue === 'object'
345
+ ? Object.assign(hue, {h: nextHue})
346
+ : nextHue
347
+ }
348
+
349
+ static getColorEncoding = function (color) {
350
+ if (typeof color === 'string') {
351
+ try {
352
+ ColorUtils.hex2Rgb(color)
353
+ return 'hex'
354
+ } catch (err) { /* Silent catch */
355
+ }
356
+ }
357
+
358
+ if (typeof color !== 'object') {
359
+ return 'unknown'
360
+ }
361
+
362
+ // Now check that the sum of the components is still a number
363
+ // And different than NaN (for that the boolean check)
364
+ const c = color
365
+
366
+ if ((c.r + c.g + c.b) && typeof (c.r + c.g + c.b) === 'number') {
367
+ return 'rgb'
368
+ }
369
+
370
+ if ((c.h + c.s + c.v) && typeof (c.h + c.s + c.v) === 'number') {
371
+ return 'hsv'
372
+ }
373
+
374
+ if ((c.h + c.s + c.l) && typeof (c.h + c.s + c.l) === 'number') {
375
+ return 'hsl'
376
+ }
377
+
378
+ if ((c.c + c.m + c.y + c.k) && typeof (c.c + c.m + c.y + c.k) === 'number') {
379
+ return 'cmyk'
380
+ }
381
+
382
+ return 'unknown'
383
+ }
384
+
385
+ static any2Hsl = function (color) {
386
+ const colorEncoding = ColorUtils.getColorEncoding(color)
387
+
388
+ switch (colorEncoding) {
389
+ case 'hsl':
390
+ return color
391
+ case 'rgb':
392
+ return ColorUtils.rgb2Hsl(color)
393
+ case 'hex':
394
+ return ColorUtils.hex2Hsl(color)
395
+ case 'hsv':
396
+ return ColorUtils.hsv2Hsl(color)
397
+ case 'cmyk':
398
+ return ColorUtils.rgb2Hsl(ColorUtils.cmyk2Rgb(color))
399
+ default:
400
+ return 'unknown'
401
+ }
402
+ }
403
+
404
+ static getTransformEncodingFunction = function (color, desiredEncoding) {
405
+ const originalEncoding = ColorUtils.getColorEncoding(color)
406
+ return ColorUtils[originalEncoding + '_to_' + desiredEncoding]
407
+ }
408
+
409
+ static darken = function (color, percentage) {
410
+ const encoding = ColorUtils.getColorEncoding(color)
411
+
412
+ if (encoding === 'unknown') {
413
+ return color
414
+ }
415
+
416
+ // Missing transformation function between hsl and cmyk
417
+ // Also, this algo is simple and precise
418
+ if (encoding === 'cmyk') {
419
+ const nextCmyk = color
420
+ nextCmyk.k = Math.min(100, 100 * percentage + nextCmyk.k)
421
+ return nextCmyk
422
+ }
423
+
424
+ const hsl = ColorUtils.any2Hsl(color)
425
+ const nextHsl = {h: hsl.h, s: hsl.s, l: Math.round(hsl.l * (1 - percentage))}
426
+
427
+ const transformFn = encoding === 'hsl'
428
+ ? c => c // If HSL return as incame
429
+ : ColorUtils.getTransformEncodingFunction(nextHsl, encoding)
430
+
431
+ if (typeof transformFn !== 'function') {
432
+ return color
433
+ }
434
+ return transformFn(nextHsl)
435
+ }
436
+ }
437
+
438
+ function _normalizeAngle(degrees) {
439
+ return (degrees % 360 + 360) % 360
440
+ }
441
+
442
+ function _hue2Rgb(p, q, t) {
443
+ if (t < 0) t += 1
444
+ if (t > 1) t -= 1
445
+ if (t < 1 / 6) return p + (q - p) * 6 * t
446
+ if (t < 1 / 2) return q
447
+ if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
448
+ return p
449
449
  }