-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
ColorUtils.java
1275 lines (1162 loc) · 36.4 KB
/
ColorUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dev.utils.common;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import dev.utils.JCLogUtils;
/**
* detail: 颜色工具类 ( 包括常用的色值 )
* @author Ttt
* <pre>
* 颜色信息和转换工具
* @see <a href="https://zh.spycolor.com"/>
* RGB 颜色空间、色调、饱和度、亮度、HSV 颜色空间详解
* @see <a href="https://blog.csdn.net/bjbz_cxy/article/details/79701006"/>
* </pre>
*/
public final class ColorUtils {
private ColorUtils() {
}
// 日志 TAG
private static final String TAG = ColorUtils.class.getSimpleName();
// 透明
public static final int TRANSPARENT = 0x00000000;
// 白色
public static final int WHITE = 0xffffffff;
// 白色 ( 半透明 )
public static final int WHITE_TRANSLUCENT = 0x80ffffff;
// 黑色
public static final int BLACK = 0xff000000;
// 黑色 ( 半透明 )
public static final int BLACK_TRANSLUCENT = 0x80000000;
// 红色
public static final int RED = 0xffff0000;
// 红色 ( 半透明 )
public static final int RED_TRANSLUCENT = 0x80ff0000;
// 绿色
public static final int GREEN = 0xff00ff00;
// 绿色 ( 半透明 )
public static final int GREEN_TRANSLUCENT = 0x8000ff00;
// 蓝色
public static final int BLUE = 0xff0000ff;
// 蓝色 ( 半透明 )
public static final int BLUE_TRANSLUCENT = 0x800000ff;
// 灰色
public static final int GRAY = 0xff969696;
// 灰色 ( 半透明 )
public static final int GRAY_TRANSLUCENT = 0x80969696;
// 天蓝
public static final int SKYBLUE = 0xff87ceeb;
// 橙色
public static final int ORANGE = 0xffffa500;
// 金色
public static final int GOLD = 0xffffd700;
// 粉色
public static final int PINK = 0xffffc0cb;
// 紫红色
public static final int FUCHSIA = 0xffff00ff;
// 灰白色
public static final int GRAYWHITE = 0xfff2f2f2;
// 紫色
public static final int PURPLE = 0xff800080;
// 青色
public static final int CYAN = 0xff00ffff;
// 黄色
public static final int YELLOW = 0xffffff00;
// 巧克力色
public static final int CHOCOLATE = 0xffd2691e;
// 番茄色
public static final int TOMATO = 0xffff6347;
// 橙红色
public static final int ORANGERED = 0xffff4500;
// 银白色
public static final int SILVER = 0xffc0c0c0;
// 深灰色
public static final int DKGRAY = 0xFF444444;
// 亮灰色
public static final int LTGRAY = 0xFFCCCCCC;
// 洋红色
public static final int MAGENTA = 0xFFFF00FF;
// 高光
public static final int HIGHLIGHT = 0x33ffffff;
// 低光
public static final int LOWLIGHT = 0x33000000;
/*
* 0-255 十进值转换成十六进制, 如 255 就是 ff
* 255 * 0.x = 十进制 转 十六进制
* <p></p>
* 透明度 0 - 100
* 00、19、33、4C、66、7F、99、B2、CC、E5、FF
* 透明度 5 - 95
* 0D、26、40、59、73、8C、A6、BF、D9、F2
*/
static {
// 设置 Color 解析器
setParser(new ColorInfo.ColorParser());
}
/**
* 获取十六进制透明度字符串
* @param alpha 0-255
* @return 透明度 ( 十六进制 ) 值
*/
public static String hexAlpha(final int alpha) {
try {
if (alpha >= 0 && alpha <= 255) {
return Integer.toHexString(alpha);
}
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "hexAlpha");
}
return null;
}
// =
/**
* 返回一个颜色 ARGB 色值数组 ( 返回十进制 )
* @param color argb/rgb color
* @return ARGB 色值数组 { alpha, red, green, blue }
*/
public static int[] getARGB(final int color) {
int[] argb = new int[4];
if (ColorUtils.isARGB(color)) {
argb[0] = alpha(color);
} else {
argb[0] = 255;
}
argb[1] = red(color);
argb[2] = green(color);
argb[3] = blue(color);
return argb;
}
// =
/**
* 返回一个颜色中的透明度值 ( 返回十进制 )
* @param color argb color
* @return alpha 值
*/
public static int alpha(final int color) {
return color >>> 24;
}
/**
* 返回一个颜色中的透明度百分比值
* @param color argb color
* @return alpha 百分比值
*/
public static float alphaPercent(final int color) {
return NumberUtils.percentF(alpha(color), 255);
}
// =
/**
* 返回一个颜色中红色的色值 ( 返回十进制 )
* @param color argb/rgb color
* @return red 值
*/
public static int red(final int color) {
return (color >> 16) & 0xFF;
}
/**
* 返回一个颜色中红色的百分比值
* @param color argb/rgb color
* @return red 百分比值
*/
public static float redPercent(final int color) {
return NumberUtils.percentF(red(color), 255);
}
// =
/**
* 返回一个颜色中绿色的色值 ( 返回十进制 )
* @param color argb/rgb color
* @return green 值
*/
public static int green(final int color) {
return (color >> 8) & 0xFF;
}
/**
* 返回一个颜色中绿色的百分比值
* @param color argb/rgb color
* @return green 百分比值
*/
public static float greenPercent(final int color) {
return NumberUtils.percentF(green(color), 255);
}
// =
/**
* 返回一个颜色中蓝色的色值 ( 返回十进制 )
* @param color argb/rgb color
* @return blue 值
*/
public static int blue(final int color) {
return color & 0xFF;
}
/**
* 返回一个颜色中蓝色的百分比值
* @param color argb/rgb color
* @return blue 百分比值
*/
public static float bluePercent(final int color) {
return NumberUtils.percentF(blue(color), 255);
}
// =
/**
* 根据对应的 red、green、blue 生成一个颜色值
* @param red 红色值 [0-255]
* @param green 绿色值 [0-255]
* @param blue 蓝色值 [0-255]
* @return rgb 颜色值
*/
public static int rgb(
final int red,
final int green,
final int blue
) {
return 0xff000000 | (red << 16) | (green << 8) | blue;
}
/**
* 根据对应的 red、green、blue 生成一个颜色值
* @param red 红色值 [0-255]
* @param green 绿色值 [0-255]
* @param blue 蓝色值 [0-255]
* @return rgb 颜色值
*/
public static int rgb(
final float red,
final float green,
final float blue
) {
return 0xff000000 |
((int) (red * 255.0F + 0.5F) << 16) |
((int) (green * 255.0F + 0.5F) << 8) |
(int) (blue * 255.0F + 0.5F);
}
// =
/**
* 根据对应的 alpha、red、green、blue 生成一个颜色值 ( 含透明度 )
* @param alpha 透明度 [0-255]
* @param red 红色值 [0-255]
* @param green 绿色值 [0-255]
* @param blue 蓝色值 [0-255]
* @return argb 颜色值
*/
public static int argb(
final int alpha,
final int red,
final int green,
final int blue
) {
return (alpha << 24) | (red << 16) | (green << 8) | blue;
}
/**
* 根据对应的 alpha、red、green、blue 生成一个颜色值 ( 含透明度 )
* @param alpha 透明度 [0-255]
* @param red 红色值 [0-255]
* @param green 绿色值 [0-255]
* @param blue 蓝色值 [0-255]
* @return argb 颜色值
*/
public static int argb(
final float alpha,
final float red,
final float green,
final float blue
) {
return ((int) (alpha * 255.0F + 0.5F) << 24) |
((int) (red * 255.0F + 0.5F) << 16) |
((int) (green * 255.0F + 0.5F) << 8) |
(int) (blue * 255.0F + 0.5F);
}
// =
/**
* 判断颜色 RGB 是否有效
* @param color rgb color
* @return {@code true} yes, {@code false} no
*/
public static boolean isRGB(final int color) {
int red = red(color);
int green = green(color);
int blue = blue(color);
return (red <= 255 && red >= 0) &&
(green <= 255 && green >= 0) &&
(blue <= 255 && blue >= 0);
}
/**
* 判断颜色 ARGB 是否有效
* @param color argb color
* @return {@code true} yes, {@code false} no
*/
public static boolean isARGB(final int color) {
int alpha = alpha(color);
int red = red(color);
int green = green(color);
int blue = blue(color);
return (alpha <= 255 && alpha >= 0) &&
(red <= 255 && red >= 0) &&
(green <= 255 && green >= 0) &&
(blue <= 255 && blue >= 0);
}
// =
/**
* 设置透明度
* @param color argb/rgb color
* @param alpha 透明度 [0-255]
* @return argb 颜色值
*/
public static int setAlpha(
final int color,
final int alpha
) {
return (color & 0x00ffffff) | (alpha << 24);
}
/**
* 设置透明度
* @param color argb/rgb color
* @param alpha 透明度 [0-255]
* @return argb 颜色值
*/
public static int setAlpha(
final int color,
final float alpha
) {
return (color & 0x00ffffff) | ((int) (alpha * 255.0F + 0.5F) << 24);
}
/**
* 改变颜色值中的红色色值
* @param color argb/rgb color
* @param red 红色值 [0-255]
* @return argb/rgb 颜色值
*/
public static int setRed(
final int color,
final int red
) {
return (color & 0xff00ffff) | (red << 16);
}
/**
* 改变颜色值中的红色色值
* @param color argb/rgb color
* @param red 红色值 [0-255]
* @return argb/rgb 颜色值
*/
public static int setRed(
final int color,
final float red
) {
return (color & 0xff00ffff) | ((int) (red * 255.0F + 0.5F) << 16);
}
/**
* 改变颜色值中的绿色色值
* @param color argb/rgb color
* @param green 绿色值 [0-255]
* @return argb/rgb 颜色值
*/
public static int setGreen(
final int color,
final int green
) {
return (color & 0xffff00ff) | (green << 8);
}
/**
* 改变颜色值中的绿色色值
* @param color argb/rgb color
* @param green 绿色值 [0-255]
* @return argb/rgb 颜色值
*/
public static int setGreen(
final int color,
final float green
) {
return (color & 0xffff00ff) | ((int) (green * 255.0F + 0.5F) << 8);
}
/**
* 改变颜色值中的蓝色色值
* @param color argb/rgb color
* @param blue 蓝色值 [0-255]
* @return argb/rgb 颜色值
*/
public static int setBlue(
final int color,
final int blue
) {
return (color & 0xffffff00) | blue;
}
/**
* 改变颜色值中的蓝色色值
* @param color argb/rgb color
* @param blue 蓝色值 [0-255]
* @return argb/rgb 颜色值
*/
public static int setBlue(
final int color,
final float blue
) {
return (color & 0xffffff00) | (int) (blue * 255.0F + 0.5F);
}
// =
/**
* 解析颜色字符串, 返回对应的颜色值
* @param colorStr argb/rgb color String
* @return argb/rgb 颜色值
*/
private static int innerParseColor(final String colorStr) {
if (colorStr.charAt(0) == '#') {
// Use a long to avoid rollovers on #ffXXXXXX
long color = Long.parseLong(colorStr.substring(1), 16);
if (colorStr.length() == 7) {
// Set the alpha value
color |= 0x00000000ff000000;
} else if (colorStr.length() != 9) {
throw new IllegalArgumentException("Unknown color");
}
return (int) color;
} else {
Integer color = sColorNameMaps.get(colorStr.toLowerCase(Locale.ROOT));
if (color != null) {
return color;
}
}
throw new IllegalArgumentException("Unknown color");
}
/**
* 解析颜色字符串, 返回对应的颜色值
* <pre>
* 支持的格式:
* #RRGGBB
* #AARRGGBB
* 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'
* </pre>
* @param colorStr argb/rgb color String
* @return argb/rgb 颜色值
*/
public static int parseColor(final String colorStr) {
if (colorStr != null) {
try {
return innerParseColor(colorStr);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "parseColor");
}
}
return Integer.MAX_VALUE;
}
/**
* 颜色值 转换 RGB 颜色字符串
* @param colorInt rgb int color
* @return rgb color String
*/
public static String intToRgbString(final int colorInt) {
try {
int color = colorInt;
color = color & 0x00ffffff;
String colorStr = Integer.toHexString(color);
while (colorStr.length() < 6) {
colorStr = "0" + colorStr;
}
return "#" + colorStr;
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "intToRgbString");
}
return null;
}
/**
* 颜色值 转换 ARGB 颜色字符串
* @param colorInt argb int color
* @return argb color String
*/
public static String intToArgbString(final int colorInt) {
try {
String colorString = Integer.toHexString(colorInt);
while (colorString.length() < 6) {
colorString = "0" + colorString;
}
while (colorString.length() < 8) {
colorString = "f" + colorString;
}
return "#" + colorString;
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "intToArgbString");
}
return null;
}
// =
/**
* 获取随机颜色值
* @return 随机颜色值
*/
public static int getRandomColor() {
return getRandomColor(true);
}
/**
* 获取随机颜色值
* @param supportAlpha 是否支持透明度
* @return argb/rgb 颜色值
*/
public static int getRandomColor(final boolean supportAlpha) {
int high = supportAlpha ? (int) (Math.random() * 0x100) << 24 : 0xFF000000;
return high | (int) (Math.random() * 0x1000000);
}
/**
* 获取随机颜色值字符串
* @return 随机颜色值
*/
public static String getRandomColorString() {
return getRandomColorString(true);
}
/**
* 获取随机颜色值字符串
* @param supportAlpha 是否支持透明度
* @return 随机颜色值
*/
public static String getRandomColorString(final boolean supportAlpha) {
if (supportAlpha) {
return intToArgbString(getRandomColor(supportAlpha));
} else {
return intToRgbString(getRandomColor(supportAlpha));
}
}
// =
/**
* 判断是否为 ARGB 格式的十六进制颜色, 例如: FF990587
* @param colorStr color String
* @return {@code true} yes, {@code false} no
*/
public static boolean judgeColorString(final String colorStr) {
if (colorStr != null && colorStr.length() == 8) {
char cc = colorStr.charAt(0);
return !(cc != '0' && cc != '1' && cc != '2' && cc != '3' && cc != '4'
&& cc != '5' && cc != '6' && cc != '7' && cc != '8' && cc != '9'
&& cc != 'A' && cc != 'B' && cc != 'C' && cc != 'D' && cc != 'E'
&& cc != 'F' && cc != 'a' && cc != 'b' && cc != 'c' && cc != 'd'
&& cc != 'e' && cc != 'f');
}
return false;
}
// =
/**
* 颜色加深 ( 单独修改 RGB 值, 不变动透明度 )
* @param colorStr color String
* @param darkValue 加深值
* @return 加深后的颜色值
*/
public static int setDark(
final String colorStr,
final int darkValue
) {
int color = parseColor(colorStr);
return setDark(color, darkValue);
}
/**
* 颜色加深 ( 单独修改 RGB 值, 不变动透明度 )
* @param color int color
* @param darkValue 加深值
* @return 加深后的颜色值
*/
public static int setDark(
final int color,
final int darkValue
) {
int red = red(color);
int green = green(color);
int blue = blue(color);
// 进行加深 ( 累减 )
red -= darkValue;
green -= darkValue;
blue -= darkValue;
// 颜色值
int colorTemp = color;
// 进行设置
colorTemp = setRed(colorTemp, NumberUtils.clamp(red, 255, 0));
colorTemp = setGreen(colorTemp, NumberUtils.clamp(green, 255, 0));
colorTemp = setBlue(colorTemp, NumberUtils.clamp(blue, 255, 0));
return colorTemp;
}
/**
* 颜色变浅, 变亮 ( 单独修改 RGB 值, 不变动透明度 )
* @param colorStr color String
* @param lightValue 变亮 ( 变浅 ) 值
* @return 变亮 ( 变浅 ) 后的颜色值
*/
public static int setLight(
final String colorStr,
final int lightValue
) {
int color = parseColor(colorStr);
return setLight(color, lightValue);
}
/**
* 颜色变浅, 变亮 ( 单独修改 RGB 值, 不变动透明度 )
* @param color int color
* @param lightValue 变亮 ( 变浅 ) 值
* @return 变亮 ( 变浅 ) 后的颜色值
*/
public static int setLight(
final int color,
final int lightValue
) {
int red = red(color);
int green = green(color);
int blue = blue(color);
// 进行变浅, 变亮 ( 累加 )
red += lightValue;
green += lightValue;
blue += lightValue;
// 颜色值
int colorTemp = color;
// 进行设置
colorTemp = setRed(colorTemp, NumberUtils.clamp(red, 255, 0));
colorTemp = setGreen(colorTemp, NumberUtils.clamp(green, 255, 0));
colorTemp = setBlue(colorTemp, NumberUtils.clamp(blue, 255, 0));
return colorTemp;
}
/**
* 设置透明度加深
* @param colorStr color String
* @param darkValue 加深值
* @return 透明度加深后的颜色值
*/
public static int setAlphaDark(
final String colorStr,
final int darkValue
) {
int color = parseColor(colorStr);
return setAlphaDark(color, darkValue);
}
/**
* 设置透明度加深
* @param color int color
* @param darkValue 加深值
* @return 透明度加深后的颜色值
*/
public static int setAlphaDark(
final int color,
final int darkValue
) {
int alpha = alpha(color);
// 透明度加深
alpha += darkValue;
// 进行设置
return setAlpha(color, NumberUtils.clamp(alpha, 255, 0));
}
/**
* 设置透明度变浅
* @param colorStr color String
* @param lightValue 变浅值
* @return 透明度变浅后的颜色值
*/
public static int setAlphaLight(
final String colorStr,
final int lightValue
) {
int color = parseColor(colorStr);
return setAlphaLight(color, lightValue);
}
/**
* 设置透明度变浅
* @param color int color
* @param lightValue 变浅值
* @return 透明度变浅后的颜色值
*/
public static int setAlphaLight(
final int color,
final int lightValue
) {
int alpha = alpha(color);
// 透明度变浅
alpha -= lightValue;
// 进行设置
return setAlpha(color, NumberUtils.clamp(alpha, 255, 0));
}
// =
/**
* 获取灰度值
* @param colorStr color String
* @return 灰度值
*/
public static int grayLevel(final String colorStr) {
int color = parseColor(colorStr);
int[] argb = getARGB(color);
return (int) (argb[1] * 0.299F + argb[2] * 0.587F + argb[3] * 0.114F);
}
/**
* 获取灰度值
* @param color int color
* @return 灰度值
*/
public static int grayLevel(final int color) {
// [] { alpha, red, green, blue }
int[] argb = getARGB(color);
return (int) (argb[1] * 0.299F + argb[2] * 0.587F + argb[3] * 0.114F);
}
// =
// 颜色字典集合
private static final Map<String, Integer> sColorNameMaps;
static {
sColorNameMaps = new HashMap<>();
sColorNameMaps.put("transparent", TRANSPARENT);
sColorNameMaps.put("white", WHITE);
sColorNameMaps.put("black", BLACK);
sColorNameMaps.put("red", RED);
sColorNameMaps.put("green", GREEN);
sColorNameMaps.put("blue", BLUE);
sColorNameMaps.put("gray", GRAY);
sColorNameMaps.put("grey", GRAY);
sColorNameMaps.put("skyblue", SKYBLUE);
sColorNameMaps.put("orange", ORANGE);
sColorNameMaps.put("gold", GOLD);
sColorNameMaps.put("pink", PINK);
sColorNameMaps.put("fuchsia", FUCHSIA);
sColorNameMaps.put("graywhite", GRAYWHITE);
sColorNameMaps.put("purple", PURPLE);
sColorNameMaps.put("cyan", CYAN);
sColorNameMaps.put("yellow", YELLOW);
sColorNameMaps.put("chocolate", CHOCOLATE);
sColorNameMaps.put("tomato", TOMATO);
sColorNameMaps.put("orangered", ORANGERED);
sColorNameMaps.put("silver", SILVER);
sColorNameMaps.put("darkgray", DKGRAY);
sColorNameMaps.put("lightgray", LTGRAY);
sColorNameMaps.put("lightgrey", LTGRAY);
sColorNameMaps.put("magenta", MAGENTA);
sColorNameMaps.put("highlight", HIGHLIGHT);
sColorNameMaps.put("lowlight", LOWLIGHT);
sColorNameMaps.put("aqua", 0xFF00FFFF);
sColorNameMaps.put("lime", 0xFF00FF00);
sColorNameMaps.put("maroon", 0xFF800000);
sColorNameMaps.put("navy", 0xFF000080);
sColorNameMaps.put("olive", 0xFF808000);
sColorNameMaps.put("teal", 0xFF008080);
}
// ==========
// = 颜色信息 =
// ==========
// 内部解析器
private static ColorInfo.Parser sParser;
/**
* 设置 Color 解析器
* @param parser {@link ColorInfo.Parser}
*/
public static void setParser(final ColorInfo.Parser parser) {
ColorUtils.sParser = parser;
}
/**
* detail: 颜色信息
* @author Ttt
*/
public static class ColorInfo {
// key
private final String key;
// value ( 如: #000000 )
private final String value;
// value 解析后的值 ( 如: #000 => #000000 )
private String valueParser;
// ARGB/RGB color
private long valueColor;
// A、R、G、B
private int alpha = 255, red = 0, green = 0, blue = 0;
// 灰度值
private int grayLevel;
// H、S、B ( V )
private float hue, saturation, brightness;
/**
* 构造函数
* @param key Key
* @param value Value ( 如: #000000 )
*/
public ColorInfo(
final String key,
final String value
) {
this.key = key;
this.value = value;
innerConvert();
}
/**
* 构造函数
* @param key Key
* @param valueColor ARGB/RGB color
*/
public ColorInfo(
final String key,
final int valueColor
) {
this(key, ColorUtils.intToArgbString(valueColor));
}
/**
* 获取 Key
* @return key String
*/
public String getKey() {
return key;
}
/**
* 获取 Value
* @return value String
*/
public String getValue() {
return value;
}
/**
* 获取 Value 解析后的值 ( 如: #000 => #000000 )
* @return value 解析后的值 ( 如: #000 => #000000 )
*/
public String getValueParser() {
return valueParser;
}
/**
* 获取 ARGB/RGB color
* @return ARGB/RGB color
*/
public long getValueColor() {
return valueColor;
}
/**
* 返回颜色中的透明度值 ( 返回十进制 )
* @return alpha 值
*/
public int getAlpha() {
return alpha;
}
/**
* 返回颜色中红色的色值 ( 返回十进制 )
* @return red 值
*/
public int getRed() {
return red;
}
/**
* 返回颜色中绿色的色值 ( 返回十进制 )
* @return green 值
*/
public int getGreen() {
return green;
}
/**
* 返回颜色中蓝色的色值 ( 返回十进制 )
* @return blue 值
*/
public int getBlue() {
return blue;
}
/**
* 获取灰度值
* @return 灰度值
*/
public int getGrayLevel() {
return grayLevel;
}
/**
* 获取颜色色调
* @return 颜色色调
*/
public float getHue() {
return hue;
}
/**
* 获取颜色饱和度
* @return 颜色饱和度
*/
public float getSaturation() {
return saturation;
}
/**
* 获取颜色亮度
* @return 颜色亮度
*/
public float getBrightness() {
return brightness;
}
@Override
public String toString() {
return "key : " + key +
"\nvalue : " + value +
"\nvalueParser : " + valueParser +
"\nalpha : " + alpha +
"\nred : " + red +
"\ngreen : " + green +
"\nblue : " + blue +
"\ngrayLevel : " + grayLevel +
"\nintToRgbString : " + ColorUtils.intToRgbString((int) valueColor) +
"\nintToArgbString : " + ColorUtils.intToArgbString((int) valueColor);
}
// ==========
// = 内部方法 =
// ==========
/**
* 内部转换处理
*/
private void innerConvert() {
String temp = value;
if (sParser != null) {
temp = sParser.handleColor(value);
// 保存解析后的值
valueParser = temp;
}
if (temp == null) return;
// 转换 long 颜色值
valueColor = ColorUtils.parseColor(temp);
int[] argb = ColorUtils.getARGB((int) valueColor);
// 获取 ARGB
alpha = argb[0];
red = argb[1];
green = argb[2];
blue = argb[3];
// 获取灰度值
grayLevel = (int) (argb[1] * 0.299F + argb[2] * 0.587F + argb[3] * 0.114F);
// 获取 HSB
float[] hsbArrays = RGBtoHSB(red, green, blue, null);
hue = hsbArrays[0]; // 色调
saturation = hsbArrays[1]; // 饱和度
brightness = hsbArrays[2]; // 亮度
}
// ============