1 package org.catacomb.util;
2
3
4 import org.catacomb.report.E;
5
6
7 import java.awt.Color;
8
9
10 public class ColorUtil {
11
12 static StandardColors standardColors;
13
14 static String[] stdColorNames = {"white", "black",
15 "red", "green", "blue",
16 "magenta", "cyan", "yellow",
17 "orange", "pink", "gray"
18 };
19 static Color[] stdColors = {Color.white, Color.black,
20 Color.red, Color.green, Color.blue,
21 Color.magenta, Color.cyan, Color.yellow,
22 Color.orange, Color.pink, Color.gray
23 };
24
25
26
27
28 public final static Color brighter(Color c) {
29 return linMod(c, 30);
30 }
31
32
33 public final static Color darker(Color c) {
34 return linMod(c, -30);
35 }
36
37
38 public final static Color slightlyBrighter(Color c) {
39 return linMod(c, 16);
40 }
41
42
43 public final static Color slightlyDarker(Color c) {
44 return linMod(c, -16);
45 }
46
47
48 public final static Color verySlightlyBrighter(Color c) {
49 Color ret = linMod(c, 8);
50 return ret;
51 }
52
53
54
55 public final static Color verySlightlyDarker(Color c) {
56 Color ret = linMod(c, -8);
57 return ret;
58 }
59
60
61 public final static Color myBrighter(Color c) {
62 return linMod(c, 35);
63 }
64
65
66 public final static Color myDarker(Color c) {
67 return linMod(c, -35);
68 }
69
70
71 public final static Color linMod(Color c, int d) {
72 int r = c.getRed();
73 int g = c.getGreen();
74 int b = c.getBlue();
75
76 r += d;
77 g += d;
78 b += d;
79 r = (r > 0 ? (r < 255 ? r : 255) : 0);
80 g = (g > 0 ? (g < 255 ? g : 255) : 0);
81 b = (b > 0 ? (b < 255 ? b : 255) : 0);
82 return new Color(r, g, b);
83 }
84
85
86
87 public static Color deserialize(String s) {
88 return parseColor(s);
89 }
90
91
92 public static Color parseColor(String s) {
93 Color cret = Color.red;
94
95 if (s == null || s.length() == 0) {
96 cret = Color.orange;
97 E.warning("asked to parse empty color");
98
99 } else if (s.startsWith("#")) {
100 cret = parseHexColor(s);
101
102 } else {
103
104 cret = simpleLookup(s);
105
106 if (cret == null) {
107
108 if (standardColors == null) {
109
110 Object obj = null;
111 E.missing("need to load standard cols from resource");
112
113 standardColors = (StandardColors)obj;
114 }
115
116 if (standardColors.defines(s)) {
117 cret = standardColors.getColor(s);
118 } else {
119 cret = parseHexColor(s);
120 }
121 }
122 }
123 return cret;
124 }
125
126
127 private static Color simpleLookup(String s) {
128 Color ret = null;
129 for (int i = 0; i < stdColors.length; i++) {
130 if (s.equals(stdColorNames[i])) {
131 ret = stdColors[i];
132 break;
133 }
134
135 }
136 return ret;
137 }
138
139
140
141 public static Color parseHexColor(String s) {
142 Color cret = null;
143 try {
144 int ic = Integer.decode(s).intValue();
145 cret = new Color(ic);
146
147 } catch (NumberFormatException ex) {
148 E.error("cant decode color string " + s);
149 cret = Color.red;
150 }
151 return cret;
152 }
153
154
155 public static Color oldParseColor(String s) {
156 Color ret = Color.black;
157 if (s != null && s.startsWith("#") && s.length() == 7) {
158 String s1 = s.substring(1, s.length());
159 int icol = Integer.parseInt(s1, 16);
160 ret = new Color(icol);
161
162 } else {
163 E.error(" - cant read color " + s);
164
165 }
166 return ret;
167 }
168
169
170
171 public static String serializeColor(Color c) {
172 String ret = null;
173 if (c == null) {
174 ret = "#000000";
175 } else {
176 int rgb = c.getRGB();
177
178
179 int xrgb = rgb | 0xff000000;
180
181 String fullhex = Integer.toHexString(xrgb);
182 ret = "#" + fullhex.substring(2, 8);
183 }
184 return ret;
185 }
186
187
188 public static String hexString(Color c) {
189 return serializeColor(c);
190
191
192
193
194
195 }
196
197
198
199 }