1 25 package jsp2_1.examples.elresolver; 26 27 import java.io.BufferedReader ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.StringTokenizer ; 34 35 40 public class ColorImplicitObject { 41 42 43 private static HashMap colorNames = null; 44 45 48 public static ColorRGB fromHex(String hex) { 49 return fromColor(java.awt.Color.decode(hex)); 50 } 51 52 55 public static ColorRGB fromColor(java.awt.Color color) { 56 return new ColorRGB(color.getRed(), color.getGreen(), color.getBlue()); 57 } 58 59 63 public static ColorRGB fromName(String name) { 64 if(colorNames == null) { 65 loadColorNames(); 66 } 67 return (ColorRGB)colorNames.get(name); 68 } 69 70 public String toString() { 71 return "Color Implicit Object"; 72 } 73 74 77 static Iterator colorNameIterator() { 78 if(colorNames == null) { 79 loadColorNames(); 80 } 81 return colorNames.keySet().iterator(); 82 } 83 84 88 private synchronized static void loadColorNames() { 89 if(colorNames == null) { 90 colorNames = new HashMap (); 91 try { 92 BufferedReader in = new BufferedReader (new InputStreamReader ( 93 ColorImplicitObject.class.getResourceAsStream( 94 "/jsp2_1/examples/elresolver/rgb.txt"))); 95 String line; 96 while((line = in.readLine()) != null) { 97 if(!line.startsWith("!")) { 98 String colorText = line.substring(0, 12); 99 String colorName = line.substring(12).trim(); 100 StringTokenizer st = new StringTokenizer ( 101 colorText, " "); 102 int red = Integer.parseInt(st.nextToken().trim()); 103 int green = Integer.parseInt(st.nextToken().trim()); 104 int blue = Integer.parseInt(st.nextToken().trim()); 105 colorNames.put(colorName, new ColorRGB(red, green, 106 blue)); 107 } 108 } 109 in.close(); 110 } 111 catch(IOException e) { 112 throw new RuntimeException ("Could not load rgb.txt", e); 113 } 114 } 115 } 116 117 121 public static class ColorR { 122 private int red; 123 public ColorR(int red) { 124 if(red < 0) red = 0; 126 if(red > 255) red = 255; 127 this.red = red; 128 } 129 public int getRed() { 130 return red; 131 } 132 public void setRed(int r) { 133 red = r; 134 } 135 public String toString() { 136 return "Color(" + red + ", ?, ?)"; 137 } 138 } 139 140 144 public static class ColorRG 145 extends ColorR 146 { 147 private int green; 148 public ColorRG(int red, int green) { 149 super(red); 150 if(green < 0) green = 0; 152 if(green > 255) green = 255; 153 this.green = green; 154 } 155 public int getGreen() { 156 return green; 157 } 158 public void setGreen(int g) { 159 green = g; 160 } 161 public String toString() { 162 return "Color(" + getRed() + ", " + green + ", ?)"; 163 } 164 } 165 166 public static class ColorRGB 167 extends ColorRG 168 { 169 private int blue; 170 public ColorRGB(int red, int green, int blue) { 171 super(red, green); 172 if(blue < 0) blue = 0; 174 if(blue > 255) blue = 255; 175 this.blue = blue; 176 } 177 public int getBlue() { 178 return blue; 179 } 180 public void setBlue(int b) { 181 blue = blue; 182 } 183 public java.awt.Color getColor() { 184 return new java.awt.Color (getRed(), getGreen(), getBlue()); 185 } 186 187 public ColorRGB getDarker() { 188 java.awt.Color darkerColor = getColor().darker(); 189 return fromColor(darkerColor); 190 } 191 192 public ColorRGB getBrighter() { 193 java.awt.Color brighterColor = getColor().brighter(); 194 return fromColor(brighterColor); 195 } 196 197 public String getHex() { 198 return "#" + toHex(getRed()) + toHex(getGreen()) + 199 toHex(getBlue()); 200 } 201 202 private String toHex(int i) { 203 String result; 204 if(i < 16) { 205 result = "0" + Integer.toHexString(i); 206 } 207 else { 208 result = Integer.toHexString(i); 209 } 210 return result; 211 } 212 213 public String toString() { 214 return "Color(" + getRed() + ", " + getGreen() + ", " + blue + ")"; 215 } 216 } 217 } 218 | Popular Tags |