Java基礎(chǔ)之顏色工具類(超詳細(xì)注釋)
顏色工具類(超詳細(xì)注釋)
設(shè)置屬性值自動(dòng)格式化rgb值和十六進(jìn)制顏色值。
import java.util.HashMap;
import java.util.Map;
public class Color{
private final String[] hex_letters = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
private Double alpha;// 透明度(0.0 ~ 1.0)
private Integer red; // 紅色值(0~255)
private Integer green; // 綠色值(0~255)
private Integer blue; // 藍(lán)色值(0~255)
private String rgb; // RGB值;格式:rgb(red, green, blue)
private String rgba; // RGBA值;格式:rgb(red, green, blue, alpha)
private String hexRed; // 十六進(jìn)制紅色值(00-FF)
private String hexGreen; // 十六進(jìn)制綠色值(00-FF)
private String hexBlue; // 十六進(jìn)制藍(lán)色值(00-FF)
private String hex; // 十六進(jìn)制顏色值;格式:#hexRedhexGreenhexBlue
/**
* 無(wú)參構(gòu)造器
* 默認(rèn)黑色不透明
*/
public Color(){
this.red = 0;
this.green = 0;
this.blue = 0;
this.alpha = 1.0;
this.hexRed = "00";
this.hexGreen = "00";
this.hexBlue = "00";
this.rgb = "rgb(0, 0, 0)";
this.rgba = "rgba(0, 0, 0, 1.0)";
this.hex = "#000000";
}
/**
* 通過(guò)RGB三值初始化
*
* @param red 紅色值(0~255)
* @param green 綠色值(0~255)
* @param blue 藍(lán)色值(0~255)
*/
public Color(Integer red, Integer green, Integer blue){
// 調(diào)用 [通過(guò)RGBA四值初始化]
this(red, green, blue, 1.0);
}
/**
* 通過(guò)RGBA四值初始化
*
* @param red 紅色值(0~255)
* @param green 綠色值(0~255)
* @param blue 藍(lán)色值(0~255)
* @param alpha 透明度(0.0 ~ 1.0)
*/
public Color(Integer red, Integer green, Integer blue, Double alpha){
// 設(shè)置透明度
this.setAlpha(alpha);
// 設(shè)置rgb值
this.setRed(red);
this.setGreen(green);
this.setBlue(blue);
}
/**
* 通過(guò)顏色字符串初始化
* @param colorStr 顏色字符串;格式為:rgb(red,green,blue) 或 rgba(red,green,blue,alpha) 或 #hexRedhexGreenhexBlue
*/
public Color(String colorStr){
// 字符串轉(zhuǎn)大寫、去空格
colorStr = colorStr.replaceAll("\\s*", "").toUpperCase();
if(colorStr.startsWith("#")) {
this.alpha = 1.0;
this.setHex(colorStr);
} else if(colorStr.startsWith("RGB(") && colorStr.endsWith(")")) {
this.alpha = 1.0;
this.setRgb(colorStr);
}else if (colorStr.startsWith("RGBA(") && colorStr.endsWith(")")) {
this.setRgba(colorStr);
}else {
throw new ColorParseException("Color parsing failed, please check color code format. The format is: \"rgb(red,green,blue)\" or \"rgba(red,green,blue,alpha)\" or \"#hexRedhexGreenhexBlue\".");
}
}
/**
* 獲取紅色值
* @return 紅色值(0~255)
*/
public Integer getRed(){
return this.red;
}
/**
* 設(shè)置紅色值
* @param red 紅色值(0~255)
*/
public void setRed(Integer red) {
// 檢查顏色值
if(red < 0 || red > 255)
throw new ColorOutOfRangeException("Red out of range, please set value in 0 ~ 255.");
// 設(shè)置紅色值
this.red = red;
// 紅色值轉(zhuǎn)十六進(jìn)制紅色值,并設(shè)置十六進(jìn)制紅色值
this.hexRed = this.normalToHex(red);
// 刷新
this.refresh();
}
/**
* 獲取綠色值
* @return 綠色值(0~255)
*/
public Integer getGreen(){
return this.green;
}
/**
* 設(shè)置綠色值
* @param green 綠色值(0~255)
*/
public void setGreen(Integer green) {
// 檢查顏色值
if(green < 0 || green > 255)
throw new ColorOutOfRangeException("Green out of range, please set value in 0 ~ 255.");
// 設(shè)置綠色值
this.green = green;
// 綠色值轉(zhuǎn)十六進(jìn)制紅色值,并設(shè)置十六進(jìn)制綠色值
this.hexGreen = this.normalToHex(green);
// 刷新
this.refresh();
}
/**
* 獲取藍(lán)色值
* @return 藍(lán)色值(0~255)
*/
public Integer getBlue(){
return this.blue;
}
/**
* 設(shè)置藍(lán)色值
* @param blue 藍(lán)色值(0~255)
*/
public void setBlue(Integer blue) {
// 檢查顏色值
if(blue < 0 || blue > 255)
throw new ColorOutOfRangeException("Blue out of range, please set value in 0 ~ 255.");
// 設(shè)置藍(lán)色值
this.blue = blue;
// 綠色值轉(zhuǎn)十六進(jìn)制紅色值,并設(shè)置十六進(jìn)制綠色值
this.hexBlue = this.normalToHex(blue);
// 刷新
this.refresh();
}
/**
* 獲取十六進(jìn)制紅色值
* @return 十六進(jìn)制紅色值(00-FF)
*/
public String getHexRed(){
return this.hexRed;
}
/**
* 設(shè)置十六進(jìn)制紅色值
* @param hexRed 十六進(jìn)制紅色值(00-FF)
*/
public void setHexRed(String hexRed) {
// 去空格、轉(zhuǎn)大寫
hexRed = hexRed.replaceAll("\\s*", "").toUpperCase();
// 檢查顏色值
checkHexColorString(hexRed,"HexRed");
// 設(shè)置十六進(jìn)制紅色值
this.hexRed = hexRed;
// 設(shè)置紅色值
this.red = hexToNormal(hexRed);
// 刷新
this.refresh();
}
/**
* 獲取十六進(jìn)制綠色值
* @return 十六進(jìn)制綠色值(00-FF)
*/
public String getHexGreen(){
return this.hexGreen;
}
/**
* 設(shè)置十六進(jìn)制綠色值
* @param hexGreen 十六進(jìn)制綠色值(00-FF)
*/
public void setHexGreen(String hexGreen) {
// 去空格、轉(zhuǎn)大寫
hexGreen = hexGreen.replaceAll("\\s*", "").toUpperCase();
// 檢查顏色值
checkHexColorString(hexGreen,"HexGreen");
// 設(shè)置十六進(jìn)制綠色值
this.hexGreen = hexGreen;
// 設(shè)置綠色值
this.green = hexToNormal(hexGreen);
// 刷新
this.refresh();
}
/**
* 獲取十六進(jìn)制藍(lán)色值
* @return 十六進(jìn)制藍(lán)色值(00-FF)
*/
public String getHexBlue(){
return this.hexBlue;
}
/**
* 設(shè)置十六進(jìn)制藍(lán)色值
* @param hexBlue 十六進(jìn)制藍(lán)色值(00-FF)
*/
public void setHexBlue(String hexBlue) {
// 去空格、轉(zhuǎn)大寫
hexBlue = hexBlue.replaceAll("\\s*", "").toUpperCase();
// 檢查顏色值
checkHexColorString(hexBlue,"HexBlue");
// 設(shè)置十六進(jìn)制藍(lán)色值
this.hexBlue = hexBlue;
// 設(shè)置藍(lán)色值
this.blue = hexToNormal(hexBlue);
// 刷新
this.refresh();
}
/**
* 獲取透明度
* @return 透明度(0.0 ~ 1.0)
*/
public Double getAlpha(){
return this.alpha;
}
/**
* 設(shè)置透明度
* @param alpha 透明度(0.0 ~ 1.0)
*/
public void setAlpha(Double alpha) {
// 檢查透明度
if(alpha < 0 || alpha > 1)
throw new ColorOutOfRangeException("Alpha out of range, please set value in 0.0 ~ 1.0 and keep one decimal place.");
// 檢查小數(shù)點(diǎn)
String[] alphaSplit = alpha.toString().split("\\.");
if(alphaSplit.length > 1)
if(alphaSplit[1].length() > 1)
throw new ColorOutOfRangeException("Alpha out of range, please set value in 0.0 ~ 1.0 and keep one decimal place.");
// 設(shè)置透明度
this.alpha = alpha;
// 刷新
this.refresh();
}
/**
* 獲取RGB值
* @return RGB值;格式:rgb(red, green, blue)
*/
public String getRgb(){
return this.rgb;
}
/**
* 設(shè)置RGB值
* @param rgb RGB值;格式:rgb(red, green, blue)
*/
public void setRgb(String rgb) {
// 解析rgb字符串
Integer[] rgbArray = parseRGB(rgb);
// 設(shè)置顏色值
this.setRed(rgbArray[0]);
this.setGreen(rgbArray[1]);
this.setBlue(rgbArray[2]);
// 設(shè)置十六進(jìn)制顏色值
this.setHexRed(this.normalToHex(this.getRed()));
this.setHexGreen(this.normalToHex(this.getGreen()));
this.setHexBlue(this.normalToHex(this.getBlue()));
// 刷新
this.refresh();
}
/**
* 獲取RGBA值
* @return RGBA值;格式:rgba(red, green, blue, alpha)
*/
public String getRgba(){
return this.rgba;
}
/**
* 設(shè)置RGBA值
* @param rgba RGBA值;格式:rgba(red, green, blue, alpha)
*/
public void setRgba(String rgba) {
// 解析rgb字符串
Double[] rgbaArray = parseRGBA(rgba);
// 設(shè)置顏色值
this.setRed((int)(double)rgbaArray[0]);
this.setGreen((int)(double)rgbaArray[1]);
this.setBlue((int)(double)rgbaArray[2]);
this.setAlpha(rgbaArray[3]);
// 設(shè)置十六進(jìn)制顏色值
this.setHexRed(this.normalToHex(this.getRed()));
this.setHexGreen(this.normalToHex(this.getGreen()));
this.setHexBlue(this.normalToHex(this.getBlue()));
// 刷新
this.refresh();
}
/**
* 獲取十六進(jìn)制顏色值
* @return 十六進(jìn)制顏色值;格式:#hexRedhexGreenhexBlue
*/
public String getHex(){
return this.hex;
}
/**
* 設(shè)置HEX值
* @param hex 十六進(jìn)制顏色值;格式:#hexRedhexGreenhexBlue
*/
public void setHex(String hex) {
// 解析hex字符串
String[] hexArray = parseHex(hex);
// 設(shè)置十六進(jìn)制顏色值
this.setHexRed(hexArray[0]);
this.setHexGreen(hexArray[1]);
this.setHexBlue(hexArray[2]);
// 設(shè)置顏色值
this.setRed(this.hexToNormal(this.getHexRed()));
this.setGreen(this.hexToNormal(this.getHexGreen()));
this.setBlue(this.hexToNormal(this.getHexBlue()));
// 刷新
this.refresh();
}
/**
* 解析RGB字符串
* @param rgb RGB值;格式:rgb(red, green, blue)
* @return 顏色值數(shù)組;0: red; 1: green; 2: blue;
*/
private Integer[] parseRGB(String rgb){
// 去空格、轉(zhuǎn)大寫
rgb = rgb.replaceAll("\\s*", "").toUpperCase();
// 檢查是否為“rgb(”開頭、“)”結(jié)束
if(rgb.startsWith("RGB(") && rgb.endsWith(")"))
rgb = rgb.substring(4, rgb.length()-1);
// rgb字符串?dāng)?shù)組 通過(guò)“,”分割
String[] rgbStrArray = rgb.split(",");
// 判斷數(shù)組長(zhǎng)度是否小于1
if(rgbStrArray.length < 1)
throw new ColorParseException("RGB parsing failed, please check RGB format. The format is: \"rgb(red,green,blue)\" or \"red,green,blue\".");
// String轉(zhuǎn)int
int red = Integer.parseInt(rgbStrArray[0]);
int green = Integer.parseInt(rgbStrArray[1]);
int blue = Integer.parseInt(rgbStrArray[2]);
// 返回rgb顏色數(shù)組
return new Integer[]{red, green, blue};
}
/**
* 解析RGBA字符串
* @param rgba RGBA值;格式:rgba(red, green, blue, alpha)
* @return 顏色值數(shù)組;0: red; 1: green; 2: blue; 3: alpha;
*/
private Double[] parseRGBA(String rgba){
// 去空格、轉(zhuǎn)大寫
rgba = rgba.replaceAll("\\s*", "").toUpperCase();
// 檢查是否為“rgba(”開頭、“)”結(jié)束
if(rgba.startsWith("RGBA(") && rgba.endsWith(")"))
rgba = rgba.substring(5, rgba.length()-1);
// rgb字符串?dāng)?shù)組 通過(guò)“,”分割
String[] rgbaStrArray = rgba.split(",");
// 判斷數(shù)組長(zhǎng)度是否小于1
if(rgbaStrArray.length < 1)
throw new ColorParseException("RGBA parsing failed, please check RGBA format. The format is: \"rgba(red,green,blue,alpha)\" or \"red,green,blue,alpha\".");
// String轉(zhuǎn)double
double red = Double.parseDouble(rgbaStrArray[0]);
double green = Double.parseDouble(rgbaStrArray[1]);
double blue = Double.parseDouble(rgbaStrArray[2]);
double alpha = Double.parseDouble(rgbaStrArray[3]);
// 返回rgba顏色數(shù)組
return new Double[]{red, green, blue, alpha};
}
/**
* 解析HEX字符串
* @param hex 十六進(jìn)制顏色值
* @return 顏色值數(shù)組;0: hexRed; 1: hexGreen; 2: hexBlue;
*/
private String[] parseHex(String hex){
// 字符串去空格、轉(zhuǎn)大寫
hex = hex.replaceAll("\\s*", "").toUpperCase();
// 去起始“#”
if(hex.startsWith("#"))
hex = hex.substring(1);
// 聲明顏色值
String hexRed;
String hexGreen;
String hexBlue;
// 檢查字符串長(zhǎng)度
if(hex.length() == 3){
// 取出顏色值
hexRed = hex.substring(0, 1);
hexGreen = hex.substring(1, 2);
hexBlue = hex.substring(2);
// 補(bǔ)全hexColor
hexRed += hexRed;
hexGreen += hexGreen;
hexBlue += hexBlue;
}else if(hex.length() == 6){
// 取出顏色值
hexRed = hex.substring(0, 2);
hexGreen = hex.substring(2, 4);
hexBlue = hex.substring(4);
}else{
throw new ColorParseException("Hex color parsing failed, please check hex color format. The format is: \"#hexColor\" or \"hexColor\", examples: \"#FFFFFF\" or \"#FFF\" or \"FFFFFF\" or \"FFF\".");
}
// 返回hex顏色數(shù)組
return new String[]{hexRed, hexGreen, hexBlue};
}
/**
* 生成RGB值
* @return RGB值;格式:rgb(red, green, blue)
*/
private synchronized String generateRgb(){
// 準(zhǔn)備結(jié)果
String result = "rgb(";
// 添加紅色值
result += this.getRed();
result += ", ";
// 添加綠色值
result += this.getGreen();
result += ", ";
// 添加藍(lán)色值
result += this.getBlue();
result += ")";
// 返回結(jié)果
return result;
}
/**
* 生成RGBA值
* @return RGBA值;格式:rgb(red, green, blue, alpha)
*/
private synchronized String generateRgba(){
// 準(zhǔn)備結(jié)果
String result = "rgba(";
// 添加紅色值
result += this.getRed();
result += ", ";
// 添加綠色值
result += this.getGreen();
result += ", ";
// 添加藍(lán)色值
result += this.getBlue();
result += ", ";
// 添加透明度
result += this.getAlpha();
result += ", ";
// 返回結(jié)果
return result;
}
/**
* 生成十六進(jìn)制值
* @return 十六進(jìn)制值;格式:#hexRedhexGreenhexBlue
*/
private synchronized String generateHex(){
// 準(zhǔn)備結(jié)果
String result = "#";
// 添加紅色值
result += this.getHexRed();
// 添加綠色值
result += this.getHexGreen();
// 添加藍(lán)色值
result += this.getHexBlue();
// 返回結(jié)果
return result;
}
/**
* 十進(jìn)制轉(zhuǎn)十六進(jìn)制
* @param number 十進(jìn)制數(shù)
* @return 十六進(jìn)制字符串
*/
private String normalToHex(Integer number){
// 十進(jìn)制轉(zhuǎn)十六進(jìn)制
String hexNumber = Integer.toHexString(number).toUpperCase();
// 檢查十六進(jìn)制字符串長(zhǎng)度
if(hexNumber.length() == 1) // 一位
hexNumber = 0 + hexNumber; // 前位+0
// 返回十六進(jìn)制字符串
return hexNumber;
}
/**
* 十六進(jìn)制轉(zhuǎn)十進(jìn)制
* @param hexStr 十六進(jìn)制字符串
* @return 十進(jìn)制數(shù)
*/
private Integer hexToNormal(String hexStr) {
// 去空格轉(zhuǎn)大寫
hexStr = hexStr.replaceAll("\\s*", "");
// 準(zhǔn)備結(jié)果
int result = 0;
// 十六進(jìn)制字母集合
Map<String, Integer> hexLettersMap = new HashMap<>();
// 十六進(jìn)制字符填充至集合
for (int i = 0; i < hex_letters.length; i++) {
hexLettersMap.put(hex_letters[i], i);
}
// 將十六進(jìn)制字符串轉(zhuǎn)為數(shù)組
String[] hexStrArray = new String[hexStr.length()];
for(int i = 0; i < hexStrArray.length; i++){
hexStrArray[i] = hexStr.substring(i, i+1);
}
// 轉(zhuǎn)十進(jìn)制數(shù)
for(int i = 0; i < hexStrArray.length; i++){
result += hexLettersMap.get(hexStrArray[i]) * Math.pow(16, hexStrArray.length - 1 - i);
}
// 返回結(jié)果
return result;
}
/**
* 檢查十六進(jìn)制顏色字符串(兩位:00~FF)
* @param hexStr 十六進(jìn)制字符串(00~FF)
* @param errorFieldName 發(fā)生錯(cuò)誤的字段名
*/
private void checkHexColorString(String hexStr, String errorFieldName){
// 去空格,轉(zhuǎn)大寫
hexStr = hexStr.replaceAll("\\s*", "").toUpperCase();
// 截取字符
String firstLetter;
String secondLetter;
// 檢查格式
if(hexStr.length() == 1){
firstLetter = secondLetter = hexStr;
}else if(hexStr.length() == 2){
firstLetter = hexStr.substring(0,1);
secondLetter = hexStr.substring(1);
}else{
throw new ColorOutOfRangeException(errorFieldName + " out of range, please set value in 00 ~ FF.");
}
// 字符正確標(biāo)識(shí)
boolean firstRight = false;
boolean secondRight = false;
// 檢查第一個(gè)字符
for (String letter : hex_letters) {
if(letter.equals(firstLetter)){
firstRight = true;
break;
}
}
// 檢查第二個(gè)字符
for (String letter : hex_letters) {
if(letter.equals(secondLetter)){
secondRight = true;
break;
}
}
// 判斷是否全部正確
if(!firstRight || !secondRight)
throw new ColorOutOfRangeException(errorFieldName + " out of range, please set value in 00 ~ FF.");
}
/**
* 刷新
*/
private void refresh(){
// 生成并設(shè)置rgb值
this.rgb = this.generateRgb();
// 生成并設(shè)置rgba值
this.rgba = this.generateRgba();
// 生成并設(shè)置十六進(jìn)制顏色值
this.hex = this.generateHex();
}
/**
* 對(duì)象轉(zhuǎn)字符串
* @return 字符串
*/
public String toString(){
return "Color: {" +
"\n\tred: " + this.getRed() +
",\n\tgreen: " + this.getGreen() +
",\n\tblue: " + this.getBlue() +
",\n\talpha: " + this.getAlpha() +
",\n\trgb: " + this.getRgb() +
",\n\trgba: " + this.getRgba() +
",\n\thexRed: " + this.getHexRed() +
",\n\thexGreen: " + this.getHexGreen() +
",\n\thexBlue: " + this.getHexBlue() +
",\n\thex: " + this.getHex() +
"\n}";
}
/**
* 顏色超出范圍異常
*/
public static class ColorOutOfRangeException extends RuntimeException{
public ColorOutOfRangeException(String message) {
super(message);
}
}
/**
* 顏色解析異常
*/
public static class ColorParseException extends RuntimeException{
public ColorParseException(String message) {
super(message);
}
}
}
到此這篇關(guān)于Java基礎(chǔ)之顏色工具類(超詳細(xì)注釋)的文章就介紹到這了,更多相關(guān)Java顏色工具類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java中利用Alibaba開源技術(shù)EasyExcel來(lái)操作Excel表的示例代碼
- Java使用easyExcel導(dǎo)出excel數(shù)據(jù)案例
- 深入淺析Java常用的格式化Json工具類
- Java服務(wù)器主機(jī)信息監(jiān)控工具類的示例代碼
- 教你如何使用Java多線程編程LockSupport工具類
- Java常用工具類庫(kù)——Hutool的使用簡(jiǎn)介
- 淺談java如何生成分享海報(bào)工具類
- java中封裝JDBC工具類的實(shí)例分析
- Java JDBC自定義封裝工具類的步驟和完整代碼
- Java8優(yōu)雅的字符串拼接工具類StringJoiner實(shí)例代碼
- Java應(yīng)用EasyExcel工具類
相關(guān)文章
Java流操作之?dāng)?shù)據(jù)流實(shí)例代碼
這篇文章主要介紹了Java流操作之?dāng)?shù)據(jù)流實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Mybatis中輸入輸出映射與動(dòng)態(tài)Sql圖文詳解
這篇文章主要給大家介紹了關(guān)于Mybatis中輸入輸出映射與動(dòng)態(tài)Sql的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
深入淺析java web log4j 配置及在web項(xiàng)目中配置Log4j的技巧
這篇文章主要介紹了2015-11-11
Java遞歸實(shí)現(xiàn)評(píng)論多級(jí)回復(fù)功能
這篇文章主要介紹了Java遞歸實(shí)現(xiàn)評(píng)論多級(jí)回復(fù)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
Spring-Boot 集成Solr客戶端的詳細(xì)步驟
本篇文章主要介紹了Spring-Boot 集成Solr客戶端的詳細(xì)步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
基于kafka實(shí)現(xiàn)Spring Cloud Bus消息總線
消息總線是一種通信工具,可以在機(jī)器之間互相傳輸消息、文件等,這篇文章主要介紹了如何利用kafka實(shí)現(xiàn)SpringCloud Bus消息總線,感興趣的可以學(xué)習(xí)一下2022-04-04
java中的char占幾個(gè)字節(jié)實(shí)例分析
這篇文章主要介紹了java中的char占幾個(gè)字節(jié)實(shí)例分析的相關(guān)資料,需要的朋友可以參考下2017-04-04

