转自:https://blog.csdn.net/ezconn/article/details/90052114
颜色简介
Android中的颜色值通常遵循RGB/ARGB标准,使用时通常以“#”字符开头,以16进制表示。
常用的颜色值格式为:
#RGB
#ARGB
#RRGGBB
#AARRGGBB
其中,ARGB 依次代表透明度(alpha)、红色(red)、绿色(green)、蓝色(blue)。以颜色值 #FF66CC99 为例,其中,FF 是透明度,66 是红色值, CC 是绿色值, 99是蓝色值。
1、透明度的计算
a,透明度分为256个等级,即 0 - 256,0就是透明,255就是不透明,透明度 0(透明) –> 255(不透明) 对应着16进制 00(透明) –> FF(不透明),比如:50%透明度;50%也就是256的一半即128,因为是从0开始算的,所以是 127,转换成16进制就是 7F
b,透明度 和 不透明度 是两个概念, 它们加起来是1,或者100%.
c,ARGB 中的透明度alpha,表示的是不透明度。
2、透明度和不透明度的转换
需要不透明度值的,可以自己算:透明度值 + 不透明度值 = 100%
如果UI给的视觉稿标注是:颜色#FFFFFF,透明度40% 。那你的计算方式应该是:
a,将透明度转换成不透明度。不透明度为:60%
b,不透明度乘以255。 我们得到结果:153
c,将计算结果转换成16进制。得到最终的不透明度:99
d,将不透明度和颜色值拼接成ARGB格式。得到最终的颜色值: #99FFFFFF
所以,UI要的颜色是:#99FFFFFF
3、透明度对应的十六进制值
//java代码生成的对应表
for (int i = 100; i>=0; i--) {
double j = (i / 100.0d);
int alpha = (int) Math.round(255-j * 255);
String hex = Integer.toHexString(alpha).toUpperCase();
if (hex.length() == 1) hex = "0" + hex;
int percent = (int) (j*100);
System.out.println(String.format("%d%% — %s", percent, hex));
}
透明度 | 16进制表示 | 透明度 | 16进制表示 | 透明度 | 16进制表示 |
---|---|---|---|---|---|
100% | 00(全透明) | 66% | 57 | 32% | AD |
99% | 03 | 65% | 59 | 31% | B0 |
98% | 05 | 64% | 5C | 30% | B3 |
97% | 07 | 63% | 5E | 29% | B5 |
96% | 0A | 62% | 61 | 28% | B8 |
95% | 0D | 61% | 63 | 27% | BA |
94% | 0F | 60% | 66 | 26% | BD |
93% | 12 | 59% | 69 | 25% | BF |
92% | 14 | 58% | 6B | 24% | C2 |
91% | 17 | 57% | 6E | 23% | C4 |
90% | 1A | 56% | 70 | 22% | C7 |
89% | 1C | 55% | 73 | 21% | C9 |
88% | 1E | 54% | 75 | 20% | CC |
87% | 21 | 53% | 78 | 19% | CF |
86% | 24 | 52% | 7A | 18% | D1 |
85% | 26 | 51% | 7D | 17% | D4 |
84% | 29 | 50% | 80 | 16% | D6 |
83% | 2B | 49% | 82 | 15% | D9 |
82% | 2E | 48% | 85 | 14% | DB |
81% | 30 | 47% | 87 | 13% | DE |
80% | 33 | 46% | 8A | 12% | E0 |
79% | 36 | 45% | 8C | 11% | E3 |
78% | 38 | 44% | 8F | 10% | E6 |
77% | 3B | 43% | 91 | 9% | E8 |
76% | 3D | 42% | 94 | 8% | EB |
75% | 40 | 41% | 96 | 7% | ED |
74% | 42 | 40% | 99 | 6% | F0 |
73% | 45 | 39% | 9C | 5% | F2 |
72% | 47 | 38% | 9E | 4% | F5 |
71% | 4A | 37% | A1 | 3% | F7 |
70% | 4D | 36% | A3 | 2% | FA |
69% | 4F | 35% | A6 | 1% | FC |
68% | 52 | 34% | A8 | 0% | FF |
67% | 54 | 33% | AB |
4、不透明度对应的十六进制值
for (double i = 1; i >= 0; i -= 0.01) {
i = Math.round(i * 100) / 100.0d;
int alpha = (int) Math.round(i * 255);
String hex = Integer.toHexString(alpha).toUpperCase();
if (hex.length() == 1) hex = "0" + hex;
int percent = (int) (i * 100);
System.out.println(String.format("%d%% — %s", percent, hex));
}
不透明度—十六进制值
不透明度 | 16进制表示 | 不透明度 | 16进制表示 | 不透明度 | 16进制表示 |
---|---|---|---|---|---|
100% | FF(完全不透明) | 66% | A8 | 32% | 52 |
99% | FC | 65% | A6 | 31% | 4F |
98% | FA | 64% | A3 | 30% | 4D |
97% | F7 | 63% | A1 | 29% | 4A |
96% | F5 | 62% | 9E | 28% | 47 |
95% | F2 | 61% | 9C | 27% | 45 |
94% | F0 | 60% | 99 | 26% | 42 |
93% | ED | 59% | 96 | 25% | 40 |
92% | EB | 58% | 94 | 24% | 3D |
91% | E8 | 57% | 91 | 23% | 3B |
90% | E6 | 56% | 8F | 22% | 38 |
89% | E3 | 55% | 8C | 21% | 36 |
88% | E0 | 54% | 8A | 20% | 33 |
87% | DE | 53% | 87 | 19% | 30 |
86% | DB | 52% | 85 | 18% | 2E |
85% | D9 | 51% | 82 | 17% | 2B |
84% | D6 | 50% | 80 | 16% | 29 |
83% | D4 | 49% | 7D | 15% | 26 |
82% | D1 | 48% | 7A | 14% | 24 |
81% | CF | 47% | 78 | 13% | 21 |
80% | CC | 46% | 75 | 12% | 1F |
79% | C9 | 45% | 73 | 11% | 1C |
78% | C7 | 44% | 70 | 10% | 1A |
77% | C4 | 43% | 6E | 9% | 17 |
76% | C2 | 42% | 6B | 8% | 14 |
75% | BF | 41% | 69 | 7% | 12 |
74% | BD | 40% | 66 | 6% | 0F |
73% | BA | 39% | 63 | 5% | 0D |
72% | B8 | 38% | 61 | 4% | 0A |
71% | B5 | 37% | 5E | 3% | 08 |
70% | B3 | 36% | 5C | 2% | 05 |
69% | B0 | 35% | 59 | 1% | 03 |
68% | AD | 34% | 57 | 0% | 00(全透明) |
67% | AB | 33% | 54 |