ARGB颜色与int相互转换

基本原理

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;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kmblack1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值