zoukankan      html  css  js  c++  java
  • Java学习-041-颜色工具类(RGB,HEX)

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

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

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

      1 /**
      2  * Aaron.ffp Inc.
      3  * Copyright (c) 2004-2016 All Rights Reserved.
      4  */
      5 package cn.ffp.autotest.base.util;
      6 
      7 import org.apache.log4j.Logger;
      8 
      9 /**
     10  * <strong>颜色工具类</strong><br>
     11  * <ul>
     12  * <li>颜色进制转换</li>
     13  * <li>颜色合法性校验</li>
     14  * </ul>
     15  * <br>
     16  * @author Aaron.ffp
     17  * @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java, 2016-03-02 11:32:40.447 Exp $
     18  */
     19 public class ColorUtils {
     20     private static Logger logger = Logger.getLogger(ColorUtils.class.getName());
     21     private static String msg = "";
     22     
     23     private static String regHex = "^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$";
     24     private static String regRgb = "^(RGB\(|rgb\()([0-9]{1,3},){2}[0-9]{1,3}\)$";
     25     private static String regRepRgb = "(rgb|\(|\)|RGB)*";
     26     
     27     public ColorUtils() {
     28     }
     29     
     30     /**
     31      * <strong>颜色十六进制转颜色RGB</strong><br>
     32      * <ul>
     33      * <li>颜色十六进制参数不合法时,返回null</li>
     34      * </ul>
     35      * <br>
     36      * @author Aaron.ffp
     37      * @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java hex2Rgb, 2016-03-24 23:50:42.004 Exp $
     38      * 
     39      * @param hex 颜色十六进制
     40      * @return 颜色RGB
     41      */
     42     public static String hex2Rgb(String hex) {
     43         StringBuilder sb = new StringBuilder();
     44         
     45         if (!ColorUtils.isHex(hex)) {
     46             msg = "颜色十六进制格式 【" + hex + "】 不合法,请确认!";
     47             logger.error(msg);
     48             return null;
     49         }
     50         
     51         String c = RegUtils.replace(hex.toUpperCase(), "#", "");
     52         
     53         String r = Integer.parseInt((c.length() == 3 ? c.substring(0, 1) + c.substring(0, 1) : c.substring(0, 2)), 16) + "";
     54         String g = Integer.parseInt((c.length() == 3 ? c.substring(1, 2) + c.substring(1, 2) : c.substring(2, 4)), 16) + "";
     55         String b = Integer.parseInt((c.length() == 3 ? c.substring(2, 3) + c.substring(2, 3) : c.substring(4, 6)), 16) + "";
     56         
     57         sb.append("RGB(" + r + "," + g + "," + b + ")");
     58         
     59         return sb.toString();
     60     }
     61     
     62     /**
     63      * <strong>颜色RGB转十六进制</strong><br>
     64      * <ul>
     65      * <li>颜色RGB不合法,则返回null</li>
     66      * </ul>
     67      * <br>
     68      * @author Aaron.ffp
     69      * @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java rgb2Hex, 2016-03-15 23:49:33.224 Exp $
     70      * 
     71      * @param rgb 颜色RGB
     72      * @return 合法时返回颜色十六进制
     73      */
     74     public static String rgb2Hex(String rgb) {
     75         StringBuilder sb = new StringBuilder();
     76         
     77         if (!ColorUtils.isRgb(rgb)) {
     78             msg = "颜色 RGB 格式【" + rgb + "】 不合法,请确认!";
     79             logger.error(msg);
     80             return null;
     81         }
     82         
     83         String r = Integer.toHexString(ColorUtils.getRed(rgb)).toUpperCase();
     84         String g = Integer.toHexString(ColorUtils.getGreen(rgb)).toUpperCase();
     85         String b = Integer.toHexString(ColorUtils.getBlue(rgb)).toUpperCase();
     86         
     87         sb.append("#");
     88         sb.append(r.length() == 1 ? "0" + r : r);
     89         sb.append(g.length() == 1 ? "0" + g : g);
     90         sb.append(b.length() == 1 ? "0" + b : b);
     91         
     92         return sb.toString();
     93     }
     94     
     95     /**
     96      * <strong>获取颜色RGB红色值</strong><br>
     97      * <br>
     98      * @author Aaron.ffp
     99      * @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java getRed, 2016-03-22 23:48:50.501 Exp $
    100      * 
    101      * @param rgb 颜色RGB
    102      * @return 红色值
    103      */
    104     public static int getRed(String rgb){
    105         return Integer.valueOf(ColorUtils.getRGB(rgb)[0]);
    106     }
    107     
    108     /**
    109      * <strong>获取颜色RGB绿色值</strong><br>
    110      * <br>
    111      * @author Aaron.ffp
    112      * @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java getGreen, 2016-03-22 23:48:16.290 Exp $
    113      * 
    114      * @param rgb 颜色RGB
    115      * @return 绿色值
    116      */
    117     public static int getGreen(String rgb){
    118         return Integer.valueOf(ColorUtils.getRGB(rgb)[1]);
    119     }
    120 
    121     /**
    122      * <strong>获取颜色RGB蓝色值</strong><br>
    123      * <br>
    124      * @author Aaron.ffp
    125      * @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java getBlue, 2016-03-22 23:47:20.801 Exp $
    126      * 
    127      * @param rgb 颜色RGB
    128      * @return 蓝色数值
    129      */
    130     public static int getBlue(String rgb){
    131         return Integer.valueOf(ColorUtils.getRGB(rgb)[2]);
    132     }
    133     
    134     /**
    135      * <strong>获取颜色RGB数组</strong><br>
    136      * <br>
    137      * @author Aaron.ffp
    138      * @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java getRGB, 2016-03-21 23:46:00.944 Exp $
    139      * 
    140      * @param rgb 颜色RGB
    141      * @return 颜色数组[红,绿,蓝]
    142      */
    143     public static String[] getRGB(String rgb){
    144         return RegUtils.replace(RegUtils.replaceSpace(rgb), regRepRgb, "").split(",");
    145     }
    146     
    147     /**
    148      * <strong>验证颜色十六进制是否合法</strong><br>
    149      * <ul>
    150      * <li>规则:#号开头,三位或六位字母数字组成的字符串</li>
    151      * </ul>
    152      * <br>
    153      * @author Aaron.ffp
    154      * @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java isHex, 2016-03-20 23:44:03.133 Exp $
    155      * 
    156      * @param hex 十六进制颜色
    157      * @return 合法则返回true
    158      */
    159     public static boolean isHex(String hex) {
    160         return RegUtils.reg(hex, regHex);
    161     }
    162     
    163     /**
    164      * <strong>验证颜色RGB是否合法</strong><br>
    165      * <ul>
    166      * <li>1、RGB符合正则表达式</li>
    167      * <li>2、颜色值在0-255之间</li>
    168      * </ul>
    169      * <br>
    170      * @author Aaron.ffp
    171      * @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java isRgb, 2016-03-12 23:41:19.925 Exp $
    172      * 
    173      * @param rgb 颜色RGB
    174      * @return 是否合法,合法返回true
    175      */
    176     public static boolean isRgb(String rgb) {
    177         boolean r = ColorUtils.getRed(rgb) >= 0 && ColorUtils.getRed(rgb) <= 255;
    178         boolean g = ColorUtils.getGreen(rgb) >= 0 && ColorUtils.getGreen(rgb) <= 255;
    179         boolean b = ColorUtils.getBlue(rgb) >= 0 && ColorUtils.getBlue(rgb) <= 255;
    180         
    181         return ColorUtils.isRgbFormat(rgb) && r && g && b;
    182     }
    183     
    184     /**
    185      * <strong>验证颜色RGB是否匹配正则表达式</strong><br>
    186      * <ul>
    187      * <li>匹配则返回true</li>
    188      * </ul>
    189      * <br>
    190      * @author Aaron.ffp
    191      * @version V1.0.0: autotest-base cn.ffp.autotest.base.util ColorUtils.java isRgbFormat, 2016-03-03 23:40:12.267 Exp $
    192      * 
    193      * @param rgb 颜色RGB
    194      * @return 是否匹配
    195      */
    196     public static boolean isRgbFormat(String rgb) {
    197         return RegUtils.reg(RegUtils.replaceSpace(rgb), regRgb);
    198     }
    199 }

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

      1 package cn.ffp.autotest.base.util;
      2 
      3 import org.testng.annotations.BeforeClass;
      4 import org.testng.annotations.Test;
      5 
      6 public class ColorUtilsTest {
      7     String[][] rgb = new String[8][2];
      8     String[][] hex = new String[11][2];
      9     
     10     @BeforeClass
     11     public void beforeClass() {
     12         rgb[0][0] = "RGB(0,0,0)";
     13         rgb[1][0] = "RGB(0,128,255)";
     14         rgb[2][0] = "RGB(128,0,75)";
     15         rgb[3][0] = "RGB(128,0,0)";
     16         rgb[4][0] = "rgb(128,23,200)";
     17         rgb[5][0] = "rgb(128, 128, 128)";
     18         rgb[6][0] = "rgb(255, 255, 255)";
     19         rgb[7][0] = "rgb(128, 128, 300)";
     20         
     21         rgb[0][1] = "#000000";
     22         rgb[1][1] = "#0080FF";
     23         rgb[2][1] = "#80004B";
     24         rgb[3][1] = "#800000";
     25         rgb[4][1] = "#8017C8";
     26         rgb[5][1] = "#808080";
     27         rgb[6][1] = "#FFFFFF";
     28         rgb[7][1] = null;
     29         
     30         hex[0][0] = "#000000";
     31         hex[1][0] = "#000";
     32         hex[2][0] = "#FAFAFA";
     33         hex[3][0] = "#000080";
     34         hex[4][0] = "#008080";
     35         hex[5][0] = "#F0FF80";
     36         hex[6][0] = "#EA00FA";
     37         hex[7][0] = "#00EAEA";
     38         hex[8][0] = "#876";
     39         hex[9][0] = "#000xEA";
     40         hex[10][0] = "#ghi";
     41         
     42         hex[0][1] = "RGB(0,0,0)";
     43         hex[1][1] = "RGB(0,0,0)";
     44         hex[2][1] = "RGB(250,250,250)";
     45         hex[3][1] = "RGB(0,0,128)";
     46         hex[4][1] = "RGB(0,128,128)";
     47         hex[5][1] = "RGB(240,255,128)";
     48         hex[6][1] = "RGB(234,0,250)";
     49         hex[7][1] = "RGB(136,119,102)";
     50         hex[8][1] = "RGB(0,0,0)";
     51         hex[9][1] = null;
     52         hex[10][1] = null;
     53     }
     54 
     55     @Test
     56     public void getBlue() {
     57         for (int i = 0; i < rgb.length; i++) {
     58             System.out.println("获取蓝色:	" + rgb[i][0] + "	" + ColorUtils.getBlue(rgb[i][0]));
     59         }
     60     }
     61 
     62     @Test
     63     public void getGreen() {
     64         for (int i = 0; i < rgb.length; i++) {
     65             System.out.println("获取绿色:	" + rgb[i][0] + "	" + ColorUtils.getGreen(rgb[i][0]));
     66         }
     67     }
     68 
     69     @Test
     70     public void getRed() {
     71         for (int i = 0; i < rgb.length; i++) {
     72             System.out.println("获取红色:	" + rgb[i][0] + "	" + ColorUtils.getRed(rgb[i][0]));
     73         }
     74     }
     75 
     76     @Test
     77     public void hex2Rgb() {
     78         for (int i = 0; i < hex.length; i++) {
     79             System.out.println("十六进制转 RGB:	" + hex[i][0] + "	期望结果:" + hex[i][1] + "	实际结果:" + ColorUtils.hex2Rgb(hex[i][0]));
     80         }
     81     }
     82 
     83     @Test
     84     public void isHex() {
     85         for (int i = 0; i < hex.length; i++) {
     86             System.out.println(hex[i][0] + "		" + ColorUtils.isHex(hex[i][0]));
     87         }
     88     }
     89 
     90     @Test
     91     public void isRgb() {
     92         for (int i = 0; i < rgb.length; i++) {
     93             System.out.println(rgb[i][0] + "		" + ColorUtils.isRgb(rgb[i][0]));
     94         }
     95     }
     96 
     97     @Test
     98     public void rgb2Hex() {
     99         for (int i = 0; i < rgb.length; i++) {
    100             System.out.println("RGB 转十六进制:	" + rgb[i][0] + "	期望结果:" + rgb[i][1] + "	实际结果:" + ColorUtils.rgb2Hex(rgb[i][0]));
    101         }
    102     }
    103 }
    单元测试源码 ColorUtilsTest.java

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

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

  • 相关阅读:
    常用正则总结
    JavaScript中with语句的理解
    设置点击文本框或图片弹出日历控件
    设置时间 new Date
    windows 下配置 nginx的问题
    CSS3滚动条-webkit-scrollbar
    /、./、../ 的区别
    js中如何获取纯正的undefined?
    关于渐进增强和优雅降级
    每日一练-第三期
  • 原文地址:https://www.cnblogs.com/fengpingfan/p/5317861.html
Copyright © 2011-2022 走看看