在日常的网页开发中,经常需要进行颜色数值获取、转换,例如获取红色,获取蓝色,获取绿色,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 的您一份参考。

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

Java学习-041-颜色工具类(RGB,HEX)的更多相关文章

  1. Java学习-049-正则工具类

    自去年九月份决定再次入学和职业资格进阶,开始备战二者考试至今,以及当下进行中的职称申请,犹如孤独的狼,不断前行在路上,而今凡凡总总的已历8月... 不感慨了,如下为一园友需要的正则工具类,直接上码: ...

  2. Java学习:数组工具类Arrays

    数组工具类Arrays java.util.Arrays是一个与数组相关的工具类,里面提供了大量的静态方法,用来实现数组常见的操作. public static String toString(数组) ...

  3. java学习_文件工具类

    工具类里面的方法全部都是静态的,调用的时候不需要实例化

  4. Java学习关于随机数工具类--Random类

    Random类是伪随机数生成器.之所以称为伪随机数(pseudorandom),是因为它们只是简单的均匀分布序列.Random类定义了以下构造函数: Random() Random(long seed ...

  5. 我的Java开发学习之旅------>工具类:Java获取字符串和文件进行MD5值

    ps:这几天本人用百度云盘秒传了几部大片到云盘上,几个G的文件瞬秒竟然显示"上传成功"!这真让我目瞪口呆,要是这样的话,那得多快的网速,这绝对是不可能的,也许这仅是个假象.百度了一 ...

  6. Rhino+envjs-1.2.js 在java运行网站js 工具类

    java爬虫遇到个页面加密的东西,找了些资料学习学习 做了个java运行js的工具类,希望对大家有用,其中用到client(获取js)可以自行换成自己的client.主要是用了 Rhino就是Java ...

  7. java 二进制数字符串转换工具类

    java 二进制数字符串转换工具类 将二进制转换成八进制 将二进制转换成十进制 将二进制转换成十六进制 将十进制转换成二进制 package com.iteye.injavawetrust.ad; i ...

  8. Java 后台验证的工具类

    Java 后台验证的工具类 public class ValidationUtil {         //手机号     public static String mobile = "^( ...

  9. Redis 工具类 java 实现的redis 工具类

    最近了解了一下非关系型数据库 redis 会使用简单的命令 在自己本地电脑 使用时必须先启动服务器端 在启动客户端 redis 简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内 ...

随机推荐

  1. python 代码片段25

    #coding=utf-8 # 虽然python是面向对象的语言,但是没有显式的构造函数概念. # python没有new关键词 class MyClass(object): pass m=MyCla ...

  2. XIII Open Cup named after E.V. Pankratiev. GP of SPb

    A. Graph Coloring 答案为$1$很好判,为$2$只需要二分图染色,对于$3$,首先爆搜哪些边要染成第$3$种颜色,然后二分图染色判定即可. B. Decimal Fraction 枚举 ...

  3. 图解Storm

    问题导读:1.你认为什么图形可以显示hadoop与storm的区别?(电梯)2.本文是如何形象讲解hadoop与storm的?(离线批量处理.实时流式处理)3.hadoop map/reduce对应s ...

  4. odeforces Beta Round #77 (Div. 2 Only)

    A. Football time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  5. js两个小技巧【看到了就记录一下】

    1.不声明第三个变量实现交换 ,b=; a=[b,b=a][];//执行完这句代码之后 a的值为2 b的值为1了 2.&&和||的用法 (学会了立马感觉高大尚了吧) ; //传统if语 ...

  6. Codeforces Round #215 (Div. 2) A. Sereja and Coat Rack

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

  7. 【noiOJ】p8210

    10:河中跳房子 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一个岩石跳到另一个岩石.这项激动人心 ...

  8. Android -- TextView (3)

    1.效果图

  9. Linux_查看linux并发连接数

    1.查看Web服务器(Nginx Apache)的并发请求数及其TCP连接状态:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a ...

  10. python 之redis

    redis是一个key-value存储系统,与memcached类似,它支持存储到value类型相对更多,包括string(字符串),list(列表),set(集合),zset(sorted set ...