1. 十六进制:
#ff4751
2. int 常量型:
private static final int color_text = 0xff4751;
3. 通过xml文件获取id:
<color name="test_color">#ff4751</color>
ContextCompat.getColor(this,R.color.test_color);
4. RGB表示:
255,71,81
5. 0-1浮点表示:
1.0,0.2784314,0.31764707
十进制转十六进制
private String change10To16(int color) {
StringBuffer stringBuffer = new StringBuffer();
int red = (color & 0xff0000) >> 16;
int green = (color & 0x00ff00) >> 8;
int blue = (color & 0x0000ff);
String str_red = Integer.toHexString(red);
str_red = str_red.length() < 2 ? ("0" + str_red) : str_red;
String str_green = Integer.toHexString(green);
str_green = str_green.length() < 2 ? ("0" + str_green) : str_green;
String str_blue = Integer.toHexString(blue);
str_blue = str_blue.length() < 2 ? ("0" + str_blue) : str_blue;
stringBuffer.append("#");
stringBuffer.append(str_red);
stringBuffer.append(str_green);
stringBuffer.append(str_blue);
return stringBuffer.toString();
}
十进制转RGB
private float[] change10ToRGB(int color) {
int red = (color & 0xff0000) >> 16;
int green = (color & 0x00ff00) >> 8;
int blue = (color & 0x0000ff);
float colors[] = new float[3];
colors[0] = red;
colors[1] = green;
colors[2] = blue;
return colors;
}
十进制转浮点
private float[] change10ToFloat(int color) {
int red = (color & 0xff0000) >> 16;
int green = (color & 0x00ff00) >> 8;
int blue = (color & 0x0000ff);
float colors[] = new float[3];
colors[0] = red/255f;
colors[1] = green/255f;
colors[2] = blue/255f;
return colors;
}