Color class overview
Color is used to encapsulate colors, supports multiple color spaces, and defaults to RGB color space. Each color object has an alpha channel with a value of 0 to 255, which represents transparency, and when the alpha channel value is 255, it is completely opaque, when the alpha channel value is 0 o'clock, it is fully transparent, the first three quantities do not work, and when the value of the alpha channel is a value between 0~255, Represents a different degree of transparency for a specified color.
Color constants that are predefined for the color class
Color name |
pre-defined color names |
Red Value |
Green Value |
Blue Value |
White |
White |
255 |
255 |
255 |
Light Grey |
Lightgray |
192 |
192 |
192 |
Grey |
Gray |
128 |
128 |
128 |
Dark grey |
Darkgray |
64 |
64 |
64 |
Black |
Black |
0 |
0 |
0 |
Red |
Red |
255 |
0 |
0 |
Pink |
Pink |
255 |
175 |
175 |
Orange |
Orange |
255 |
200 |
0 |
Yellow |
Yellow |
255 |
255 |
0 |
Green |
Green |
0 |
255 |
0 |
Magenta |
Magenta |
255 |
0 |
255 |
Cyan |
Cyan |
0 |
255 |
255 |
Blue |
Blue |
0 |
0 |
255 |
customizing colors with the color constructor
The constructor for color:
Color(int,int,int): 指定RGB值0~255,alpha通道为默认值255,即不透明
Color(int,int,int,int): 指定RGB、alpha通道的值,0~255
Color(int): 指定RGB值,其中int转换为16进制表示形式,0xff000000,其中ff为alpha通道,为默认值255,16~23 bit 为R,8~15 bit 为G,0~7 bit 为B
**Color(int, boolean): 如果boolean只为True,则int值转换为包含alpha通道的16进制,24~32 bit 为alpha通道值,否则alpha通道值为255
Color(float,float,float): 指定RGB值,范围0.0~1.0,alpha通道默认为1.0
Color(float,float,float,float): 指定RGB、alpha通道的值,范围为0.0~1.0
Colr(ColorSpace,float[],float): 三个参数分别为颜色空间、各通道颜色值组成的数组,alpha通道值,其中float[]数组中元素的个数取决于颜色空间,如RGB颜色空间为3,CMYK颜色空间为4
Methods for color objects
getRed(): 获取Red通道的值
getGreen(): 获取Green通道的值
getBlue(): 获取Blue通道的值
getAlpha(): 获取alpha通道的值
getRGB(): 获取颜色的RGB、alpha通道的值,转换为16进制后,24~31 bit为alpha通道,16~23 bit 为red,8~15 bit 为green通道,0~6 bit 为blue通道
brighter(): 返回一个比当前颜色浅一级的Color对象
darker(): 返回一个比当前颜色深一级的Color对象
equals(Object): 比较两个颜色对象是否颜色相同
getColor(String): 获取某个字符串的系统属性的值,所对应的颜色,返回一个Color对象,否则返回null
getColor(String,Color): 同上,如果Sring对应的系统属性值为null,则返回Color值
getColor(String,int): 同上,否则返回int值所对应的Color对象
getColorSpace(): 返回颜色空间
HSBtoRGB(float,float,float): 从HSB空间转换到RGB空间,返回RGB空间对应的int值,不含alpha通道
RGBtoHSB(): 从RGB空间转换到HSB空间,返回HSB空间对应的float值,不含alpha通道
Java.awt.Color class