Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
624ce48e KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
624ce48e | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 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 | ||
36 | /** | |
37 | * The color value. Default is SGRWHITE. | |
38 | */ | |
39 | private int value = SGRWHITE; | |
40 | ||
41 | /** | |
42 | * Get color value. Note that these deliberately match the color values | |
43 | * of the ECMA-48 / ANSI X3.64 / VT100-ish SGR function ("ANSI colors"). | |
44 | * | |
45 | * @return the value | |
46 | */ | |
47 | public int getValue() { | |
48 | return value; | |
49 | } | |
624ce48e KL |
50 | |
51 | /** | |
7b5261bc KL |
52 | * Private constructor used to make the static Color instances. |
53 | * | |
54 | * @param value the integer Color value | |
624ce48e | 55 | */ |
7b5261bc KL |
56 | private Color(final int value) { |
57 | this.value = value; | |
58 | } | |
624ce48e KL |
59 | |
60 | /** | |
7b5261bc KL |
61 | * Public constructor returns one of the static Color instances. |
62 | * | |
63 | * @param colorName "red", "blue", etc. | |
64 | * @return Color.RED, Color.BLUE, etc. | |
624ce48e | 65 | */ |
7b5261bc | 66 | static Color getColor(final String colorName) { |
0d47c546 KL |
67 | String str = colorName.toLowerCase(); |
68 | ||
69 | if (str.equals("black")) { | |
7b5261bc | 70 | return Color.BLACK; |
0d47c546 | 71 | } else if (str.equals("white")) { |
7b5261bc | 72 | return Color.WHITE; |
0d47c546 | 73 | } else if (str.equals("red")) { |
7b5261bc | 74 | return Color.RED; |
0d47c546 | 75 | } else if (str.equals("cyan")) { |
7b5261bc | 76 | return Color.CYAN; |
0d47c546 | 77 | } else if (str.equals("green")) { |
7b5261bc | 78 | return Color.GREEN; |
0d47c546 | 79 | } else if (str.equals("magenta")) { |
7b5261bc | 80 | return Color.MAGENTA; |
0d47c546 | 81 | } else if (str.equals("blue")) { |
7b5261bc | 82 | return Color.BLUE; |
0d47c546 | 83 | } else if (str.equals("yellow")) { |
7b5261bc | 84 | return Color.YELLOW; |
0d47c546 | 85 | } else if (str.equals("brown")) { |
7b5261bc | 86 | return Color.YELLOW; |
0d47c546 | 87 | } else { |
7b5261bc KL |
88 | // Let unknown strings become white |
89 | return Color.WHITE; | |
90 | } | |
624ce48e KL |
91 | } |
92 | ||
7b5261bc KL |
93 | /** |
94 | * SGR black value = 0. | |
95 | */ | |
96 | private static final int SGRBLACK = 0; | |
97 | ||
98 | /** | |
99 | * SGR red value = 1. | |
100 | */ | |
101 | private static final int SGRRED = 1; | |
102 | ||
103 | /** | |
104 | * SGR green value = 2. | |
105 | */ | |
106 | private static final int SGRGREEN = 2; | |
107 | ||
108 | /** | |
109 | * SGR yellow value = 3. | |
110 | */ | |
111 | private static final int SGRYELLOW = 3; | |
112 | ||
113 | /** | |
114 | * SGR blue value = 4. | |
115 | */ | |
116 | private static final int SGRBLUE = 4; | |
117 | ||
118 | /** | |
119 | * SGR magenta value = 5. | |
120 | */ | |
121 | private static final int SGRMAGENTA = 5; | |
122 | ||
123 | /** | |
124 | * SGR cyan value = 6. | |
125 | */ | |
126 | private static final int SGRCYAN = 6; | |
127 | ||
128 | /** | |
129 | * SGR white value = 7. | |
130 | */ | |
131 | private static final int SGRWHITE = 7; | |
624ce48e KL |
132 | |
133 | /** | |
134 | * Black. Bold + black = dark grey | |
135 | */ | |
7b5261bc | 136 | public static final Color BLACK = new Color(SGRBLACK); |
624ce48e KL |
137 | |
138 | /** | |
7b5261bc | 139 | * Red. |
624ce48e | 140 | */ |
7b5261bc | 141 | public static final Color RED = new Color(SGRRED); |
624ce48e KL |
142 | |
143 | /** | |
7b5261bc | 144 | * Green. |
624ce48e | 145 | */ |
7b5261bc | 146 | public static final Color GREEN = new Color(SGRGREEN); |
624ce48e KL |
147 | |
148 | /** | |
149 | * Yellow. Sometimes not-bold yellow is brown. | |
150 | */ | |
7b5261bc | 151 | public static final Color YELLOW = new Color(SGRYELLOW); |
624ce48e KL |
152 | |
153 | /** | |
7b5261bc | 154 | * Blue. |
624ce48e | 155 | */ |
7b5261bc | 156 | public static final Color BLUE = new Color(SGRBLUE); |
624ce48e KL |
157 | |
158 | /** | |
7b5261bc | 159 | * Magenta (purple). |
624ce48e | 160 | */ |
7b5261bc | 161 | public static final Color MAGENTA = new Color(SGRMAGENTA); |
624ce48e KL |
162 | |
163 | /** | |
7b5261bc | 164 | * Cyan (blue-green). |
624ce48e | 165 | */ |
7b5261bc | 166 | public static final Color CYAN = new Color(SGRCYAN); |
624ce48e KL |
167 | |
168 | /** | |
7b5261bc | 169 | * White. |
624ce48e | 170 | */ |
7b5261bc | 171 | public static final Color WHITE = new Color(SGRWHITE); |
624ce48e KL |
172 | |
173 | /** | |
174 | * Invert a color in the same way as (CGA/VGA color XOR 0x7). | |
7b5261bc | 175 | * |
624ce48e KL |
176 | * @return the inverted color |
177 | */ | |
4328bb42 | 178 | public Color invert() { |
7b5261bc KL |
179 | switch (value) { |
180 | case SGRBLACK: | |
181 | return Color.WHITE; | |
182 | case SGRWHITE: | |
183 | return Color.BLACK; | |
184 | case SGRRED: | |
185 | return Color.CYAN; | |
186 | case SGRCYAN: | |
187 | return Color.RED; | |
188 | case SGRGREEN: | |
189 | return Color.MAGENTA; | |
190 | case SGRMAGENTA: | |
191 | return Color.GREEN; | |
192 | case SGRBLUE: | |
193 | return Color.YELLOW; | |
194 | case SGRYELLOW: | |
195 | return Color.BLUE; | |
196 | default: | |
197 | throw new IllegalArgumentException("Invalid Color value: " + value); | |
198 | } | |
4328bb42 KL |
199 | } |
200 | ||
201 | /** | |
7b5261bc KL |
202 | * Comparison check. All fields must match to return true. |
203 | * | |
204 | * @param rhs another Color instance | |
205 | * @return true if all fields are equal | |
4328bb42 KL |
206 | */ |
207 | @Override | |
7b5261bc KL |
208 | public boolean equals(final Object rhs) { |
209 | if (!(rhs instanceof Color)) { | |
210 | return false; | |
211 | } | |
212 | ||
213 | Color that = (Color) rhs; | |
214 | return (value == that.value); | |
215 | } | |
4328bb42 | 216 | |
e826b451 KL |
217 | /** |
218 | * Hashcode uses all fields in equals(). | |
219 | * | |
220 | * @return the hash | |
221 | */ | |
222 | @Override | |
223 | public int hashCode() { | |
224 | return value; | |
225 | } | |
226 | ||
7b5261bc KL |
227 | /** |
228 | * Make human-readable description of this Color. | |
229 | * | |
230 | * @return displayable String "red", "blue", etc. | |
231 | */ | |
232 | @Override | |
233 | public String toString() { | |
234 | switch (value) { | |
235 | case SGRBLACK: | |
236 | return "black"; | |
237 | case SGRWHITE: | |
238 | return "white"; | |
239 | case SGRRED: | |
240 | return "red"; | |
241 | case SGRCYAN: | |
242 | return "cyan"; | |
243 | case SGRGREEN: | |
244 | return "green"; | |
245 | case SGRMAGENTA: | |
246 | return "magenta"; | |
247 | case SGRBLUE: | |
248 | return "blue"; | |
249 | case SGRYELLOW: | |
250 | return "yellow"; | |
e826b451 KL |
251 | default: |
252 | throw new IllegalArgumentException("Invalid Color value: " + value); | |
7b5261bc | 253 | } |
624ce48e | 254 | } |
7b5261bc | 255 | |
624ce48e | 256 | } |