Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
624ce48e KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
624ce48e | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
624ce48e | 7 | * |
e16dda65 KL |
8 | * Permission is hereby granted, free of charge, to any person obtaining a |
9 | * copy of this software and associated documentation files (the "Software"), | |
10 | * to deal in the Software without restriction, including without limitation | |
11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | * and/or sell copies of the Software, and to permit persons to whom the | |
13 | * Software is furnished to do so, subject to the following conditions: | |
624ce48e | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
624ce48e | 17 | * |
e16dda65 KL |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
24 | * DEALINGS IN THE SOFTWARE. | |
7b5261bc KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
624ce48e KL |
28 | */ |
29 | package jexer.bits; | |
30 | ||
31 | /** | |
32 | * A text cell color. | |
33 | */ | |
7b5261bc KL |
34 | public final class Color { |
35 | ||
d36057df KL |
36 | // ------------------------------------------------------------------------ |
37 | // Constants -------------------------------------------------------------- | |
38 | // ------------------------------------------------------------------------ | |
624ce48e | 39 | |
7b5261bc KL |
40 | /** |
41 | * SGR black value = 0. | |
42 | */ | |
43 | private static final int SGRBLACK = 0; | |
44 | ||
45 | /** | |
46 | * SGR red value = 1. | |
47 | */ | |
48 | private static final int SGRRED = 1; | |
49 | ||
50 | /** | |
51 | * SGR green value = 2. | |
52 | */ | |
53 | private static final int SGRGREEN = 2; | |
54 | ||
55 | /** | |
56 | * SGR yellow value = 3. | |
57 | */ | |
58 | private static final int SGRYELLOW = 3; | |
59 | ||
60 | /** | |
61 | * SGR blue value = 4. | |
62 | */ | |
63 | private static final int SGRBLUE = 4; | |
64 | ||
65 | /** | |
66 | * SGR magenta value = 5. | |
67 | */ | |
68 | private static final int SGRMAGENTA = 5; | |
69 | ||
70 | /** | |
71 | * SGR cyan value = 6. | |
72 | */ | |
73 | private static final int SGRCYAN = 6; | |
74 | ||
75 | /** | |
76 | * SGR white value = 7. | |
77 | */ | |
78 | private static final int SGRWHITE = 7; | |
624ce48e KL |
79 | |
80 | /** | |
81 | * Black. Bold + black = dark grey | |
82 | */ | |
7b5261bc | 83 | public static final Color BLACK = new Color(SGRBLACK); |
624ce48e KL |
84 | |
85 | /** | |
7b5261bc | 86 | * Red. |
624ce48e | 87 | */ |
7b5261bc | 88 | public static final Color RED = new Color(SGRRED); |
624ce48e KL |
89 | |
90 | /** | |
7b5261bc | 91 | * Green. |
624ce48e | 92 | */ |
7b5261bc | 93 | public static final Color GREEN = new Color(SGRGREEN); |
624ce48e KL |
94 | |
95 | /** | |
96 | * Yellow. Sometimes not-bold yellow is brown. | |
97 | */ | |
7b5261bc | 98 | public static final Color YELLOW = new Color(SGRYELLOW); |
624ce48e KL |
99 | |
100 | /** | |
7b5261bc | 101 | * Blue. |
624ce48e | 102 | */ |
7b5261bc | 103 | public static final Color BLUE = new Color(SGRBLUE); |
624ce48e KL |
104 | |
105 | /** | |
7b5261bc | 106 | * Magenta (purple). |
624ce48e | 107 | */ |
7b5261bc | 108 | public static final Color MAGENTA = new Color(SGRMAGENTA); |
624ce48e KL |
109 | |
110 | /** | |
7b5261bc | 111 | * Cyan (blue-green). |
624ce48e | 112 | */ |
7b5261bc | 113 | public static final Color CYAN = new Color(SGRCYAN); |
624ce48e KL |
114 | |
115 | /** | |
7b5261bc | 116 | * White. |
624ce48e | 117 | */ |
7b5261bc | 118 | public static final Color WHITE = new Color(SGRWHITE); |
624ce48e | 119 | |
d36057df KL |
120 | // ------------------------------------------------------------------------ |
121 | // Variables -------------------------------------------------------------- | |
122 | // ------------------------------------------------------------------------ | |
123 | ||
124 | /** | |
125 | * The color value. Default is SGRWHITE. | |
126 | */ | |
127 | private int value = SGRWHITE; | |
128 | ||
129 | // ------------------------------------------------------------------------ | |
130 | // Constructors ----------------------------------------------------------- | |
131 | // ------------------------------------------------------------------------ | |
132 | ||
133 | /** | |
134 | * Private constructor used to make the static Color instances. | |
135 | * | |
136 | * @param value the integer Color value | |
137 | */ | |
138 | private Color(final int value) { | |
139 | this.value = value; | |
140 | } | |
141 | ||
142 | // ------------------------------------------------------------------------ | |
143 | // Color ------------------------------------------------------------------ | |
144 | // ------------------------------------------------------------------------ | |
145 | ||
146 | /** | |
147 | * Get color value. Note that these deliberately match the color values | |
148 | * of the ECMA-48 / ANSI X3.64 / VT100-ish SGR function ("ANSI colors"). | |
149 | * | |
150 | * @return the value | |
151 | */ | |
152 | public int getValue() { | |
153 | return value; | |
154 | } | |
155 | ||
156 | /** | |
157 | * Public constructor returns one of the static Color instances. | |
158 | * | |
159 | * @param colorName "red", "blue", etc. | |
160 | * @return Color.RED, Color.BLUE, etc. | |
161 | */ | |
162 | static Color getColor(final String colorName) { | |
163 | String str = colorName.toLowerCase(); | |
164 | ||
165 | if (str.equals("black")) { | |
166 | return Color.BLACK; | |
167 | } else if (str.equals("white")) { | |
168 | return Color.WHITE; | |
169 | } else if (str.equals("red")) { | |
170 | return Color.RED; | |
171 | } else if (str.equals("cyan")) { | |
172 | return Color.CYAN; | |
173 | } else if (str.equals("green")) { | |
174 | return Color.GREEN; | |
175 | } else if (str.equals("magenta")) { | |
176 | return Color.MAGENTA; | |
177 | } else if (str.equals("blue")) { | |
178 | return Color.BLUE; | |
179 | } else if (str.equals("yellow")) { | |
180 | return Color.YELLOW; | |
181 | } else if (str.equals("brown")) { | |
182 | return Color.YELLOW; | |
183 | } else { | |
184 | // Let unknown strings become white | |
185 | return Color.WHITE; | |
186 | } | |
187 | } | |
188 | ||
624ce48e KL |
189 | /** |
190 | * Invert a color in the same way as (CGA/VGA color XOR 0x7). | |
7b5261bc | 191 | * |
624ce48e KL |
192 | * @return the inverted color |
193 | */ | |
4328bb42 | 194 | public Color invert() { |
7b5261bc KL |
195 | switch (value) { |
196 | case SGRBLACK: | |
197 | return Color.WHITE; | |
198 | case SGRWHITE: | |
199 | return Color.BLACK; | |
200 | case SGRRED: | |
201 | return Color.CYAN; | |
202 | case SGRCYAN: | |
203 | return Color.RED; | |
204 | case SGRGREEN: | |
205 | return Color.MAGENTA; | |
206 | case SGRMAGENTA: | |
207 | return Color.GREEN; | |
208 | case SGRBLUE: | |
209 | return Color.YELLOW; | |
210 | case SGRYELLOW: | |
211 | return Color.BLUE; | |
212 | default: | |
213 | throw new IllegalArgumentException("Invalid Color value: " + value); | |
214 | } | |
4328bb42 KL |
215 | } |
216 | ||
217 | /** | |
7b5261bc KL |
218 | * Comparison check. All fields must match to return true. |
219 | * | |
220 | * @param rhs another Color instance | |
221 | * @return true if all fields are equal | |
4328bb42 KL |
222 | */ |
223 | @Override | |
7b5261bc KL |
224 | public boolean equals(final Object rhs) { |
225 | if (!(rhs instanceof Color)) { | |
226 | return false; | |
227 | } | |
228 | ||
229 | Color that = (Color) rhs; | |
230 | return (value == that.value); | |
231 | } | |
4328bb42 | 232 | |
e826b451 KL |
233 | /** |
234 | * Hashcode uses all fields in equals(). | |
235 | * | |
236 | * @return the hash | |
237 | */ | |
238 | @Override | |
239 | public int hashCode() { | |
240 | return value; | |
241 | } | |
242 | ||
7b5261bc KL |
243 | /** |
244 | * Make human-readable description of this Color. | |
245 | * | |
246 | * @return displayable String "red", "blue", etc. | |
247 | */ | |
248 | @Override | |
249 | public String toString() { | |
250 | switch (value) { | |
251 | case SGRBLACK: | |
252 | return "black"; | |
253 | case SGRWHITE: | |
254 | return "white"; | |
255 | case SGRRED: | |
256 | return "red"; | |
257 | case SGRCYAN: | |
258 | return "cyan"; | |
259 | case SGRGREEN: | |
260 | return "green"; | |
261 | case SGRMAGENTA: | |
262 | return "magenta"; | |
263 | case SGRBLUE: | |
264 | return "blue"; | |
265 | case SGRYELLOW: | |
266 | return "yellow"; | |
e826b451 KL |
267 | default: |
268 | throw new IllegalArgumentException("Invalid Color value: " + value); | |
7b5261bc | 269 | } |
624ce48e | 270 | } |
7b5261bc | 271 | |
624ce48e | 272 | } |