@ledvance/base 1.1.13 → 1.1.15

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 (65) hide show
  1. package/.prettierrc.js +0 -0
  2. package/package.json +1 -1
  3. package/src/api/native.d.ts +5 -0
  4. package/src/api/native.ts +193 -160
  5. package/src/api/nativeEventEmitter.ts +54 -54
  6. package/src/components/Card.tsx +25 -25
  7. package/src/components/Cell.tsx +32 -32
  8. package/src/components/ColorAdjustView.tsx +44 -44
  9. package/src/components/ColorTempAdjustView.tsx +37 -37
  10. package/src/components/ColorsLine.d.ts +7 -0
  11. package/src/components/ColorsLine.tsx +48 -0
  12. package/src/components/DeleteButton.d.ts +1 -1
  13. package/src/components/DeleteButton.tsx +27 -27
  14. package/src/components/Dialog.tsx +28 -28
  15. package/src/components/FanAdjustView.tsx +115 -105
  16. package/src/components/InfoText.tsx +29 -29
  17. package/src/components/LampAdjustView.tsx +52 -52
  18. package/src/components/LinearGradientLine.tsx +53 -53
  19. package/src/components/MoodColorsLine.d.ts +9 -0
  20. package/src/components/MoodColorsLine.tsx +38 -0
  21. package/src/components/Page.tsx +34 -34
  22. package/src/components/Popup.tsx +17 -17
  23. package/src/components/Segmented.d.ts +2 -2
  24. package/src/components/Segmented.tsx +66 -65
  25. package/src/components/Spacer.tsx +5 -5
  26. package/src/components/Tag.tsx +42 -42
  27. package/src/components/TextButton.d.ts +1 -1
  28. package/src/components/TextButton.tsx +23 -23
  29. package/src/components/TextField.tsx +58 -58
  30. package/src/components/connect.tsx +10 -10
  31. package/src/components/ldvColorBrightness.tsx +12 -12
  32. package/src/components/ldvColorSlider.tsx +109 -109
  33. package/src/components/ldvPickerView.tsx +70 -70
  34. package/src/components/ldvPresetView.tsx +68 -68
  35. package/src/components/ldvSaturation.tsx +14 -14
  36. package/src/components/ldvSlider.d.ts +3 -3
  37. package/src/components/ldvSlider.tsx +93 -93
  38. package/src/components/ldvSwitch.tsx +35 -35
  39. package/src/components/ldvTemperatureSlider.tsx +120 -120
  40. package/src/components/ldvTopBar.tsx +50 -50
  41. package/src/components/ldvTopName.tsx +44 -44
  42. package/src/components/segmentControl.tsx +59 -59
  43. package/src/components/weekSelect.tsx +76 -76
  44. package/src/composeLayout.tsx +165 -165
  45. package/src/i18n/index.ts +12 -12
  46. package/src/i18n/strings.ts +11004 -11004
  47. package/src/main.tsx +4 -4
  48. package/src/models/GlobalParams.ts +7 -7
  49. package/src/models/TuyaApi.d.ts +3 -3
  50. package/src/models/TuyaApi.ts +61 -61
  51. package/src/models/combine.ts +7 -7
  52. package/src/models/configureStore.ts +14 -14
  53. package/src/models/index.ts +4 -4
  54. package/src/models/modules/NativePropsSlice.tsx +170 -169
  55. package/src/models/modules/Result.ts +8 -8
  56. package/src/models/modules/common.ts +90 -90
  57. package/src/res/index.ts +35 -35
  58. package/src/utils/ColorParser.ts +150 -150
  59. package/src/utils/ColorUtils.tsx +414 -414
  60. package/src/utils/Support.d.ts +9 -0
  61. package/src/utils/Support.ts +85 -0
  62. package/src/utils/cctUtils.d.ts +1 -0
  63. package/src/utils/cctUtils.ts +111 -0
  64. package/src/utils/common.ts +186 -174
  65. package/src/utils/index.ts +123 -123
@@ -3,447 +3,447 @@ const HUE_MAX = 360.0
3
3
  const SV_MAX = 100.0
4
4
 
5
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
- }
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
+ }
247
117
 
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
- }
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
+ }
267
132
 
268
- return { h: h, s: s, l: l }
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
269
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)
270
215
 
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
- }
216
+ const c = (1 - rprim - k) / (1 - k)
217
+ const m = (1 - gprim - k) / (1 - k)
218
+ const y = (1 - bprim - k) / (1 - k)
278
219
 
279
- s = s * (l < 50 ? l : (100 - l))
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
+ }
280
236
 
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
237
+ var r = 255 * (1 - c) * (1 - k)
238
+ var g = 255 * (1 - m) * (1 - k)
239
+ var b = 255 * (1 - y) * (1 - k)
365
240
 
366
- if ((c.r + c.g + c.b) && typeof (c.r + c.g + c.b) === 'number') {
367
- return 'rgb'
368
- }
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
+ }
369
255
 
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
- }
256
+ var l = (2 - s) * v / 2
377
257
 
378
- if ((c.c + c.m + c.y + c.k) && typeof (c.c + c.m + c.y + c.k) === 'number') {
379
- return 'cmyk'
380
- }
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
+ }
381
267
 
382
- return 'unknown'
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
+ }
383
356
  }
384
357
 
385
- static any2Hsl = function(color) {
386
- const colorEncoding = ColorUtils.getColorEncoding(color)
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
+ }
387
373
 
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
- }
374
+ if ((c.h + c.s + c.l) && typeof (c.h + c.s + c.l) === 'number') {
375
+ return 'hsl'
402
376
  }
403
377
 
404
- static getTransformEncodingFunction = function(color, desiredEncoding) {
405
- const originalEncoding = ColorUtils.getColorEncoding(color)
406
- return ColorUtils[originalEncoding + '_to_' + desiredEncoding]
378
+ if ((c.c + c.m + c.y + c.k) && typeof (c.c + c.m + c.y + c.k) === 'number') {
379
+ return 'cmyk'
407
380
  }
408
381
 
409
- static darken = function(color, percentage) {
410
- const encoding = ColorUtils.getColorEncoding(color)
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
+ }
411
408
 
412
- if (encoding === 'unknown') {
413
- return color
414
- }
409
+ static darken = function (color, percentage) {
410
+ const encoding = ColorUtils.getColorEncoding(color)
415
411
 
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
- }
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
423
 
424
- const hsl = ColorUtils.any2Hsl(color)
425
- const nextHsl = { h: hsl.h, s: hsl.s, l: Math.round(hsl.l * (1 - percentage)) }
424
+ const hsl = ColorUtils.any2Hsl(color)
425
+ const nextHsl = {h: hsl.h, s: hsl.s, l: Math.round(hsl.l * (1 - percentage))}
426
426
 
427
- const transformFn = encoding === 'hsl'
428
- ? c => c // If HSL return as incame
429
- : ColorUtils.getTransformEncodingFunction(nextHsl, encoding)
427
+ const transformFn = encoding === 'hsl'
428
+ ? c => c // If HSL return as incame
429
+ : ColorUtils.getTransformEncodingFunction(nextHsl, encoding)
430
430
 
431
- if (typeof transformFn !== 'function') {
432
- return color
433
- }
434
- return transformFn(nextHsl)
431
+ if (typeof transformFn !== 'function') {
432
+ return color
435
433
  }
434
+ return transformFn(nextHsl)
435
+ }
436
436
  }
437
437
 
438
438
  function _normalizeAngle(degrees) {
439
- return (degrees % 360 + 360) % 360
439
+ return (degrees % 360 + 360) % 360
440
440
  }
441
441
 
442
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
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
  }