Java学习-041-颜色工具类(RGB,HEX)

时间:2023-11-29 08:56:44

在日常的网页开发中,经常需要进行颜色数值获取、转换,例如获取红色,获取蓝色,获取绿色,RGB转十六进制颜色,十六进制颜色转RGB等,因而在学习过程中,写了一个小工具类,仅供各位小主参考!

多不闲言,直接上码了,哈哈哈。。。

颜色工具类源码 ColorUtils.java 如下所示:

 /**
* Aaron.ffp Inc.
* Copyright (c) 2004-2016 All Rights Reserved.
*/
package cn.ffp.autotest.base.util; import org.apache.log4j.Logger; /**
* <strong>颜色工具类</strong><br>
* <ul>
* <li>颜色进制转换</li>
* <li>颜色合法性校验</li>
* </ul>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java, 2016-03-02 11:32:40.447 Exp $
*/
public class ColorUtils {
private static Logger logger = Logger.getLogger(ColorUtils.class.getName());
private static String msg = ""; private static String regHex = "^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$";
private static String regRgb = "^(RGB\\(|rgb\\()([0-9]{1,3},){2}[0-9]{1,3}\\)$";
private static String regRepRgb = "(rgb|\\(|\\)|RGB)*"; public ColorUtils() {
} /**
* <strong>颜色十六进制转颜色RGB</strong><br>
* <ul>
* <li>颜色十六进制参数不合法时,返回null</li>
* </ul>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java hex2Rgb, 2016-03-24 23:50:42.004 Exp $
*
* @param hex 颜色十六进制
* @return 颜色RGB
*/
public static String hex2Rgb(String hex) {
StringBuilder sb = new StringBuilder(); if (!ColorUtils.isHex(hex)) {
msg = "颜色十六进制格式 【" + hex + "】 不合法,请确认!";
logger.error(msg);
return null;
} String c = RegUtils.replace(hex.toUpperCase(), "#", ""); String r = Integer.parseInt((c.length() == 3 ? c.substring(0, 1) + c.substring(0, 1) : c.substring(0, 2)), 16) + "";
String g = Integer.parseInt((c.length() == 3 ? c.substring(1, 2) + c.substring(1, 2) : c.substring(2, 4)), 16) + "";
String b = Integer.parseInt((c.length() == 3 ? c.substring(2, 3) + c.substring(2, 3) : c.substring(4, 6)), 16) + ""; sb.append("RGB(" + r + "," + g + "," + b + ")"); return sb.toString();
} /**
* <strong>颜色RGB转十六进制</strong><br>
* <ul>
* <li>颜色RGB不合法,则返回null</li>
* </ul>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java rgb2Hex, 2016-03-15 23:49:33.224 Exp $
*
* @param rgb 颜色RGB
* @return 合法时返回颜色十六进制
*/
public static String rgb2Hex(String rgb) {
StringBuilder sb = new StringBuilder(); if (!ColorUtils.isRgb(rgb)) {
msg = "颜色 RGB 格式【" + rgb + "】 不合法,请确认!";
logger.error(msg);
return null;
} String r = Integer.toHexString(ColorUtils.getRed(rgb)).toUpperCase();
String g = Integer.toHexString(ColorUtils.getGreen(rgb)).toUpperCase();
String b = Integer.toHexString(ColorUtils.getBlue(rgb)).toUpperCase(); sb.append("#");
sb.append(r.length() == 1 ? "0" + r : r);
sb.append(g.length() == 1 ? "0" + g : g);
sb.append(b.length() == 1 ? "0" + b : b); return sb.toString();
} /**
* <strong>获取颜色RGB红色值</strong><br>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java getRed, 2016-03-22 23:48:50.501 Exp $
*
* @param rgb 颜色RGB
* @return 红色值
*/
public static int getRed(String rgb){
return Integer.valueOf(ColorUtils.getRGB(rgb)[0]);
} /**
* <strong>获取颜色RGB绿色值</strong><br>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java getGreen, 2016-03-22 23:48:16.290 Exp $
*
* @param rgb 颜色RGB
* @return 绿色值
*/
public static int getGreen(String rgb){
return Integer.valueOf(ColorUtils.getRGB(rgb)[1]);
} /**
* <strong>获取颜色RGB蓝色值</strong><br>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java getBlue, 2016-03-22 23:47:20.801 Exp $
*
* @param rgb 颜色RGB
* @return 蓝色数值
*/
public static int getBlue(String rgb){
return Integer.valueOf(ColorUtils.getRGB(rgb)[2]);
} /**
* <strong>获取颜色RGB数组</strong><br>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java getRGB, 2016-03-21 23:46:00.944 Exp $
*
* @param rgb 颜色RGB
* @return 颜色数组[红,绿,蓝]
*/
public static String[] getRGB(String rgb){
return RegUtils.replace(RegUtils.replaceSpace(rgb), regRepRgb, "").split(",");
} /**
* <strong>验证颜色十六进制是否合法</strong><br>
* <ul>
* <li>规则:#号开头,三位或六位字母数字组成的字符串</li>
* </ul>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java isHex, 2016-03-20 23:44:03.133 Exp $
*
* @param hex 十六进制颜色
* @return 合法则返回true
*/
public static boolean isHex(String hex) {
return RegUtils.reg(hex, regHex);
} /**
* <strong>验证颜色RGB是否合法</strong><br>
* <ul>
* <li>1、RGB符合正则表达式</li>
* <li>2、颜色值在0-255之间</li>
* </ul>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java isRgb, 2016-03-12 23:41:19.925 Exp $
*
* @param rgb 颜色RGB
* @return 是否合法,合法返回true
*/
public static boolean isRgb(String rgb) {
boolean r = ColorUtils.getRed(rgb) >= 0 && ColorUtils.getRed(rgb) <= 255;
boolean g = ColorUtils.getGreen(rgb) >= 0 && ColorUtils.getGreen(rgb) <= 255;
boolean b = ColorUtils.getBlue(rgb) >= 0 && ColorUtils.getBlue(rgb) <= 255; return ColorUtils.isRgbFormat(rgb) && r && g && b;
} /**
* <strong>验证颜色RGB是否匹配正则表达式</strong><br>
* <ul>
* <li>匹配则返回true</li>
* </ul>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java isRgbFormat, 2016-03-03 23:40:12.267 Exp $
*
* @param rgb 颜色RGB
* @return 是否匹配
*/
public static boolean isRgbFormat(String rgb) {
return RegUtils.reg(RegUtils.replaceSpace(rgb), regRgb);
}
}

颜色工具类单元测试源码 ColorUtilsTest.java 如下所示:

 package cn.ffp.autotest.base.util;

 import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; public class ColorUtilsTest {
String[][] rgb = new String[8][2];
String[][] hex = new String[11][2]; @BeforeClass
public void beforeClass() {
rgb[0][0] = "RGB(0,0,0)";
rgb[1][0] = "RGB(0,128,255)";
rgb[2][0] = "RGB(128,0,75)";
rgb[3][0] = "RGB(128,0,0)";
rgb[4][0] = "rgb(128,23,200)";
rgb[5][0] = "rgb(128, 128, 128)";
rgb[6][0] = "rgb(255, 255, 255)";
rgb[7][0] = "rgb(128, 128, 300)"; rgb[0][1] = "#000000";
rgb[1][1] = "#0080FF";
rgb[2][1] = "#80004B";
rgb[3][1] = "#800000";
rgb[4][1] = "#8017C8";
rgb[5][1] = "#808080";
rgb[6][1] = "#FFFFFF";
rgb[7][1] = null; hex[0][0] = "#000000";
hex[1][0] = "#000";
hex[2][0] = "#FAFAFA";
hex[3][0] = "#000080";
hex[4][0] = "#008080";
hex[5][0] = "#F0FF80";
hex[6][0] = "#EA00FA";
hex[7][0] = "#00EAEA";
hex[8][0] = "#876";
hex[9][0] = "#000xEA";
hex[10][0] = "#ghi"; hex[0][1] = "RGB(0,0,0)";
hex[1][1] = "RGB(0,0,0)";
hex[2][1] = "RGB(250,250,250)";
hex[3][1] = "RGB(0,0,128)";
hex[4][1] = "RGB(0,128,128)";
hex[5][1] = "RGB(240,255,128)";
hex[6][1] = "RGB(234,0,250)";
hex[7][1] = "RGB(136,119,102)";
hex[8][1] = "RGB(0,0,0)";
hex[9][1] = null;
hex[10][1] = null;
} @Test
public void getBlue() {
for (int i = 0; i < rgb.length; i++) {
System.out.println("获取蓝色:\t" + rgb[i][0] + "\t" + ColorUtils.getBlue(rgb[i][0]));
}
} @Test
public void getGreen() {
for (int i = 0; i < rgb.length; i++) {
System.out.println("获取绿色:\t" + rgb[i][0] + "\t" + ColorUtils.getGreen(rgb[i][0]));
}
} @Test
public void getRed() {
for (int i = 0; i < rgb.length; i++) {
System.out.println("获取红色:\t" + rgb[i][0] + "\t" + ColorUtils.getRed(rgb[i][0]));
}
} @Test
public void hex2Rgb() {
for (int i = 0; i < hex.length; i++) {
System.out.println("十六进制转 RGB:\t" + hex[i][0] + "\t期望结果:" + hex[i][1] + "\t实际结果:" + ColorUtils.hex2Rgb(hex[i][0]));
}
} @Test
public void isHex() {
for (int i = 0; i < hex.length; i++) {
System.out.println(hex[i][0] + "\t\t" + ColorUtils.isHex(hex[i][0]));
}
} @Test
public void isRgb() {
for (int i = 0; i < rgb.length; i++) {
System.out.println(rgb[i][0] + "\t\t" + ColorUtils.isRgb(rgb[i][0]));
}
} @Test
public void rgb2Hex() {
for (int i = 0; i < rgb.length; i++) {
System.out.println("RGB 转十六进制:\t" + rgb[i][0] + "\t期望结果:" + rgb[i][1] + "\t实际结果:" + ColorUtils.rgb2Hex(rgb[i][0]));
}
}
}

单元测试源码 ColorUtilsTest.java

至此, Java学习-041-颜色工具类(RGB,HEX) 顺利完结,希望此文能够给初学 JavaWeb 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^