📄 colorencoder.java
字号:
/**
*
*/
package com.esri.solutions.jitk.data.svg;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
/**
* @author vlad2928
*
*/
public class ColorEncoder {
// HashTable with 147 standard SVG entries
private static final Hashtable<String, String> _svgColors = new Hashtable<String, String>();
static {
_svgColors.put("black","0,0,0"); _svgColors.put("navy","0,0,128"); _svgColors.put("darkblue","0,0,139");
_svgColors.put("mediumblue","0,0,205"); _svgColors.put("blue","0,0,255"); _svgColors.put("darkgreen","0,100,0");
_svgColors.put("green","0,128,0"); _svgColors.put("teal","0,128,128"); _svgColors.put("darkcyan","0,139,139");
_svgColors.put("deepskyblue","0,191,255"); _svgColors.put("darkturquoise","0,206,209"); _svgColors.put("mediumspringgreen","0,250,154");
_svgColors.put("lime","0,255,0"); _svgColors.put("springgreen","0,255,127"); _svgColors.put("cyan","0,255,255");
_svgColors.put("aqua","0,255,255"); _svgColors.put("midnightblue","25,25,112"); _svgColors.put("dodgerblue","30,144,255");
_svgColors.put("lightseagreen","32,178,170"); _svgColors.put("forestgreen","34,139,34"); _svgColors.put("seagreen","46,139,87");
_svgColors.put("darkslategray","47,79,79"); _svgColors.put("darkslategrey","47,79,79"); _svgColors.put("limegreen","50,205,50");
_svgColors.put("mediumseagreen","60,179,113"); _svgColors.put("turquoise","64,224,208"); _svgColors.put("royalblue","65,105,225");
_svgColors.put("steelblue","70,130,180"); _svgColors.put("darkslateblue","72,61,139"); _svgColors.put("mediumturquoise","72,209,204");
_svgColors.put("indigo","75,0,130"); _svgColors.put("darkolivegreen","85,107,47"); _svgColors.put("cadetblue","95,158,160");
_svgColors.put("cornflowerblue","100,149,237"); _svgColors.put("mediumaquamarine","102,205,170"); _svgColors.put("dimgrey","105,105,105");
_svgColors.put("dimgray","105,105,105"); _svgColors.put("slateblue","106,90,205"); _svgColors.put("olivedrab","107,142,35");
_svgColors.put("slategrey","112,128,144"); _svgColors.put("slategray","112,128,144"); _svgColors.put("lightslategray","119,136,153");
_svgColors.put("lightslategrey","119,136,153"); _svgColors.put("mediumslateblue","123,104,238"); _svgColors.put("lawngreen","124,252,0");
_svgColors.put("chartreuse","127,255,0"); _svgColors.put("aquamarine","127,255,212"); _svgColors.put("maroon","128,0,0");
_svgColors.put("purple","128,0,128"); _svgColors.put("olive","128,128,0"); _svgColors.put("gray","128,128,128");
_svgColors.put("grey","128,128,128"); _svgColors.put("skyblue","135,206,235"); _svgColors.put("lightskyblue","135,206,250");
_svgColors.put("blueviolet","138,43,226"); _svgColors.put("darkred","139,0,0"); _svgColors.put("darkmagenta","139,0,139");
_svgColors.put("saddlebrown","139,69,19"); _svgColors.put("darkseagreen","143,188,143"); _svgColors.put("lightgreen","144,238,144");
_svgColors.put("mediumpurple","147,112,219"); _svgColors.put("darkviolet","148,0,211"); _svgColors.put("palegreen","152,251,152");
_svgColors.put("darkorchid","153,50,204"); _svgColors.put("yellowgreen","154,205,50"); _svgColors.put("sienna","160,82,45");
_svgColors.put("brown","165,42,42"); _svgColors.put("darkgray","169,169,169"); _svgColors.put("darkgrey","169,169,169");
_svgColors.put("lightblue","173,216,230"); _svgColors.put("greenyellow","173,255,47"); _svgColors.put("paleturquoise","175,238,238");
_svgColors.put("lightsteelblue","176,196,222"); _svgColors.put("powderblue","176,224,230"); _svgColors.put("firebrick","178,34,34");
_svgColors.put("darkgoldenrod","184,134,11"); _svgColors.put("mediumorchid","186,85,211"); _svgColors.put("rosybrown","188,143,143");
_svgColors.put("darkkhaki","189,183,107"); _svgColors.put("silver","192,192,192"); _svgColors.put("mediumvioletred","199,21,133");
_svgColors.put("indianred","205,92,92"); _svgColors.put("peru","205,133,63"); _svgColors.put("chocolate","210,105,30");
_svgColors.put("tan","210,180,140"); _svgColors.put("lightgray","211,211,211"); _svgColors.put("lightgrey","211,211,211");
_svgColors.put("thistle","216,191,216"); _svgColors.put("orchid","218,112,214"); _svgColors.put("goldenrod","218,165,32");
_svgColors.put("palevioletred","219,112,147"); _svgColors.put("crimson","220,20,60"); _svgColors.put("gainsboro","220,220,220");
_svgColors.put("plum","221,160,221"); _svgColors.put("burlywood","222,184,135"); _svgColors.put("lightcyan","224,255,255");
_svgColors.put("lavender","230,230,250"); _svgColors.put("darksalmon","233,150,122"); _svgColors.put("violet","238,130,238");
_svgColors.put("palegoldenrod","238,232,170"); _svgColors.put("lightcoral","240,128,128"); _svgColors.put("khaki","240,230,140");
_svgColors.put("aliceblue","240,248,255"); _svgColors.put("honeydew","240,255,240"); _svgColors.put("azure","240,255,255");
_svgColors.put("sandybrown","244,164,96"); _svgColors.put("wheat","245,222,179"); _svgColors.put("beige","245,245,220");
_svgColors.put("whitesmoke","245,245,245"); _svgColors.put("mintcream","245,255,250"); _svgColors.put("ghostwhite","248,248,255");
_svgColors.put("salmon","250,128,114"); _svgColors.put("antiquewhite","250,235,215"); _svgColors.put("linen","250,240,230");
_svgColors.put("lightgoldenrodyellow","250,250,210"); _svgColors.put("oldlace","253,245,230"); _svgColors.put("red","255,0,0");
_svgColors.put("fuchsia","255,0,255"); _svgColors.put("magenta","255,0,255"); _svgColors.put("deeppink","255,20,147");
_svgColors.put("orangered","255,69,0"); _svgColors.put("tomato","255,99,71"); _svgColors.put("hotpink","255,105,180");
_svgColors.put("coral","255,127,80"); _svgColors.put("darkorange","255,140,0"); _svgColors.put("lightsalmon","255,160,122");
_svgColors.put("orange","255,165,0"); _svgColors.put("lightpink","255,182,193"); _svgColors.put("pink","255,192,203");
_svgColors.put("gold","255,215,0"); _svgColors.put("peachpuff","255,218,185"); _svgColors.put("navajowhite","255,222,173");
_svgColors.put("moccasin","255,228,181"); _svgColors.put("bisque","255,228,196"); _svgColors.put("mistyrose","255,228,225");
_svgColors.put("blanchedalmond","255,235,205"); _svgColors.put("papayawhip","255,239,213"); _svgColors.put("lavenderblush","255,240,245");
_svgColors.put("seashell","255,245,238"); _svgColors.put("cornsilk","255,248,220"); _svgColors.put("lemonchiffon","255,250,205");
_svgColors.put("floralwhite","255,250,240"); _svgColors.put("snow","255,250,250"); _svgColors.put("yellow","255,255,0");
_svgColors.put("lightyellow","255,255,224"); _svgColors.put("ivory","255,255,240"); _svgColors.put("white","255,255,255");
}
/**
* @author vlad2928
* Method isADFRGBColorFormat()
*
* @param color - color string
* @return true - if string is ADF RGB formatted string
* false - otherwise
*/
public static boolean isADFRGBColorFormat(String color) {
return (color != null && color.replaceAll("\\s", "").matches("\\d{1,3},\\d{1,3},\\d{1,3}"));
}
/**
* @author vlad2928
* Method isSVGRGBColorFormat()
*
* @param color - color string
* @return true - if string is SVG RGB formatted string
* false - otherwise
*/
public static boolean isSVGRGBColorFormat(String color) {
return (color != null && color.replaceAll("\\s", "").matches("rgb\\(\\d{1,3},\\d{1,3},\\d{1,3}\\)"));
}
/**
* @author vlad2928
* Method isPatternLink()
*
* @param color - color string
* @return true - if string is pattern link formatted string
* false - otherwise
*/
public static boolean isPatternLink(String color) {
return (color != null && color.replaceAll("\\s", "").matches("url\\(#\\w+\\)"));
}
/**
* @author vlad2928
* Method isSVGStandardColor()
*
* @param color - color string
* @return true - if string is SVG standard color
* false - otherwise
*/
public static boolean isSVGStandardColor(String color) {
return (color != null && (_svgColors.containsKey(color) || _svgColors.containsValue(color)));
}
/**
* @author vlad2928
* Method parseColor() attempts to parse an input String into RGB color String in ADF format - "red,green,blue"
*
* @param color - color string to parse
* @return 1) "red,green,blue"-formatted string for [rgb(red,green,blue) | svg standard color name | red,green,blue]
* 2) link to pattern - for input like "url(#link to pattern)"
* 3) "none" - for "none" input
* 4) "" - for all other cases
*/
public static String parseColor(String color) {
if(color != null) {
color = color.replaceAll("\\s", "").toLowerCase();
if(isSVGRGBColorFormat(color)) // This is a SVG RGB color format
color = color.replaceAll("rgb", "").replaceAll("\\(", "").replaceAll("\\)", "");
if(isADFRGBColorFormat(color)) { // This is an ADF RGB color format
String[] rgb = color.split(",");
int red = SVGUtils.toInt(rgb[0], -1);
int green = SVGUtils.toInt(rgb[1], -1);
int blue = SVGUtils.toInt(rgb[2], -1);
if((red >= 0 && red <= 255) && (green >= 0 && green <= 255) && (blue >= 0 && blue <= 255))
return rgb[0] + "," + rgb[1] + "," + rgb[2];
}
if(color.equals("none")) return "none";
if(_svgColors.containsKey(color)) // This is a standard SVG color name
return _svgColors.get(color);
if(isPatternLink(color)) // This is a link to the pattern
return color.replaceAll("url", "").replaceAll("\\(#", "").replaceAll("\\)", "");
}
return "";
}
/**
* @author vlad2928
* @param rgb color to covert
* @return svg formatted color
*
*/
public static String toSVG(String rgbColor) {
if(isADFRGBColorFormat(rgbColor))
return "rgb(" + rgbColor + ")";
else if(isSVGStandardColor(rgbColor))
for(Iterator<Map.Entry<String, String>> i = _svgColors.entrySet().iterator(); i.hasNext();) {
Map.Entry<String,String> entry = i.next();
if(entry.getValue().equals(rgbColor))
return entry.getKey().toString();
}
return parseColor(rgbColor);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -