@tendaui/components 1.3.0 → 1.3.1

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 (142) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +176 -176
  3. package/button/_example/ghost.tsx +2 -5
  4. package/color-picker/utils/color-picker/cmyk.ts +89 -89
  5. package/color-picker/utils/color-picker/color.ts +467 -467
  6. package/color-picker/utils/color-picker/constants.ts +187 -187
  7. package/color-picker/utils/color-picker/draggable.ts +99 -99
  8. package/color-picker/utils/color-picker/format.ts +90 -90
  9. package/color-picker/utils/color-picker/gradient.ts +237 -237
  10. package/color-picker/utils/color-picker/index.ts +7 -7
  11. package/color-picker/utils/color-picker/types.ts +33 -33
  12. package/dialog/hooks/useDialogPosition.ts +35 -35
  13. package/form/type.ts +519 -519
  14. package/global-config/default-config.ts +95 -95
  15. package/global-config/locale/ar_KW.ts +259 -259
  16. package/global-config/locale/en_US.ts +265 -265
  17. package/global-config/locale/it_IT.ts +264 -264
  18. package/global-config/locale/ja_JP.ts +264 -264
  19. package/global-config/locale/ko_KR.ts +264 -264
  20. package/global-config/locale/ru_RU.ts +277 -277
  21. package/global-config/locale/zh_CN.ts +265 -265
  22. package/global-config/locale/zh_TW.ts +265 -265
  23. package/global-config/mobile/default-config.ts +6 -6
  24. package/global-config/mobile/locale/ar_KW.ts +112 -112
  25. package/global-config/mobile/locale/en_US.ts +113 -113
  26. package/global-config/mobile/locale/it_IT.ts +113 -113
  27. package/global-config/mobile/locale/ja_JP.ts +100 -100
  28. package/global-config/mobile/locale/ko_KR.ts +100 -100
  29. package/global-config/mobile/locale/ru_RU.ts +112 -112
  30. package/global-config/mobile/locale/zh_CN.ts +100 -100
  31. package/global-config/mobile/locale/zh_TW.ts +100 -100
  32. package/global-config/t.ts +111 -111
  33. package/input-number/InputNumber.tsx +124 -124
  34. package/input-number/defaultProps.ts +17 -17
  35. package/input-number/index.ts +9 -9
  36. package/input-number/style/css.js +1 -1
  37. package/input-number/style/index.js +1 -1
  38. package/input-number/useInputNumber.tsx +270 -270
  39. package/list/ListItem.tsx +36 -36
  40. package/list/ListItemMeta.tsx +40 -40
  41. package/list/defaultProps.ts +11 -11
  42. package/list/hooks/useListVirtualScroll.ts +82 -82
  43. package/list/style/css.js +1 -1
  44. package/list/style/index.js +1 -1
  45. package/locale/LocalReceiver.ts +55 -55
  46. package/locale/ar_KW.ts +7 -7
  47. package/locale/en_US.ts +7 -7
  48. package/locale/it_IT.ts +6 -6
  49. package/locale/ja_JP.ts +6 -6
  50. package/locale/ko_KR.ts +6 -6
  51. package/locale/ru_RU.ts +6 -6
  52. package/locale/zh_CN.ts +5 -5
  53. package/locale/zh_TW.ts +7 -7
  54. package/package.json +2 -2
  55. package/radio/_example/default.tsx +2 -2
  56. package/select/type.ts +382 -382
  57. package/select-input/type.ts +280 -280
  58. package/slider/SliderHandleButton.tsx +50 -50
  59. package/slider/defaultProps.ts +15 -15
  60. package/slider/style/css.js +1 -1
  61. package/slider/style/index.js +1 -1
  62. package/styles/_global.scss +40 -40
  63. package/styles/_vars.scss +374 -374
  64. package/styles/components/alert/_index.scss +175 -175
  65. package/styles/components/alert/_vars.scss +41 -41
  66. package/styles/components/badge/_index.scss +71 -71
  67. package/styles/components/badge/_vars.scss +26 -26
  68. package/styles/components/button/_index.scss +499 -499
  69. package/styles/components/button/_mixins.scss +40 -40
  70. package/styles/components/button/_vars.scss +121 -121
  71. package/styles/components/checkbox/_index.scss +158 -158
  72. package/styles/components/checkbox/_var.scss +59 -59
  73. package/styles/components/color-picker/_index.scss +586 -586
  74. package/styles/components/color-picker/_vars.scss +79 -79
  75. package/styles/components/dialog/_animate.scss +133 -133
  76. package/styles/components/dialog/_index.scss +310 -310
  77. package/styles/components/dialog/_vars.scss +60 -60
  78. package/styles/components/drawer/_index.scss +206 -206
  79. package/styles/components/drawer/_var.scss +55 -55
  80. package/styles/components/fireworks/_index.scss +86 -86
  81. package/styles/components/fireworks/_vars.scss +5 -5
  82. package/styles/components/form/_index.scss +175 -175
  83. package/styles/components/form/_mixins.scss +74 -74
  84. package/styles/components/form/_vars.scss +101 -101
  85. package/styles/components/input/_index.scss +350 -350
  86. package/styles/components/input/_mixins.scss +120 -120
  87. package/styles/components/input/_vars.scss +130 -130
  88. package/styles/components/input-number/_index.scss +327 -327
  89. package/styles/components/input-number/_vars.scss +56 -56
  90. package/styles/components/ip-input/_index.scss +277 -277
  91. package/styles/components/layout/_index.scss +47 -47
  92. package/styles/components/layout/_vars.scss +19 -19
  93. package/styles/components/layout/doc.scss +74 -74
  94. package/styles/components/list/_index.scss +172 -172
  95. package/styles/components/list/_vars.scss +42 -42
  96. package/styles/components/loading/_index.scss +113 -113
  97. package/styles/components/loading/_vars.scss +40 -40
  98. package/styles/components/notification/_index.scss +140 -140
  99. package/styles/components/notification/_mixins.scss +13 -13
  100. package/styles/components/notification/_vars.scss +60 -60
  101. package/styles/components/popup/_index.scss +78 -78
  102. package/styles/components/popup/_mixin.scss +149 -149
  103. package/styles/components/popup/_vars.scss +33 -33
  104. package/styles/components/radio/_index.scss +376 -376
  105. package/styles/components/radio/_vars.scss +89 -89
  106. package/styles/components/select/_index.scss +291 -291
  107. package/styles/components/select/_var.scss +64 -64
  108. package/styles/components/select-input/_index.scss +5 -5
  109. package/styles/components/select-input/_var.scss +4 -4
  110. package/styles/components/slider/_index.scss +241 -241
  111. package/styles/components/slider/_vars.scss +51 -51
  112. package/styles/components/switch/_index.scss +175 -175
  113. package/styles/components/switch/_vars.scss +63 -63
  114. package/styles/components/table/_index.scss +194 -194
  115. package/styles/components/table/_var.scss +52 -52
  116. package/styles/components/tabs/_index.scss +165 -165
  117. package/styles/components/tabs/_mixins.scss +11 -11
  118. package/styles/components/tabs/_vars.scss +72 -72
  119. package/styles/components/tag/_index.scss +317 -317
  120. package/styles/components/tag/_var.scss +86 -86
  121. package/styles/components/tag-input/_index.scss +164 -164
  122. package/styles/components/tag-input/_vars.scss +17 -17
  123. package/styles/mixins/_focus.scss +8 -8
  124. package/styles/mixins/_layout.scss +32 -32
  125. package/styles/mixins/_reset.scss +11 -11
  126. package/styles/mixins/_scrollbar.scss +32 -32
  127. package/styles/mixins/_text.scss +50 -50
  128. package/styles/themes/_dark.scss +169 -169
  129. package/styles/themes/_font.scss +69 -69
  130. package/styles/themes/_index.scss +5 -5
  131. package/styles/themes/_light.scss +170 -170
  132. package/styles/themes/_radius.scss +9 -9
  133. package/styles/themes/_size.scss +68 -68
  134. package/styles/utilities/_animation.scss +58 -58
  135. package/styles/utilities/_tips.scss +10 -10
  136. package/switch/_example/with-label.tsx +1 -1
  137. package/utils/input-number/large-number.ts +423 -423
  138. package/utils/input-number/number.ts +257 -257
  139. package/utils/log/index.ts +3 -3
  140. package/utils/log/log.ts +29 -29
  141. package/utils/log/types.ts +9 -9
  142. package/utils/style.ts +58 -58
@@ -1,467 +1,467 @@
1
- import tinyColor from "tinycolor2";
2
- import { cmykInputToColor, rgb2cmyk } from "./cmyk";
3
- import { ALPHA_FORMAT_MAP } from "./constants";
4
- import { GradientColorPoint, GradientColors, isGradientColor, parseGradientString } from "./gradient";
5
- import type { AlphaConvertibleFormat, ColorFormat } from "./types";
6
-
7
- export interface ColorObject {
8
- alpha: number;
9
- css: string;
10
- hex: string;
11
- hex8: string;
12
- hsl: string;
13
- hsla: string;
14
- hsv: string;
15
- hsva: string;
16
- rgb: string;
17
- rgba: string;
18
- saturation: number;
19
- value: number;
20
- isGradient: boolean;
21
- linearGradient?: string;
22
- }
23
-
24
- interface ColorStates {
25
- s: number;
26
- v: number;
27
- h: number;
28
- a: number;
29
- }
30
-
31
- interface GradientStates {
32
- colors: GradientColorPoint[];
33
- degree: number;
34
- selectedId: string;
35
- css?: string;
36
- }
37
-
38
- const mathRound = Math.round;
39
- const hsv2rgba = (states: ColorStates): tinyColor.ColorFormats.RGBA => tinyColor(states).toRgb();
40
- const hsv2hsva = (states: ColorStates): tinyColor.ColorFormats.HSVA => tinyColor(states).toHsv();
41
- const hsv2hsla = (states: ColorStates): tinyColor.ColorFormats.HSLA => tinyColor(states).toHsl();
42
-
43
- /**
44
- * 将渐变对象转换成字符串
45
- */
46
- export const gradientColors2string = (object: GradientColors): string => {
47
- const { points, degree } = object;
48
- const colorsStop = points
49
- .sort((pA, pB) => pA.left - pB.left)
50
- .map((p) => `${p.color} ${Math.round(p.left * 100) / 100}%`);
51
-
52
- return `linear-gradient(${degree}deg,${colorsStop.join(",")})`;
53
- };
54
-
55
- /**
56
- * 去除颜色的透明度
57
- */
58
- export const getColorWithoutAlpha = (color: string) => tinyColor(color).setAlpha(1).toHexString();
59
-
60
- // 生成一个随机ID
61
- export const genId = () => (1 + Math.random() * 4294967295).toString(16);
62
-
63
- /**
64
- * 生成一个渐变颜色
65
- * @param left
66
- * @param color
67
- * @returns
68
- */
69
- export const genGradientPoint = (left: number, color: string, id?: string): GradientColorPoint => ({
70
- id: id || genId(),
71
- left,
72
- color
73
- });
74
-
75
- export class Color {
76
- states: ColorStates = {
77
- s: 100,
78
- v: 100,
79
- h: 100,
80
- a: 1
81
- };
82
-
83
- originColor: string;
84
-
85
- isGradient: boolean;
86
-
87
- gradientStates: GradientStates = {
88
- colors: [],
89
- degree: 0,
90
- selectedId: null,
91
- css: ""
92
- };
93
-
94
- constructor(input: string) {
95
- this.update(input);
96
- }
97
-
98
- update(input: string) {
99
- if (input === this.originColor) return;
100
- const gradientColors = parseGradientString(input);
101
-
102
- if (this.isGradient && !gradientColors) {
103
- /* case 1: 渐变模式单独修改某个位置点的色值 */
104
- const colorHsv = tinyColor(input).toHsv();
105
- this.states = colorHsv;
106
- this.updateCurrentGradientColor();
107
- return;
108
- }
109
- this.originColor = input;
110
- this.isGradient = false;
111
- let colorInput = input;
112
-
113
- /* case 2: 修改整个渐变,生成一套新的颜色点 */
114
- if (gradientColors) {
115
- this.isGradient = true;
116
- const object = gradientColors as GradientColors;
117
- const points = object.points.map((c, index) =>
118
- genGradientPoint(c.left, c.color, this.gradientStates.colors[index]?.id)
119
- );
120
- this.gradientStates = {
121
- colors: points,
122
- degree: object.degree,
123
- selectedId: this.gradientStates.selectedId || points[0]?.id || null
124
- };
125
- this.gradientStates.css = this.linearGradient;
126
- colorInput = this.gradientSelectedPoint?.color;
127
- }
128
-
129
- this.updateStates(colorInput);
130
- }
131
-
132
- get saturation() {
133
- return this.states.s;
134
- }
135
-
136
- set saturation(value) {
137
- this.states.s = Math.max(0, Math.min(100, value));
138
- this.updateCurrentGradientColor();
139
- }
140
-
141
- get value() {
142
- return this.states.v;
143
- }
144
-
145
- set value(value) {
146
- this.states.v = Math.max(0, Math.min(100, value));
147
- this.updateCurrentGradientColor();
148
- }
149
-
150
- get hue() {
151
- return this.states.h;
152
- }
153
-
154
- set hue(value) {
155
- this.states.h = Math.max(0, Math.min(360, value));
156
- this.updateCurrentGradientColor();
157
- }
158
-
159
- get alpha() {
160
- return this.states.a;
161
- }
162
-
163
- set alpha(value) {
164
- this.states.a = Math.max(0, Math.min(1, Math.round(value * 100) / 100));
165
- this.updateCurrentGradientColor();
166
- }
167
-
168
- get rgb() {
169
- const { r, g, b } = hsv2rgba(this.states);
170
- return `rgb(${mathRound(r)}, ${mathRound(g)}, ${mathRound(b)})`;
171
- }
172
-
173
- get rgba() {
174
- const { r, g, b, a } = hsv2rgba(this.states);
175
- return `rgba(${mathRound(r)}, ${mathRound(g)}, ${mathRound(b)}, ${a})`;
176
- }
177
-
178
- get hsv() {
179
- const { h, s, v } = this.getHsva();
180
- return `hsv(${h}, ${s}%, ${v}%)`;
181
- }
182
-
183
- get hsva() {
184
- const { h, s, v, a } = this.getHsva();
185
- return `hsva(${h}, ${s}%, ${v}%, ${a})`;
186
- }
187
-
188
- get hsl() {
189
- const { h, s, l } = this.getHsla();
190
- return `hsl(${h}, ${s}%, ${l}%)`;
191
- }
192
-
193
- get hsla() {
194
- const { h, s, l, a } = this.getHsla();
195
- return `hsla(${h}, ${s}%, ${l}%, ${a})`;
196
- }
197
-
198
- get hex() {
199
- return tinyColor(this.states).toHexString();
200
- }
201
-
202
- get hex8() {
203
- return tinyColor(this.states).toHex8String();
204
- }
205
-
206
- get cmyk() {
207
- const { c, m, y, k } = this.getCmyk();
208
- return `cmyk(${c}, ${m}, ${y}, ${k})`;
209
- }
210
-
211
- get css() {
212
- if (this.isGradient) {
213
- return this.linearGradient;
214
- }
215
- return this.rgba;
216
- }
217
-
218
- get linearGradient() {
219
- const { gradientColors, gradientDegree } = this;
220
- return gradientColors2string({
221
- points: gradientColors,
222
- degree: gradientDegree
223
- });
224
- }
225
-
226
- get gradientColors() {
227
- return this.gradientStates.colors;
228
- }
229
-
230
- set gradientColors(colors: GradientColorPoint[]) {
231
- this.gradientStates.colors = colors;
232
- this.gradientStates.css = this.linearGradient;
233
- }
234
-
235
- get gradientSelectedId() {
236
- return this.gradientStates.selectedId;
237
- }
238
-
239
- set gradientSelectedId(id: string) {
240
- if (id === this.gradientSelectedId) {
241
- return;
242
- }
243
- this.gradientStates.selectedId = id;
244
- this.updateStates(this.gradientSelectedPoint?.color);
245
- }
246
-
247
- get gradientDegree() {
248
- return this.gradientStates.degree;
249
- }
250
-
251
- set gradientDegree(degree: number) {
252
- this.gradientStates.degree = Math.max(0, Math.min(360, degree));
253
- this.gradientStates.css = this.linearGradient;
254
- }
255
-
256
- get gradientSelectedPoint() {
257
- const { gradientColors, gradientSelectedId } = this;
258
- return gradientColors.find((color) => color.id === gradientSelectedId);
259
- }
260
-
261
- getFormatsColorMap() {
262
- return {
263
- HEX: this.hex,
264
- CMYK: this.cmyk,
265
- RGB: this.rgb,
266
- RGBA: this.rgba,
267
- HSL: this.hsl,
268
- HSLA: this.hsla,
269
- HSV: this.hsv,
270
- HSVA: this.hsva,
271
- CSS: this.css,
272
- HEX8: this.hex8
273
- };
274
- }
275
-
276
- getFormattedColor(format: ColorFormat, enableAlpha: boolean) {
277
- if (this.isGradient) return this.linearGradient;
278
- const finalFormat = (
279
- enableAlpha && format in ALPHA_FORMAT_MAP ? ALPHA_FORMAT_MAP[format as AlphaConvertibleFormat] : format
280
- ) as keyof ReturnType<Color["getFormatsColorMap"]>;
281
- return this.getFormatsColorMap()[finalFormat];
282
- }
283
-
284
- updateCurrentGradientColor() {
285
- const { isGradient, gradientColors, gradientSelectedId } = this;
286
- const { length } = gradientColors;
287
- const current = this.gradientSelectedPoint;
288
- if (!isGradient || length === 0 || !current) {
289
- return false;
290
- }
291
- const index = gradientColors.findIndex((color) => color.id === gradientSelectedId);
292
- const newColor = {
293
- ...current,
294
- color: this.rgba
295
- };
296
- gradientColors.splice(index, 1, newColor);
297
- this.gradientColors = gradientColors.slice();
298
- return this;
299
- }
300
-
301
- updateStates(input: string) {
302
- const color = tinyColor(cmykInputToColor(input));
303
- const hsva = color.toHsv();
304
- this.states = hsva;
305
- }
306
-
307
- getRgba() {
308
- const { r, g, b, a } = hsv2rgba(this.states);
309
- return {
310
- r: mathRound(r),
311
- g: mathRound(g),
312
- b: mathRound(b),
313
- a
314
- };
315
- }
316
-
317
- getCmyk() {
318
- const { r, g, b } = this.getRgba();
319
- const [c, m, y, k] = rgb2cmyk(r, g, b);
320
- return {
321
- c: mathRound(c * 100),
322
- m: mathRound(m * 100),
323
- y: mathRound(y * 100),
324
- k: mathRound(k * 100)
325
- };
326
- }
327
-
328
- getHsva(): tinyColor.ColorFormats.HSVA {
329
- let { h, s, v, a } = hsv2hsva(this.states);
330
- h = mathRound(h);
331
- s = mathRound(s * 100);
332
- v = mathRound(v * 100);
333
- a *= 1;
334
- return {
335
- h,
336
- s,
337
- v,
338
- a
339
- };
340
- }
341
-
342
- getHsla(): tinyColor.ColorFormats.HSLA {
343
- let { h, s, l, a } = hsv2hsla(this.states);
344
- h = mathRound(h);
345
- s = mathRound(s * 100);
346
- l = mathRound(l * 100);
347
- a *= 1;
348
- return {
349
- h,
350
- s,
351
- l,
352
- a
353
- };
354
- }
355
-
356
- /**
357
- * 判断输入色是否与当前色相同
358
- */
359
- equals(color: string): boolean {
360
- return tinyColor.equals(this.rgba, color);
361
- }
362
-
363
- /**
364
- * 校验输入色是否是一个有效颜色
365
- * @param color
366
- * @returns
367
- */
368
- static isValid(color: string): boolean {
369
- if (parseGradientString(color)) {
370
- return true;
371
- }
372
- return tinyColor(color).isValid();
373
- }
374
-
375
- /**
376
- * 对象转颜色字符串
377
- */
378
- static object2color(object: any, format: ColorFormat) {
379
- if (format === "CMYK") {
380
- const { c, m, y, k } = object;
381
- return `cmyk(${c}, ${m}, ${y}, ${k})`;
382
- }
383
-
384
- if (format === "RGB" || format === "RGBA") {
385
- return tinyColor(object).toRgbString();
386
- }
387
-
388
- if (format === "HSL" || format === "HSLA") {
389
- return tinyColor(object).toHslString();
390
- }
391
-
392
- if (format === "HSV" || format === "HSVA") {
393
- return tinyColor(object).toHsvString();
394
- }
395
-
396
- return tinyColor(object).toHexString();
397
- }
398
-
399
- /**
400
- * 是否是渐变色
401
- * @param input
402
- * @returns
403
- */
404
- static isGradientColor = (input: string) => !!isGradientColor(input);
405
-
406
- /**
407
- * 比较两个颜色是否相同
408
- * @param color1
409
- * @param color2
410
- * @returns
411
- */
412
- static compare = (color1: string, color2: string): boolean => {
413
- const isGradientColor1 = Color.isGradientColor(color1);
414
- const isGradientColor2 = Color.isGradientColor(color2);
415
- if (isGradientColor1 && isGradientColor2) {
416
- const gradientStr1 = parseGradientString(color1);
417
- const gradientStr2 = parseGradientString(color2);
418
- if (!gradientStr1 || !gradientStr2) return false;
419
-
420
- const gradientColor1 = gradientColors2string(gradientStr1);
421
- const gradientColor2 = gradientColors2string(gradientStr2);
422
- return gradientColor1 === gradientColor2;
423
- }
424
- if (!isGradientColor1 && !isGradientColor2) {
425
- return tinyColor.equals(color1, color2);
426
- }
427
- return false;
428
- };
429
- }
430
-
431
- const COLOR_OBJECT_OUTPUT_KEYS = [
432
- "alpha",
433
- "css",
434
- "hex",
435
- "hex8",
436
- "hsl",
437
- "hsla",
438
- "hsv",
439
- "hsva",
440
- "rgb",
441
- "rgba",
442
- "saturation",
443
- "value",
444
- "isGradient"
445
- ];
446
-
447
- /**
448
- * 获取对外输出的color对象
449
- * @param color
450
- * @returns
451
- */
452
- export const getColorObject = (color: Color): ColorObject | null => {
453
- if (!color) {
454
- return null;
455
- }
456
- console.log(color);
457
- const colorObject = Object.create(null);
458
- COLOR_OBJECT_OUTPUT_KEYS.forEach((key) => {
459
- colorObject[key] = (color as { [key: string]: any })[key];
460
- });
461
- if (color.isGradient) {
462
- colorObject.linearGradient = color.linearGradient;
463
- }
464
- return colorObject;
465
- };
466
-
467
- export default Color;
1
+ import tinyColor from "tinycolor2";
2
+ import { cmykInputToColor, rgb2cmyk } from "./cmyk";
3
+ import { ALPHA_FORMAT_MAP } from "./constants";
4
+ import { GradientColorPoint, GradientColors, isGradientColor, parseGradientString } from "./gradient";
5
+ import type { AlphaConvertibleFormat, ColorFormat } from "./types";
6
+
7
+ export interface ColorObject {
8
+ alpha: number;
9
+ css: string;
10
+ hex: string;
11
+ hex8: string;
12
+ hsl: string;
13
+ hsla: string;
14
+ hsv: string;
15
+ hsva: string;
16
+ rgb: string;
17
+ rgba: string;
18
+ saturation: number;
19
+ value: number;
20
+ isGradient: boolean;
21
+ linearGradient?: string;
22
+ }
23
+
24
+ interface ColorStates {
25
+ s: number;
26
+ v: number;
27
+ h: number;
28
+ a: number;
29
+ }
30
+
31
+ interface GradientStates {
32
+ colors: GradientColorPoint[];
33
+ degree: number;
34
+ selectedId: string;
35
+ css?: string;
36
+ }
37
+
38
+ const mathRound = Math.round;
39
+ const hsv2rgba = (states: ColorStates): tinyColor.ColorFormats.RGBA => tinyColor(states).toRgb();
40
+ const hsv2hsva = (states: ColorStates): tinyColor.ColorFormats.HSVA => tinyColor(states).toHsv();
41
+ const hsv2hsla = (states: ColorStates): tinyColor.ColorFormats.HSLA => tinyColor(states).toHsl();
42
+
43
+ /**
44
+ * 将渐变对象转换成字符串
45
+ */
46
+ export const gradientColors2string = (object: GradientColors): string => {
47
+ const { points, degree } = object;
48
+ const colorsStop = points
49
+ .sort((pA, pB) => pA.left - pB.left)
50
+ .map((p) => `${p.color} ${Math.round(p.left * 100) / 100}%`);
51
+
52
+ return `linear-gradient(${degree}deg,${colorsStop.join(",")})`;
53
+ };
54
+
55
+ /**
56
+ * 去除颜色的透明度
57
+ */
58
+ export const getColorWithoutAlpha = (color: string) => tinyColor(color).setAlpha(1).toHexString();
59
+
60
+ // 生成一个随机ID
61
+ export const genId = () => (1 + Math.random() * 4294967295).toString(16);
62
+
63
+ /**
64
+ * 生成一个渐变颜色
65
+ * @param left
66
+ * @param color
67
+ * @returns
68
+ */
69
+ export const genGradientPoint = (left: number, color: string, id?: string): GradientColorPoint => ({
70
+ id: id || genId(),
71
+ left,
72
+ color
73
+ });
74
+
75
+ export class Color {
76
+ states: ColorStates = {
77
+ s: 100,
78
+ v: 100,
79
+ h: 100,
80
+ a: 1
81
+ };
82
+
83
+ originColor: string;
84
+
85
+ isGradient: boolean;
86
+
87
+ gradientStates: GradientStates = {
88
+ colors: [],
89
+ degree: 0,
90
+ selectedId: null,
91
+ css: ""
92
+ };
93
+
94
+ constructor(input: string) {
95
+ this.update(input);
96
+ }
97
+
98
+ update(input: string) {
99
+ if (input === this.originColor) return;
100
+ const gradientColors = parseGradientString(input);
101
+
102
+ if (this.isGradient && !gradientColors) {
103
+ /* case 1: 渐变模式单独修改某个位置点的色值 */
104
+ const colorHsv = tinyColor(input).toHsv();
105
+ this.states = colorHsv;
106
+ this.updateCurrentGradientColor();
107
+ return;
108
+ }
109
+ this.originColor = input;
110
+ this.isGradient = false;
111
+ let colorInput = input;
112
+
113
+ /* case 2: 修改整个渐变,生成一套新的颜色点 */
114
+ if (gradientColors) {
115
+ this.isGradient = true;
116
+ const object = gradientColors as GradientColors;
117
+ const points = object.points.map((c, index) =>
118
+ genGradientPoint(c.left, c.color, this.gradientStates.colors[index]?.id)
119
+ );
120
+ this.gradientStates = {
121
+ colors: points,
122
+ degree: object.degree,
123
+ selectedId: this.gradientStates.selectedId || points[0]?.id || null
124
+ };
125
+ this.gradientStates.css = this.linearGradient;
126
+ colorInput = this.gradientSelectedPoint?.color;
127
+ }
128
+
129
+ this.updateStates(colorInput);
130
+ }
131
+
132
+ get saturation() {
133
+ return this.states.s;
134
+ }
135
+
136
+ set saturation(value) {
137
+ this.states.s = Math.max(0, Math.min(100, value));
138
+ this.updateCurrentGradientColor();
139
+ }
140
+
141
+ get value() {
142
+ return this.states.v;
143
+ }
144
+
145
+ set value(value) {
146
+ this.states.v = Math.max(0, Math.min(100, value));
147
+ this.updateCurrentGradientColor();
148
+ }
149
+
150
+ get hue() {
151
+ return this.states.h;
152
+ }
153
+
154
+ set hue(value) {
155
+ this.states.h = Math.max(0, Math.min(360, value));
156
+ this.updateCurrentGradientColor();
157
+ }
158
+
159
+ get alpha() {
160
+ return this.states.a;
161
+ }
162
+
163
+ set alpha(value) {
164
+ this.states.a = Math.max(0, Math.min(1, Math.round(value * 100) / 100));
165
+ this.updateCurrentGradientColor();
166
+ }
167
+
168
+ get rgb() {
169
+ const { r, g, b } = hsv2rgba(this.states);
170
+ return `rgb(${mathRound(r)}, ${mathRound(g)}, ${mathRound(b)})`;
171
+ }
172
+
173
+ get rgba() {
174
+ const { r, g, b, a } = hsv2rgba(this.states);
175
+ return `rgba(${mathRound(r)}, ${mathRound(g)}, ${mathRound(b)}, ${a})`;
176
+ }
177
+
178
+ get hsv() {
179
+ const { h, s, v } = this.getHsva();
180
+ return `hsv(${h}, ${s}%, ${v}%)`;
181
+ }
182
+
183
+ get hsva() {
184
+ const { h, s, v, a } = this.getHsva();
185
+ return `hsva(${h}, ${s}%, ${v}%, ${a})`;
186
+ }
187
+
188
+ get hsl() {
189
+ const { h, s, l } = this.getHsla();
190
+ return `hsl(${h}, ${s}%, ${l}%)`;
191
+ }
192
+
193
+ get hsla() {
194
+ const { h, s, l, a } = this.getHsla();
195
+ return `hsla(${h}, ${s}%, ${l}%, ${a})`;
196
+ }
197
+
198
+ get hex() {
199
+ return tinyColor(this.states).toHexString();
200
+ }
201
+
202
+ get hex8() {
203
+ return tinyColor(this.states).toHex8String();
204
+ }
205
+
206
+ get cmyk() {
207
+ const { c, m, y, k } = this.getCmyk();
208
+ return `cmyk(${c}, ${m}, ${y}, ${k})`;
209
+ }
210
+
211
+ get css() {
212
+ if (this.isGradient) {
213
+ return this.linearGradient;
214
+ }
215
+ return this.rgba;
216
+ }
217
+
218
+ get linearGradient() {
219
+ const { gradientColors, gradientDegree } = this;
220
+ return gradientColors2string({
221
+ points: gradientColors,
222
+ degree: gradientDegree
223
+ });
224
+ }
225
+
226
+ get gradientColors() {
227
+ return this.gradientStates.colors;
228
+ }
229
+
230
+ set gradientColors(colors: GradientColorPoint[]) {
231
+ this.gradientStates.colors = colors;
232
+ this.gradientStates.css = this.linearGradient;
233
+ }
234
+
235
+ get gradientSelectedId() {
236
+ return this.gradientStates.selectedId;
237
+ }
238
+
239
+ set gradientSelectedId(id: string) {
240
+ if (id === this.gradientSelectedId) {
241
+ return;
242
+ }
243
+ this.gradientStates.selectedId = id;
244
+ this.updateStates(this.gradientSelectedPoint?.color);
245
+ }
246
+
247
+ get gradientDegree() {
248
+ return this.gradientStates.degree;
249
+ }
250
+
251
+ set gradientDegree(degree: number) {
252
+ this.gradientStates.degree = Math.max(0, Math.min(360, degree));
253
+ this.gradientStates.css = this.linearGradient;
254
+ }
255
+
256
+ get gradientSelectedPoint() {
257
+ const { gradientColors, gradientSelectedId } = this;
258
+ return gradientColors.find((color) => color.id === gradientSelectedId);
259
+ }
260
+
261
+ getFormatsColorMap() {
262
+ return {
263
+ HEX: this.hex,
264
+ CMYK: this.cmyk,
265
+ RGB: this.rgb,
266
+ RGBA: this.rgba,
267
+ HSL: this.hsl,
268
+ HSLA: this.hsla,
269
+ HSV: this.hsv,
270
+ HSVA: this.hsva,
271
+ CSS: this.css,
272
+ HEX8: this.hex8
273
+ };
274
+ }
275
+
276
+ getFormattedColor(format: ColorFormat, enableAlpha: boolean) {
277
+ if (this.isGradient) return this.linearGradient;
278
+ const finalFormat = (
279
+ enableAlpha && format in ALPHA_FORMAT_MAP ? ALPHA_FORMAT_MAP[format as AlphaConvertibleFormat] : format
280
+ ) as keyof ReturnType<Color["getFormatsColorMap"]>;
281
+ return this.getFormatsColorMap()[finalFormat];
282
+ }
283
+
284
+ updateCurrentGradientColor() {
285
+ const { isGradient, gradientColors, gradientSelectedId } = this;
286
+ const { length } = gradientColors;
287
+ const current = this.gradientSelectedPoint;
288
+ if (!isGradient || length === 0 || !current) {
289
+ return false;
290
+ }
291
+ const index = gradientColors.findIndex((color) => color.id === gradientSelectedId);
292
+ const newColor = {
293
+ ...current,
294
+ color: this.rgba
295
+ };
296
+ gradientColors.splice(index, 1, newColor);
297
+ this.gradientColors = gradientColors.slice();
298
+ return this;
299
+ }
300
+
301
+ updateStates(input: string) {
302
+ const color = tinyColor(cmykInputToColor(input));
303
+ const hsva = color.toHsv();
304
+ this.states = hsva;
305
+ }
306
+
307
+ getRgba() {
308
+ const { r, g, b, a } = hsv2rgba(this.states);
309
+ return {
310
+ r: mathRound(r),
311
+ g: mathRound(g),
312
+ b: mathRound(b),
313
+ a
314
+ };
315
+ }
316
+
317
+ getCmyk() {
318
+ const { r, g, b } = this.getRgba();
319
+ const [c, m, y, k] = rgb2cmyk(r, g, b);
320
+ return {
321
+ c: mathRound(c * 100),
322
+ m: mathRound(m * 100),
323
+ y: mathRound(y * 100),
324
+ k: mathRound(k * 100)
325
+ };
326
+ }
327
+
328
+ getHsva(): tinyColor.ColorFormats.HSVA {
329
+ let { h, s, v, a } = hsv2hsva(this.states);
330
+ h = mathRound(h);
331
+ s = mathRound(s * 100);
332
+ v = mathRound(v * 100);
333
+ a *= 1;
334
+ return {
335
+ h,
336
+ s,
337
+ v,
338
+ a
339
+ };
340
+ }
341
+
342
+ getHsla(): tinyColor.ColorFormats.HSLA {
343
+ let { h, s, l, a } = hsv2hsla(this.states);
344
+ h = mathRound(h);
345
+ s = mathRound(s * 100);
346
+ l = mathRound(l * 100);
347
+ a *= 1;
348
+ return {
349
+ h,
350
+ s,
351
+ l,
352
+ a
353
+ };
354
+ }
355
+
356
+ /**
357
+ * 判断输入色是否与当前色相同
358
+ */
359
+ equals(color: string): boolean {
360
+ return tinyColor.equals(this.rgba, color);
361
+ }
362
+
363
+ /**
364
+ * 校验输入色是否是一个有效颜色
365
+ * @param color
366
+ * @returns
367
+ */
368
+ static isValid(color: string): boolean {
369
+ if (parseGradientString(color)) {
370
+ return true;
371
+ }
372
+ return tinyColor(color).isValid();
373
+ }
374
+
375
+ /**
376
+ * 对象转颜色字符串
377
+ */
378
+ static object2color(object: any, format: ColorFormat) {
379
+ if (format === "CMYK") {
380
+ const { c, m, y, k } = object;
381
+ return `cmyk(${c}, ${m}, ${y}, ${k})`;
382
+ }
383
+
384
+ if (format === "RGB" || format === "RGBA") {
385
+ return tinyColor(object).toRgbString();
386
+ }
387
+
388
+ if (format === "HSL" || format === "HSLA") {
389
+ return tinyColor(object).toHslString();
390
+ }
391
+
392
+ if (format === "HSV" || format === "HSVA") {
393
+ return tinyColor(object).toHsvString();
394
+ }
395
+
396
+ return tinyColor(object).toHexString();
397
+ }
398
+
399
+ /**
400
+ * 是否是渐变色
401
+ * @param input
402
+ * @returns
403
+ */
404
+ static isGradientColor = (input: string) => !!isGradientColor(input);
405
+
406
+ /**
407
+ * 比较两个颜色是否相同
408
+ * @param color1
409
+ * @param color2
410
+ * @returns
411
+ */
412
+ static compare = (color1: string, color2: string): boolean => {
413
+ const isGradientColor1 = Color.isGradientColor(color1);
414
+ const isGradientColor2 = Color.isGradientColor(color2);
415
+ if (isGradientColor1 && isGradientColor2) {
416
+ const gradientStr1 = parseGradientString(color1);
417
+ const gradientStr2 = parseGradientString(color2);
418
+ if (!gradientStr1 || !gradientStr2) return false;
419
+
420
+ const gradientColor1 = gradientColors2string(gradientStr1);
421
+ const gradientColor2 = gradientColors2string(gradientStr2);
422
+ return gradientColor1 === gradientColor2;
423
+ }
424
+ if (!isGradientColor1 && !isGradientColor2) {
425
+ return tinyColor.equals(color1, color2);
426
+ }
427
+ return false;
428
+ };
429
+ }
430
+
431
+ const COLOR_OBJECT_OUTPUT_KEYS = [
432
+ "alpha",
433
+ "css",
434
+ "hex",
435
+ "hex8",
436
+ "hsl",
437
+ "hsla",
438
+ "hsv",
439
+ "hsva",
440
+ "rgb",
441
+ "rgba",
442
+ "saturation",
443
+ "value",
444
+ "isGradient"
445
+ ];
446
+
447
+ /**
448
+ * 获取对外输出的color对象
449
+ * @param color
450
+ * @returns
451
+ */
452
+ export const getColorObject = (color: Color): ColorObject | null => {
453
+ if (!color) {
454
+ return null;
455
+ }
456
+ console.log(color);
457
+ const colorObject = Object.create(null);
458
+ COLOR_OBJECT_OUTPUT_KEYS.forEach((key) => {
459
+ colorObject[key] = (color as { [key: string]: any })[key];
460
+ });
461
+ if (color.isGradient) {
462
+ colorObject.linearGradient = color.linearGradient;
463
+ }
464
+ return colorObject;
465
+ };
466
+
467
+ export default Color;