序
本文主要介绍下java Color对象中的alpha值。
alpha
java/awt/Color.java
/** * Creates an opaque sRGB color with the specified red, green, * and blue values in the range (0 - 255). * The actual color used in rendering depends * on finding the best match given the color space * available for a given output device. * Alpha is defaulted to 255. * * @throws IllegalArgumentException ifr
,g
* orb
are outside of the range * 0 to 255, inclusive * @param r the red component * @param g the green component * @param b the blue component * @see #getRed * @see #getGreen * @see #getBlue * @see #getRGB */ public Color(int r, int g, int b) { this(r, g, b, 255); } /** * Creates an sRGB color with the specified red, green, blue, and alpha * values in the range (0 - 255). * * @throws IllegalArgumentException ifr
,g
, *b
ora
are outside of the range * 0 to 255, inclusive * @param r the red component * @param g the green component * @param b the blue component * @param a the alpha component * @see #getRed * @see #getGreen * @see #getBlue * @see #getAlpha * @see #getRGB */ @ConstructorProperties({"red", "green", "blue", "alpha"}) public Color(int r, int g, int b, int a) { value = ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0); testColorValueRange(r,g,b,a); }
java里头的color不指定alpha的话,默认其值为255,也就是没有透明度。
opacity
color对象里头的alpha其实是指不透明度,其值范围为0-255,越大越不透明。 其通常对应opacity,这个就是单词语义表达的不透明度,其值范围[0,1.0f],值越大,越不透明。
opacity与alpha的映射
opacity与alpha之间的主要关系列表如下
100% — FF95% — F290% — E685% — D980% — CC75% — BF70% — B365% — A660% — 9955% — 8C50% — 8045% — 7340% — 6635% — 5930% — 4D25% — 4020% — 3315% — 2610% — 1A5% — 0D0% — 00
这个怎么转义呢,如下
int alpha = Math.round(opacity * 255);
再将int输出为十六进制的表示方式
String hex = Integer.toHexString(alpha).toUpperCase(); if (hex.length() == 1){ hex = "0" + hex; }
不足两位往前不零