Android 颜色透明度换算

xiaoxiao2021-02-28  84

Android 颜色透明度换算

 发表于 2016-03-11

文章目录 1. 简介 1.1. 颜色1.2. 透明度 2. 换算3. 参考

简介

颜色

Android中的颜色值通常遵循RGB/ARGB标准,使用时通常以“#”字符开头,以16进制表示。 常用的颜色值格式为:

1 2 3 4 #RGB #ARGB #RRGGBB #AARRGGBB

其中,ARGB 依次代表透明度(alpha)、红色(red)、绿色(green)、蓝色(blue)。 以颜色值 #FF99CC00 为例,其中,FF 是透明度,99 是红色值, CC 是绿色值, 00 是蓝色值。

透明度

透明度分为256阶(0-255),计算机上用16进制表示为(00-ff)。透明就是0阶,不透明就是255阶,如果50%透明就是127阶(256的一半当然是128,但因为是从0开始,所以实际上是127)。透明度 和 不透明度 是两个概念, 它们加起来是1,或者100%.ARGB 中的透明度alpha,表示的是不透明度。依据来自维基百科中的定义。

The alpha channel is normally used as an opacity channel. If a pixel has a value of 0% in its alpha channel, it is fully transparent (and, thus, invisible), whereas a value of 100% in the alpha channel gives a fully opaque pixel (traditional digital images). Values between 0% and 100% make it possible for pixels to show through a background like a glass, an effect not possible with simple binary (transparent or opaque) transparency. It allows easy image compositing.

换算

在开发过程中,UI/UE给的标注图上,所有颜色值是RGB,但是透明度经常都是百分比,例如:颜色值:#FFFFFF,透明度40%。 使用过程中我们需要进行换算。以之前的值为例,换算过程如下:

将透明度转换成不透明度(转换方式参考“透明度”,第2条) 。 不透明度为60%不透明度乘以255。 我们得到结果:153将计算结果转换成16进制。得到最终的不透明度:99将不透明度和颜色值拼接成ARGB格式。得到最终的颜色值: #99FFFFFF

简单的换算,可以先将透明度,转换成不透明度,再根据下面的表格进行对应。

100% — FF 95% — F2 90% — E6 85% — D9 80% — CC 75% — BF 70% — B3 65% — A6 60% — 99 55% — 8C 50% — 80 45% — 73 40% — 66 35% — 59 30% — 4D 25% — 40 20% — 33 15% — 26 10% — 1A 5% — 0D 0% — 00

参考

Color標示色碼的方法RGBA color spaceUnderstanding colors in AndroidHow to make a background transparent 20% in android
转载请注明原文地址: https://www.6miu.com/read-84716.html

最新回复(0)