设置属性值自动格式化rgb值和十六进制颜色值。
import java.text.DecimalFormat;
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 Integer red;
private Integer green;
private Integer blue;
private Integer alpha;
private String rgb;
private String rgba;
private String hexRed;
private String hexGreen;
private String hexBlue;
private String hexAlpha;
private String hex;
* 无参构造器 默认黑色不透明
public Color() {
this.red = 0x00;
this.green = 0x00;
this.blue = 0x00;
this.alpha = 0xFF;
this.hexRed = "00";
this.hexGreen = "00";
this.hexBlue = "00";
this.hexAlpha = "FF";
this.rgb = "rgb(0, 0, 0)";
this.rgba = "rgba(0, 0, 0, 1.0)";
this.hex = "#FF000000";
* 通过RGB三值初始化
* @param red 红色值(0 ~ 255)
* @param green 绿色值(0 ~ 255)
* @param blue 蓝色值(0 ~ 255)
public Color(Integer red, Integer green, Integer blue) {
this(red, green, blue, 0xFF);
* 通过RGBA四值初始化
* @param red 红色值(0 ~ 255)
* @param green 绿色值(0 ~ 255)
* @param blue 蓝色值(0 ~ 255)
* @param alpha 透明度(0 ~ 255)
public Color(Integer red, Integer green, Integer blue, Integer alpha) {
this.setAlpha(alpha);
this.setRed(red);
this.setGreen(green);
this.setBlue(blue);
* 通过颜色字符串初始化
* @param colorStr 颜色字符串;格式为:rgb(red,green,blue) 或 rgba(red,green,blue,alpha) 或
* #hexRedhexGreenhexBlue
public Color(String colorStr) {
colorStr = colorStr.replaceAll("\\s*", "").toUpperCase();
if (colorStr.startsWith("#")) {
this.setHex(colorStr);
} else if (colorStr.startsWith("RGB(") && colorStr.endsWith(")")) {
this.setAlpha(0xFF);
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;
* 设置红色值
* @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.");
this.red = red;
this.hexRed = this.normalToHex(red);
this.refresh();
* 获取绿色值
* @return 绿色值(0 ~ 255)
public Integer getGreen() {
return this.green;
* 设置绿色值
* @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.");
this.green = green;
this.hexGreen = this.normalToHex(green);
this.refresh();
* 获取蓝色值
* @return 蓝色值(0 ~ 255)
public Integer getBlue() {
return this.blue;
* 设置蓝色值
* @param blue 蓝色值(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.");
this.blue = blue;
this.hexBlue = this.normalToHex(blue);
this.refresh();
* 获取十六进制红色值
* @return 十六进制红色值(00 ~ FF)
public String getHexRed() {
return this.hexRed;
* 设置十六进制红色值
* @param hexRed 十六进制红色值(00 ~ FF)
public void setHexRed(String hexRed) {
hexRed = hexRed.replaceAll("\\s*", "").toUpperCase();
checkHexColorString(hexRed, "HexRed");
this.hexRed = hexRed;
this.red = hexToNormal(hexRed);
this.refresh();
* 获取十六进制绿色值
* @return 十六进制绿色值(00 ~ FF)
public String getHexGreen() {
return this.hexGreen;
* 设置十六进制绿色值
* @param hexGreen 十六进制绿色值(00 ~ FF)
public void setHexGreen(String hexGreen) {
hexGreen = hexGreen.replaceAll("\\s*", "").toUpperCase();
checkHexColorString(hexGreen, "HexGreen");
this.hexGreen = hexGreen;
this.green = hexToNormal(hexGreen);
this.refresh();
* 获取十六进制蓝色值
* @return 十六进制蓝色值(00 ~ FF)
public String getHexBlue() {
return this.hexBlue;
* 设置十六进制蓝色值
* @param hexBlue 十六进制蓝色值(00 ~ FF)
public void setHexBlue(String hexBlue) {
hexBlue = hexBlue.replaceAll("\\s*", "").toUpperCase();
checkHexColorString(hexBlue, "HexBlue");
this.hexBlue = hexBlue;
this.blue = hexToNormal(hexBlue);
this.refresh();
* 获取透明度
* @return 透明度(0 ~ 255)
public Integer getAlpha() {
return this.alpha;
* 设置透明度
* @param alpha 透明度(0 ~ 255)
public void setAlpha(Integer alpha) {
if (alpha < 0 || alpha > 255)
throw new ColorOutOfRangeException("Alpha out of range, please set value in 0 ~ 255 and keep one decimal place.");
this.alpha = alpha;
this.hexAlpha = normalToHex(alpha);
this.refresh();
* 获取十六进制透明度
public String getHexAlpha() {
return this.hexAlpha;
* 设置十六进制透明度
* @param hexAlpha 十六进制透明度(00 ~ FF)
public void setHexAlpha(String hexAlpha) {
hexAlpha = hexAlpha.replaceAll("\\s*", "").toUpperCase();
checkHexColorString(hexAlpha, "HexAlpha");
this.hexAlpha = hexAlpha;
this.alpha = hexToNormal(hexAlpha);
this.refresh();
* 获取RGB值
* @return RGB值;格式:rgb(red, green, blue)
public String getRgb() {
return this.rgb;
* 设置RGB值
* @param rgb RGB值;格式:rgb(red, green, blue)
public void setRgb(String rgb) {
int[] rgbArray = parseRGB(rgb);
this.setRed(rgbArray[0]);
this.setGreen(rgbArray[1]);
this.setBlue(rgbArray[2]);
this.setAlpha(0xFF);
this.refresh();
* 获取RGBA值
* @return RGBA值;格式:rgba(red, green, blue, alpha)
public String getRgba() {
return this.rgba;
* 设置RGBA值
* @param rgba RGBA值;格式:rgba(red, green, blue, alpha)
public void setRgba(String rgba) {
int[] rgbaArray = parseRGBA(rgba);
this.setRed(rgbaArray[0]);
this.setGreen(rgbaArray[1]);
this.setBlue(rgbaArray[2]);
this.setAlpha(rgbaArray[3]);
this.refresh();
* 获取十六进制颜色值
* @return 十六进制颜色值;格式:#hexRedhexGreenhexBlue
public String getHex() {
return this.hex;
* 设置HEX值
* @param hex 十六进制颜色值;格式:#hexRedhexGreenhexBlue
public void setHex(String hex) {
String[] hexArray = parseHex(hex);
this.setHexAlpha(hexArray[0]);
this.setHexRed(hexArray[1]);
this.setHexGreen(hexArray[2]);
this.setHexBlue(hexArray[3]);
this.refresh();
* 解析RGB字符串
* @param rgb RGB值;格式:rgb(red, green, blue)
* @return 颜色值数组;0: red; 1: green; 2: blue;
private int[] parseRGB(String rgb) {
rgb = rgb.replaceAll("\\s*", "").toUpperCase();
if (rgb.startsWith("RGB(") && rgb.endsWith(")"))
rgb = rgb.substring(4, rgb.length() - 1);
String[] rgbStrArray = rgb.split(",");
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\".");
int red = Integer.parseInt(rgbStrArray[0]);
int green = Integer.parseInt(rgbStrArray[1]);
int blue = Integer.parseInt(rgbStrArray[2]);
return new int[] { red, green, blue };
* 解析RGBA字符串
* @param rgba RGBA值;格式:rgba(red, green, blue, alpha)
* @return 颜色值数组;0: red; 1: green; 2: blue; 3: alpha;
private int[] parseRGBA(String rgba) {
rgba = rgba.replaceAll("\\s*", "").toUpperCase();
if (rgba.startsWith("RGBA(") && rgba.endsWith(")"))
rgba = rgba.substring(5, rgba.length() - 1);
String[] rgbaStrArray = rgba.split(",");
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\".");
int red = Integer.parseInt(rgbaStrArray[0]);
int green = Integer.parseInt(rgbaStrArray[1]);
int blue = Integer.parseInt(rgbaStrArray[2]);
int alpha = getIntAlpha(Double.parseDouble(rgbaStrArray[3]));
return new int[] { red, green, blue, alpha };
* 解析HEX字符串
* @param hex 十六进制颜色值
* @return 颜色值数组;0: hexRed; 1: hexGreen; 2: hexBlue;
private String[] parseHex(String hex) {
hex = hex.replaceAll("\\s*", "").toUpperCase();
if (hex.startsWith("#"))
hex = hex.substring(1);
String hexRed = null;
String hexGreen = null;
String hexBlue = null;
String hexAlpha = null;
switch (hex.length()) {
case 3:
hexAlpha = "FF";
hexRed = hex.substring(0, 1);
hexGreen = hex.substring(1, 2);
hexBlue = hex.substring(2);
hexRed += hexRed;
hexGreen += hexGreen;
hexBlue += hexBlue;
break;
case 4:
hexAlpha = hex.substring(0, 1);
hexRed = hex.substring(1, 2);
hexGreen = hex.substring(2, 3);
hexBlue = hex.substring(3);
hexAlpha += hexAlpha;
hexRed += hexRed;
hexGreen += hexGreen;
hexBlue += hexBlue;
break;
case 6:
hexAlpha = "FF";
hexRed = hex.substring(0, 2);
hexGreen = hex.substring(2, 4);
hexBlue = hex.substring(4);
break;
case 8:
hexAlpha = hex.substring(0, 2);
hexRed = hex.substring(2, 4);
hexGreen = hex.substring(4, 6);
hexBlue = hex.substring(6);
break;
default:
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\".");
return new String[] { hexAlpha, hexRed, hexGreen, hexBlue };
* 生成RGB值
* @return RGB值;格式:rgb(red, green, blue)
private synchronized String generateRgb() {
String result = "rgb(";
result += this.getRed();
result += ", ";
result += this.getGreen();
result += ", ";
result += this.getBlue();
result += ")";
return result;
* 生成RGBA值
* @return RGBA值;格式:rgb(red, green, blue, alpha)
private synchronized String generateRgba() {
String result = "rgba(";
result += this.getRed();
result += ", ";
result += this.getGreen();
result += ", ";
result += this.getBlue();
result += ", ";
result += getDoubleAlpha(this.getAlpha());
result += ")";
return result;
* 生成十六进制值
* @return 十六进制值;格式:#hexRedhexGreenhexBlue
private synchronized String generateHex() {
String result = "#";
result += this.getHexAlpha();
result += this.getHexRed();
result += this.getHexGreen();
result += this.getHexBlue();
return result;
* 十进制转十六进制
* @param number 十进制数
* @return 十六进制字符串
private String normalToHex(Integer number) {
String hexNumber = Integer.toHexString(number).toUpperCase();
if (hexNumber.length() == 1)
hexNumber = 0 + hexNumber;
return hexNumber;
* 十六进制转十进制
* @param hexStr 十六进制字符串
* @return 十进制数
private Integer hexToNormal(String hexStr) {
hexStr = hexStr.replaceAll("\\s*", "");
int result = 0;
Map<String, Integer> hexLettersMap = new HashMap<>();
for (int i = 0; i < hex_letters.length; i++) {
hexLettersMap.put(hex_letters[i], i);
String[] hexStrArray = new String[hexStr.length()];
for (int i = 0; i < hexStrArray.length; i++) {
hexStrArray[i] = hexStr.substring(i, i + 1);
for (int i = 0; i < hexStrArray.length; i++) {
result += hexLettersMap.get(hexStrArray[i]) * Math.pow(16, hexStrArray.length - 1 - i);
return result;
* 获取整型透明度
* @param alpha 透明度(0.0 ~ 1.0)
* @return 整型透明度
private Integer getIntAlpha(Double alpha) {
if (alpha == null)
return null;
if (alpha < 0 || alpha > 1)
throw new ColorOutOfRangeException("Float alpha out of range, please set value in 0.0 ~ 1.0");
return (int) Math.round(alpha * 255);
* 获取浮点型透明度
* @param alpha 透明度(0 ~ 255)
* @return 浮点型透明度
private Double getDoubleAlpha(Integer alpha) {
if (alpha == null)
return null;
if (alpha < 0 || alpha > 255)
throw new ColorOutOfRangeException("Float alpha out of range, please set value in 0 ~ 255");
DecimalFormat df = new DecimalFormat("#.#");
return Double.parseDouble(df.format((double) alpha / 255));
* 检查十六进制颜色字符串(两位:00~FF)
* @param hexStr 十六进制字符串(00~FF)
* @param errorFieldName 发生错误的字段名
private void checkHexColorString(String hexStr, String errorFieldName) {
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.");
boolean firstRight = false;
boolean secondRight = false;
for (String letter : hex_letters) {
if (letter.equals(firstLetter)) {
firstRight = true;
break;
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() {
this.rgb = this.generateRgb();
this.rgba = this.generateRgba();
this.hex = this.generateHex();
* 对象转字符串
* @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\thexAlpha: " + this.getHexAlpha() +
",\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);
public class ColorUtil {
public static Object evaluateColor(float fraction, Object startValue,
Object endValue) {
int startInt = (Integer) startValue;
int startA = (startInt >> 24) & 0xff;
int n, m, pos, pi[102];
char p[102], t[150002];
char file[30] = "..\\output\\output_ .txt";
int Compute_Prefix_Function( );
int KMP_Match( );
int main( )
freopen( ".