Frames | No Frames |
1: /* CSSColor.java -- Converts CSS color values 2: Copyright (C) 2006 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.javax.swing.text.html.css; 40: 41: import java.awt.Color; 42: import java.util.HashMap; 43: import java.util.Iterator; 44: import java.util.Set; 45: 46: /** 47: * Converts CSS color values into AWT Color values. 48: * 49: * @author Roman Kennke (kennke@aicas.com) 50: */ 51: public class CSSColor 52: { 53: 54: private static final HashMap COLOR_MAP; 55: static 56: { 57: COLOR_MAP = new HashMap(); 58: COLOR_MAP.put("maroon", "#800000"); 59: COLOR_MAP.put("red", "#ff0000"); 60: COLOR_MAP.put("orange", "#ffa500"); 61: COLOR_MAP.put("yellow", "#ffff00"); 62: COLOR_MAP.put("olive", "#808000"); 63: COLOR_MAP.put("purple", "#800080"); 64: COLOR_MAP.put("fuchsia", "#ff00ff"); 65: COLOR_MAP.put("white", "#ffffff"); 66: COLOR_MAP.put("lime", "#00ff00"); 67: COLOR_MAP.put("green", "#008000"); 68: COLOR_MAP.put("navy", "#000080"); 69: COLOR_MAP.put("blue", "#0000ff"); 70: COLOR_MAP.put("aqua", "#00ffff"); 71: COLOR_MAP.put("teal", "#008080"); 72: COLOR_MAP.put("black", "#000000"); 73: COLOR_MAP.put("silver", "#c0c0c0"); 74: COLOR_MAP.put("gray", "#808080"); 75: } 76: 77: /** 78: * The CSS value. 79: */ 80: private String value; 81: 82: /** 83: * The converted color. 84: */ 85: private Color color; 86: 87: /** 88: * Creates a new instance. 89: * 90: * @param val the CSS value 91: */ 92: public CSSColor(String val) 93: { 94: value = val; 95: color = convertValue(value); 96: } 97: 98: /** 99: * Converts a CSS color value to an AWT color. 100: * 101: * @param value the CSS color value 102: * 103: * @return the converted color value 104: */ 105: public static Color convertValue(String value) 106: { 107: Color color; 108: String val1 = value.toLowerCase(); 109: if (val1.charAt(0) != '#') 110: val1 = (String) COLOR_MAP.get(val1); 111: if (val1 != null) 112: { 113: String hexVal = val1.substring(1).trim(); 114: try 115: { 116: int rgb = Integer.parseInt(hexVal, 16); 117: color = new Color(rgb); 118: } 119: catch (NumberFormatException ex) 120: { 121: color = Color.BLACK; 122: } 123: } 124: else 125: color = null; 126: return color; 127: } 128: 129: /** 130: * Returns the converted color. 131: * 132: * @return the converted color 133: */ 134: public Color getValue() 135: { 136: return color; 137: } 138: 139: public String toString() 140: { 141: return value; 142: } 143: 144: /** 145: * Returns <code>true</code> if the specified value is a valid color value, 146: * <code>false</code> otherwise. 147: * 148: * @param val the value to check 149: * 150: * @return <code>true</code> if the specified value is a valid color value, 151: * <code>false</code> otherwise 152: */ 153: public static boolean isValidColor(String val) 154: { 155: boolean ret = false; 156: if (val.charAt(0) == '#') 157: ret = true; 158: else 159: { 160: Set colors = COLOR_MAP.keySet(); 161: for (Iterator i = colors.iterator(); i.hasNext() && ret == false;) 162: { 163: String color = (String) i.next(); 164: if (color.equalsIgnoreCase(val)) 165: ret = true; 166: } 167: } 168: return ret; 169: } 170: }