基本原理
ARGB每个通道各占8bit(也就是1byte),值范围为0-255;
其中A表示alpha通道,一般设置为不透明(FF000000),因为最高位为1,所以color转换为int后int值一般为负值.
示例转换下列颜色值
- A:255
- R:150
- G:75
- B:0
转换后颜色值为-6927616
rgb转int
select
cast(x'FF000000' as int) | --alpha
(150 << 16) | --read
(75 << 8) | --green
0 --blue
@ColorInt
public static int rgb(
@IntRange(from = 0, to = 255) int red,
@IntRange(from = 0, to = 255) int green,
@IntRange(from = 0, to = 255) int blue) {
return 0xff000000 | (red << 16) | (green << 8) | blue;
}
int转rgb
select
(-6927616>>24) & cast(x'FF' as int), --alpha
(-6927616>>16) & cast(x'FF' as int), --read
(-6927616>>8) & cast(x'FF' as int), --green
(-6927616) & cast(x'FF' as int) --blue
int A = (color >> 24) & 0xff; // or color >>> 24
int R = (color >> 16) & 0xff;
int G = (color >> 8) & 0xff;
int B = (color ) & 0xff;