在日常的網頁開發中,經常需要進行顏色數值獲取、轉換,例如獲取紅色,獲取藍色,獲取綠色,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("獲取藍色:\t" + rgb[i][0] + "\t" + 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("獲取綠色:\t" + rgb[i][0] + "\t" + 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("獲取紅色:\t" + rgb[i][0] + "\t" + 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:\t" + hex[i][0] + "\t期望結果:" + hex[i][1] + "\t實際結果:" + 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] + "\t\t" + 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] + "\t\t" + 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 轉十六進制:\t" + rgb[i][0] + "\t期望結果:" + rgb[i][1] + "\t實際結果:" + ColorUtils.rgb2Hex(rgb[i][0])); 101 } 102 } 103 }
至此, Java學習-041-顏色工具類(RGB,HEX) 順利完結,希望此文能夠給初學 JavaWeb 的您一份參考。
最后,非常感謝親的駐足,希望此文能對親有所幫助。熱烈歡迎親一起探討,共同進步。非常感謝! ^_^