Color tool class (super detailed comment)
Set the attribute value to automatically format the rgb value and hexadecimal color value.
import ; import ; 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;// Transparency (0.0 ~ 1.0) private Integer red; // Red value (0~255) private Integer green; // Green value (0~255) private Integer blue; // Blue value (0~255) private String rgb; // RGB value; format: rgb(red, green, blue) private String rgba; // RGBA value; format: rgb(red, green, blue, alpha) private String hexRed; // Hexadecimal red value (00-FF) private String hexGreen; // Hexadecimal green value (00-FF) private String hexBlue; // Hexadecimal blue value (00-FF) private String hex; // Hex color value; format: #hexRedhexGreenhexBlue /** * Parameterless constructor * Default black opaque */ public Color(){ = 0; = 0; = 0; = 1.0; = "00"; = "00"; = "00"; = "rgb(0, 0, 0)"; = "rgba(0, 0, 0, 1.0)"; = "#000000"; } /** * Initialization via RGB three-value * * @param red value (0~255) * @param green green value (0~255) * @param blue blue value (0~255) */ public Color(Integer red, Integer green, Integer blue){ // Call [Initialization through RGBA quad value] this(red, green, blue, 1.0); } /** * Initialization via RGBA quadruple value * * @param red value (0~255) * @param green green value (0~255) * @param blue blue value (0~255) * @param alpha Transparency (0.0 ~ 1.0) */ public Color(Integer red, Integer green, Integer blue, Double alpha){ // Set transparency (alpha); // Set rgb value (red); (green); (blue); } /** * Initialization via color string * @param colorStr color string; format: rgb(red,green,blue) or rgba(red,green,blue,alpha) or #hexRedhexGreenhexBlue */ public Color(String colorStr){ // Turn string to uppercase and remove spaces colorStr = ("\\s*", "").toUpperCase(); if(("#")) { = 1.0; (colorStr); } else if(("RGB(") && (")")) { = 1.0; (colorStr); }else if (("RGBA(") && (")")) { (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\"."); } } /** * Get the red value * @return Red value (0~255) */ public Integer getRed(){ return ; } /** * Set the red value * @param red value (0~255) */ public void setRed(Integer red) { // Check the color value if(red < 0 || red > 255) throw new ColorOutOfRangeException("Red out of range, please set value in 0 ~ 255."); // Set the red value = red; // Turn red value into hexadecimal red value and set hexadecimal red value = (red); // refresh (); } /** * Get green value * @return Green value (0~255) */ public Integer getGreen(){ return ; } /** * Set green value * @param green green value (0~255) */ public void setGreen(Integer green) { // Check the color value if(green < 0 || green > 255) throw new ColorOutOfRangeException("Green out of range, please set value in 0 ~ 255."); // Set green value = green; // Turn green value into hexadecimal red value and set hexadecimal green value = (green); // refresh (); } /** * Get blue value * @return Blue value (0~255) */ public Integer getBlue(){ return ; } /** * Set the blue value * @param blue blue value (0~255) */ public void setBlue(Integer blue) { // Check the color value if(blue < 0 || blue > 255) throw new ColorOutOfRangeException("Blue out of range, please set value in 0 ~ 255."); // Set blue value = blue; // Turn green value into hexadecimal red value and set hexadecimal green value = (blue); // refresh (); } /** * Get the hexadecimal red value * @return Hexadecimal red value (00-FF) */ public String getHexRed(){ return ; } /** * Set the hexadecimal red value * @param hexRed hex red value (00-FF) */ public void setHexRed(String hexRed) { // Remove spaces and convert capital hexRed = ("\\s*", "").toUpperCase(); // Check the color value checkHexColorString(hexRed,"HexRed"); // Set the hexadecimal red value = hexRed; // Set the red value = hexToNormal(hexRed); // refresh (); } /** * Get the hexadecimal green value * @return Hexadecimal green value (00-FF) */ public String getHexGreen(){ return ; } /** * Set the hexadecimal green value * @param hexGreen Hex green value (00-FF) */ public void setHexGreen(String hexGreen) { // Remove spaces and convert capital hexGreen = ("\\s*", "").toUpperCase(); // Check the color value checkHexColorString(hexGreen,"HexGreen"); // Set the hexadecimal green value = hexGreen; // Set green value = hexToNormal(hexGreen); // refresh (); } /** * Get the hexadecimal blue value * @return Hexadecimal blue value (00-FF) */ public String getHexBlue(){ return ; } /** * Set the hexadecimal blue value * @param hexBlue Hex blue value (00-FF) */ public void setHexBlue(String hexBlue) { // Remove spaces and convert capital hexBlue = ("\\s*", "").toUpperCase(); // Check the color value checkHexColorString(hexBlue,"HexBlue"); // Set hexadecimal blue value = hexBlue; // Set blue value = hexToNormal(hexBlue); // refresh (); } /** * Get transparency * @return Transparency (0.0 ~ 1.0) */ public Double getAlpha(){ return ; } /** * Set transparency * @param alpha Transparency (0.0 ~ 1.0) */ public void setAlpha(Double alpha) { // Check transparency 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."); // Check the decimal point String[] alphaSplit = ().split("\\."); if( > 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."); // Set transparency = alpha; // refresh (); } /** * Get RGB value * @return RGB value; format: rgb(red, green, blue) */ public String getRgb(){ return ; } /** * Set RGB value * @param rgb RGB value; format: rgb(red, green, blue) */ public void setRgb(String rgb) { // parse rgb string Integer[] rgbArray = parseRGB(rgb); // Set color value (rgbArray[0]); (rgbArray[1]); (rgbArray[2]); // Set hexadecimal color value ((())); ((())); ((())); // refresh (); } /** * Get RGBA value * @return RGBA value; format: rgba(red, green, blue, alpha) */ public String getRgba(){ return ; } /** * Set RGBA value * @param rgba RGBA value; format: rgba(red, green, blue, alpha) */ public void setRgba(String rgba) { // parse rgb string Double[] rgbaArray = parseRGBA(rgba); // Set color value ((int)(double)rgbaArray[0]); ((int)(double)rgbaArray[1]); ((int)(double)rgbaArray[2]); (rgbaArray[3]); // Set hexadecimal color value ((())); ((())); ((())); // refresh (); } /** * Get hexadecimal color value * @return Hex color value; Format: #hexRedhexGreenhexBlue */ public String getHex(){ return ; } /** * Set HEX value * @param hex hex color value; format: #hexRedhexGreenhexBlue */ public void setHex(String hex) { // parse hex string String[] hexArray = parseHex(hex); // Set hexadecimal color value (hexArray[0]); (hexArray[1]); (hexArray[2]); // Set color value ((())); ((())); ((())); // refresh (); } /** * Parsing RGB strings * @param rgb RGB value; format: rgb(red, green, blue) * @return Color value array; 0: red; 1: green; 2: blue; */ private Integer[] parseRGB(String rgb){ // Remove spaces and convert capital rgb = ("\\s*", "").toUpperCase(); // Check whether it starts with "rgb(" and ends with ")" if(("RGB(") && (")")) rgb = (4, ()-1); // rgb string array is divided by "," String[] rgbStrArray = (","); // Determine whether the array length is less than 1 if( < 1) throw new ColorParseException("RGB parsing failed, please check RGB format. The format is: \"rgb(red,green,blue)\" or \"red,green,blue\"."); // String to int int red = (rgbStrArray[0]); int green = (rgbStrArray[1]); int blue = (rgbStrArray[2]); // Return rgb color array return new Integer[]{red, green, blue}; } /** * Parsing RGBA strings * @param rgba RGBA value; format: rgba(red, green, blue, alpha) * @return Color value array; 0: red; 1: green; 2: blue; 3: alpha; */ private Double[] parseRGBA(String rgba){ // Remove spaces and convert capital rgba = ("\\s*", "").toUpperCase(); // Check whether it starts with "rgba(" and ends with ") if(("RGBA(") && (")")) rgba = (5, ()-1); // rgb string array is divided by "," String[] rgbaStrArray = (","); // Determine whether the array length is less than 1 if( < 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 to double double red = (rgbaStrArray[0]); double green = (rgbaStrArray[1]); double blue = (rgbaStrArray[2]); double alpha = (rgbaStrArray[3]); // Return rgba color array return new Double[]{red, green, blue, alpha}; } /** * Parsing HEX strings * @param hex hex color value * @return Color value array; 0: hexRed; 1: hexGreen; 2: hexBlue; */ private String[] parseHex(String hex){ // Remove spaces and convert capital to string hex = ("\\s*", "").toUpperCase(); // Go to the starting "#" if(("#")) hex = (1); // Declare color value String hexRed; String hexGreen; String hexBlue; // Check the string length if(() == 3){ // Remove the color value hexRed = (0, 1); hexGreen = (1, 2); hexBlue = (2); // Complete hexColor hexRed += hexRed; hexGreen += hexGreen; hexBlue += hexBlue; }else if(() == 6){ // Remove the color value hexRed = (0, 2); hexGreen = (2, 4); hexBlue = (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\"."); } // Return hex color array return new String[]{hexRed, hexGreen, hexBlue}; } /** * Generate RGB values * @return RGB value; format: rgb(red, green, blue) */ private synchronized String generateRgb(){ // Prepare the results String result = "rgb("; // Add red value result += (); result += ", "; // Add green value result += (); result += ", "; // Add blue value result += (); result += ")"; // Return result return result; } /** * Generate RGBA values * @return RGBA value; format: rgb(red, green, blue, alpha) */ private synchronized String generateRgba(){ // Prepare the results String result = "rgba("; // Add red value result += (); result += ", "; // Add green value result += (); result += ", "; // Add blue value result += (); result += ", "; // Add transparency result += (); result += ", "; // Return result return result; } /** * Generate hexadecimal value * @return Hex value; Format: #hexRedhexGreenhexBlue */ private synchronized String generateHex(){ // Prepare the results String result = "#"; // Add red value result += (); // Add green value result += (); // Add blue value result += (); // Return result return result; } /** * Decimal to hexadecimal * @param number Decimal number * @return Hexadecimal string */ private String normalToHex(Integer number){ // Decimal to hexadecimal String hexNumber = (number).toUpperCase(); // Check the length of hexadecimal string if(() == 1) // One hexNumber = 0 + hexNumber; // Front position +0 // Returns hexadecimal string return hexNumber; } /** * Hexadecimal to decimal * @param hexStr hex string * @return Decimal number */ private Integer hexToNormal(String hexStr) { // Turn to capitalize by removing spaces hexStr = ("\\s*", ""); // Prepare the results int result = 0; // Hexadecimal letter collection Map<String, Integer> hexLettersMap = new HashMap<>(); // Hexadecimal characters are filled to the collection for (int i = 0; i < hex_letters.length; i++) { (hex_letters[i], i); } // Convert hexadecimal string to array String[] hexStrArray = new String[()]; for(int i = 0; i < ; i++){ hexStrArray[i] = (i, i+1); } // Turn to decimal number for(int i = 0; i < ; i++){ result += (hexStrArray[i]) * (16, - 1 - i); } // Return result return result; } /** * Check the hexadecimal color string (two digits: 00~FF) * @param hexStr hex string (00~FF) * @param errorFieldName The field name where the error occurred */ private void checkHexColorString(String hexStr, String errorFieldName){ // Remove spaces and convert capital hexStr = ("\\s*", "").toUpperCase(); // Intercept characters String firstLetter; String secondLetter; // Check the format if(() == 1){ firstLetter = secondLetter = hexStr; }else if(() == 2){ firstLetter = (0,1); secondLetter = (1); }else{ throw new ColorOutOfRangeException(errorFieldName + " out of range, please set value in 00 ~ FF."); } // Correct character identification boolean firstRight = false; boolean secondRight = false; // Check the first character for (String letter : hex_letters) { if((firstLetter)){ firstRight = true; break; } } // Check the second character for (String letter : hex_letters) { if((secondLetter)){ secondRight = true; break; } } // Determine whether all are correct if(!firstRight || !secondRight) throw new ColorOutOfRangeException(errorFieldName + " out of range, please set value in 00 ~ FF."); } /** * refresh */ private void refresh(){ // Generate and set rgb value = (); // Generate and set rgba value = (); // Generate and set hexadecimal color values = (); } /** * Object to string * @return string */ public String toString(){ return "Color: {" + "\n\tred: " + () + ",\n\tgreen: " + () + ",\n\tblue: " + () + ",\n\talpha: " + () + ",\n\trgb: " + () + ",\n\trgba: " + () + ",\n\thexRed: " + () + ",\n\thexGreen: " + () + ",\n\thexBlue: " + () + ",\n\thex: " + () + "\n}"; } /** * Color out of range abnormality */ public static class ColorOutOfRangeException extends RuntimeException{ public ColorOutOfRangeException(String message) { super(message); } } /** * Color resolution exception */ public static class ColorParseException extends RuntimeException{ public ColorParseException(String message) { super(message); } } }
This is the end of this article about Java basic color tool class (super detailed annotations). For more related Java color tool class content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!