在日常开发中,设计稿常常配置不透明度和透明度参数百分比,用在程序中需要转化成十六进制值。
一、不透明度对照表
不透明度 | 0% | 1% | 2% | 3% | 4% | 5% | 6% | 7% | 8% | 9% |
十六进制值 | 00 | 02 | 05 | 07 | 0A | 0C | 0F | 11 | 14 | 16 |
不透明度 | 10% | 11% | 12% | 13% | 14% | 15% | 16% | 17% | 18% | 19% |
十六进制值 | 19 | 1C | 1E | 21 | 23 | 26 | 28 | 2B | 2D | 30 |
不透明度 | 20% | 21% | 22% | 23% | 24% | 25% | 26% | 27% | 28% | 29% |
十六进制值 | 33 | 35 | 38 | 3A | 3D | 3F | 42 | 44 | 47 | 49 |
不透明度 | 30% | 31% | 32% | 33% | 34% | 35% | 36% | 37% | 38% | 39% |
十六进制值 | 4C | 4F | 51 | 54 | 56 | 59 | 5B | 5E | 60 | 63 |
不透明度 | 40% | 41% | 42% | 43% | 44% | 45% | 46% | 47% | 48% | 49% |
十六进制值 | 66 | 68 | 6B | 6D | 70 | 72 | 75 | 77 | 7A | 7C |
不透明度 | 50% | 51% | 52% | 53% | 54% | 55% | 56% | 57% | 58% | 59% |
十六进制值 | 7F | 82 | 84 | 87 | 89 | 8C | 8E | 91 | 93 | 96 |
不透明度 | 60% | 61% | 62% | 63% | 64% | 65% | 66% | 67% | 68% | 69% |
十六进制值 | 99 | 9B | 9E | A0 | A3 | A5 | A8 | AA | AD | AF |
不透明度 | 70% | 71% | 72% | 73% | 74% | 75% | 76% | 77% | 78% | 79% |
十六进制值 | B2 | B5 | B7 | BA | BC | BF | C1 | C4 | C6 | C9 |
不透明度 | 80% | 81% | 82% | 83% | 84% | 85% | 86% | 87% | 88% | 89% |
十六进制值 | CC | CE | D1 | D3 | D6 | D8 | DB | DD | E0 | E2 |
不透明度 | 90% | 91% | 92% | 93% | 94% | 95% | 96% | 97% | 98% | 99% | 100% |
十六进制值 | E5 | E8 | EA | ED | EF | F2 | F4 | F7 | F9 | FC | FF |
二、透明度对照表
透明度 | 0% | 1% | 2% | 3% | 4% | 5% | 6% | 7% | 8% | 9% |
十六进制值 | FF | FC | F9 | F7 | F4 | F2 | EF | ED | EA | E8 |
透明度 | 10% | 11% | 12% | 13% | 14% | 15% | 16% | 17% | 18% | 19% |
十六进制值 | E5 | E2 | E0 | DD | DB | D8 | D6 | D3 | D1 | CE |
透明度 | 20% | 21% | 22% | 23% | 24% | 25% | 26% | 27% | 28% | 29% |
十六进制值 | CC | C9 | C6 | C4 | C1 | BF | BC | BA | B7 | B5 |
透明度 | 30% | 31% | 32% | 33% | 34% | 35% | 36% | 37% | 38% | 39% |
十六进制值 | B2 | AF | AD | AA | A8 | A5 | A3 | A0 | 9E | 9B |
透明度 | 40% | 41% | 42% | 43% | 44% | 45% | 46% | 47% | 48% | 49% |
十六进制值 | 99 | 96 | 93 | 91 | 8E | 8C | 89 | 87 | 84 | 82 |
透明度 | 50% | 51% | 52% | 53% | 54% | 55% | 56% | 57% | 58% | 59% |
十六进制值 | 7F | 7C | 7A | 77 | 75 | 72 | 70 | 6D | 6B | 68 |
透明度 | 60% | 61% | 62% | 63% | 64% | 65% | 66% | 67% | 68% | 69% |
十六进制值 | 66 | 63 | 60 | 5E | 5B | 59 | 56 | 54 | 51 | 4F |
透明度 | 70% | 71% | 72% | 73% | 74% | 75% | 76% | 77% | 78% | 79% |
十六进制值 | 4C | 49 | 47 | 44 | 42 | 3F | 3D | 3A | 38 | 35 |
透明度 | 80% | 81% | 82% | 83% | 84% | 85% | 86% | 87% | 88% | 89% |
十六进制值 | 33 | 30 | 2D | 2B | 28 | 26 | 23 | 21 | 1E | 1C |
透明度 | 90% | 91% | 92% | 93% | 94% | 95% | 96% | 97% | 98% | 99% | 100% |
十六进制值 | 19 | 16 | 14 | 11 | 0F | 0C | 0A | 07 | 05 | 02 | 00 |
三、不透明度十六进制和百分比转化Java实现代码
/**
* Converts a percentage (0-100) to a two-digit hexadecimal value representing opacity.
*
* @param percentage The opacity percentage (0-100).
* @return A two-digit hexadecimal string.
*/
public static String percentageToHex(int percentage) {
if (percentage < 0 || percentage > 100) {
throw new IllegalArgumentException("Percentage must be between 0 and 100.");
}
// Calculate the equivalent value on a scale of 0 to 255
int value = (int) Math.round(percentage * 255.0 / 100.0);
// Convert to a two-digit hexadecimal string
return String.format("%02X", value);
}
/**
* Converts a two-digit hexadecimal string representing opacity to a percentage (0-100).
*
* @param hex The two-digit hexadecimal string.
* @return The opacity percentage.
*/
public static int hexToPercentage(String hex) {
if (hex == null || hex.length() != 2) {
throw new IllegalArgumentException("Hex string must be two digits long.");
}
// Convert the hex string to an integer
int value = Integer.parseInt(hex, 16);
// Calculate the equivalent percentage
int percentage = (int) Math.round(value * 100.0 / 255.0);
return percentage;
}
ARGB 用于在表达式中设置颜色属性,其中用红色成分R、绿色成分G和蓝色成分B,以及A透明度系数(不透明度)alpha 定义颜色。
例如:
全透明的颜色如下:
<color name="transparent">#00000000</color>
60%不透明白色的颜色如下:
<color name="white_60">#99FFFFFF</color>
发表回复