Commit | Line | Data |
---|---|---|
624ce48e KL |
1 | /** |
2 | * Jexer - Java Text User Interface | |
3 | * | |
624ce48e KL |
4 | * License: LGPLv3 or later |
5 | * | |
7b5261bc KL |
6 | * This module is licensed under the GNU Lesser General Public License |
7 | * Version 3. Please see the file "COPYING" in this directory for more | |
8 | * information about the GNU Lesser General Public License Version 3. | |
624ce48e KL |
9 | * |
10 | * Copyright (C) 2015 Kevin Lamonte | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Lesser General Public License | |
14 | * as published by the Free Software Foundation; either version 3 of | |
15 | * the License, or (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this program; if not, see | |
24 | * http://www.gnu.org/licenses/, or write to the Free Software | |
25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
26 | * 02110-1301 USA | |
7b5261bc KL |
27 | * |
28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
29 | * @version 1 | |
624ce48e KL |
30 | */ |
31 | package jexer.bits; | |
32 | ||
33 | /** | |
34 | * A text cell color. | |
35 | */ | |
7b5261bc KL |
36 | public final class Color { |
37 | ||
38 | /** | |
39 | * The color value. Default is SGRWHITE. | |
40 | */ | |
41 | private int value = SGRWHITE; | |
42 | ||
43 | /** | |
44 | * Get color value. Note that these deliberately match the color values | |
45 | * of the ECMA-48 / ANSI X3.64 / VT100-ish SGR function ("ANSI colors"). | |
46 | * | |
47 | * @return the value | |
48 | */ | |
49 | public int getValue() { | |
50 | return value; | |
51 | } | |
624ce48e KL |
52 | |
53 | /** | |
7b5261bc KL |
54 | * Private constructor used to make the static Color instances. |
55 | * | |
56 | * @param value the integer Color value | |
624ce48e | 57 | */ |
7b5261bc KL |
58 | private Color(final int value) { |
59 | this.value = value; | |
60 | } | |
624ce48e KL |
61 | |
62 | /** | |
7b5261bc KL |
63 | * Public constructor returns one of the static Color instances. |
64 | * | |
65 | * @param colorName "red", "blue", etc. | |
66 | * @return Color.RED, Color.BLUE, etc. | |
624ce48e | 67 | */ |
7b5261bc | 68 | static Color getColor(final String colorName) { |
0d47c546 KL |
69 | String str = colorName.toLowerCase(); |
70 | ||
71 | if (str.equals("black")) { | |
7b5261bc | 72 | return Color.BLACK; |
0d47c546 | 73 | } else if (str.equals("white")) { |
7b5261bc | 74 | return Color.WHITE; |
0d47c546 | 75 | } else if (str.equals("red")) { |
7b5261bc | 76 | return Color.RED; |
0d47c546 | 77 | } else if (str.equals("cyan")) { |
7b5261bc | 78 | return Color.CYAN; |
0d47c546 | 79 | } else if (str.equals("green")) { |
7b5261bc | 80 | return Color.GREEN; |
0d47c546 | 81 | } else if (str.equals("magenta")) { |
7b5261bc | 82 | return Color.MAGENTA; |
0d47c546 | 83 | } else if (str.equals("blue")) { |
7b5261bc | 84 | return Color.BLUE; |
0d47c546 | 85 | } else if (str.equals("yellow")) { |
7b5261bc | 86 | return Color.YELLOW; |
0d47c546 | 87 | } else if (str.equals("brown")) { |
7b5261bc | 88 | return Color.YELLOW; |
0d47c546 | 89 | } else { |
7b5261bc KL |
90 | // Let unknown strings become white |
91 | return Color.WHITE; | |
92 | } | |
624ce48e KL |
93 | } |
94 | ||
7b5261bc KL |
95 | /** |
96 | * SGR black value = 0. | |
97 | */ | |
98 | private static final int SGRBLACK = 0; | |
99 | ||
100 | /** | |
101 | * SGR red value = 1. | |
102 | */ | |
103 | private static final int SGRRED = 1; | |
104 | ||
105 | /** | |
106 | * SGR green value = 2. | |
107 | */ | |
108 | private static final int SGRGREEN = 2; | |
109 | ||
110 | /** | |
111 | * SGR yellow value = 3. | |
112 | */ | |
113 | private static final int SGRYELLOW = 3; | |
114 | ||
115 | /** | |
116 | * SGR blue value = 4. | |
117 | */ | |
118 | private static final int SGRBLUE = 4; | |
119 | ||
120 | /** | |
121 | * SGR magenta value = 5. | |
122 | */ | |
123 | private static final int SGRMAGENTA = 5; | |
124 | ||
125 | /** | |
126 | * SGR cyan value = 6. | |
127 | */ | |
128 | private static final int SGRCYAN = 6; | |
129 | ||
130 | /** | |
131 | * SGR white value = 7. | |
132 | */ | |
133 | private static final int SGRWHITE = 7; | |
624ce48e KL |
134 | |
135 | /** | |
136 | * Black. Bold + black = dark grey | |
137 | */ | |
7b5261bc | 138 | public static final Color BLACK = new Color(SGRBLACK); |
624ce48e KL |
139 | |
140 | /** | |
7b5261bc | 141 | * Red. |
624ce48e | 142 | */ |
7b5261bc | 143 | public static final Color RED = new Color(SGRRED); |
624ce48e KL |
144 | |
145 | /** | |
7b5261bc | 146 | * Green. |
624ce48e | 147 | */ |
7b5261bc | 148 | public static final Color GREEN = new Color(SGRGREEN); |
624ce48e KL |
149 | |
150 | /** | |
151 | * Yellow. Sometimes not-bold yellow is brown. | |
152 | */ | |
7b5261bc | 153 | public static final Color YELLOW = new Color(SGRYELLOW); |
624ce48e KL |
154 | |
155 | /** | |
7b5261bc | 156 | * Blue. |
624ce48e | 157 | */ |
7b5261bc | 158 | public static final Color BLUE = new Color(SGRBLUE); |
624ce48e KL |
159 | |
160 | /** | |
7b5261bc | 161 | * Magenta (purple). |
624ce48e | 162 | */ |
7b5261bc | 163 | public static final Color MAGENTA = new Color(SGRMAGENTA); |
624ce48e KL |
164 | |
165 | /** | |
7b5261bc | 166 | * Cyan (blue-green). |
624ce48e | 167 | */ |
7b5261bc | 168 | public static final Color CYAN = new Color(SGRCYAN); |
624ce48e KL |
169 | |
170 | /** | |
7b5261bc | 171 | * White. |
624ce48e | 172 | */ |
7b5261bc | 173 | public static final Color WHITE = new Color(SGRWHITE); |
624ce48e KL |
174 | |
175 | /** | |
176 | * Invert a color in the same way as (CGA/VGA color XOR 0x7). | |
7b5261bc | 177 | * |
624ce48e KL |
178 | * @return the inverted color |
179 | */ | |
4328bb42 | 180 | public Color invert() { |
7b5261bc KL |
181 | switch (value) { |
182 | case SGRBLACK: | |
183 | return Color.WHITE; | |
184 | case SGRWHITE: | |
185 | return Color.BLACK; | |
186 | case SGRRED: | |
187 | return Color.CYAN; | |
188 | case SGRCYAN: | |
189 | return Color.RED; | |
190 | case SGRGREEN: | |
191 | return Color.MAGENTA; | |
192 | case SGRMAGENTA: | |
193 | return Color.GREEN; | |
194 | case SGRBLUE: | |
195 | return Color.YELLOW; | |
196 | case SGRYELLOW: | |
197 | return Color.BLUE; | |
198 | default: | |
199 | throw new IllegalArgumentException("Invalid Color value: " + value); | |
200 | } | |
4328bb42 KL |
201 | } |
202 | ||
203 | /** | |
7b5261bc KL |
204 | * Comparison check. All fields must match to return true. |
205 | * | |
206 | * @param rhs another Color instance | |
207 | * @return true if all fields are equal | |
4328bb42 KL |
208 | */ |
209 | @Override | |
7b5261bc KL |
210 | public boolean equals(final Object rhs) { |
211 | if (!(rhs instanceof Color)) { | |
212 | return false; | |
213 | } | |
214 | ||
215 | Color that = (Color) rhs; | |
216 | return (value == that.value); | |
217 | } | |
4328bb42 | 218 | |
e826b451 KL |
219 | /** |
220 | * Hashcode uses all fields in equals(). | |
221 | * | |
222 | * @return the hash | |
223 | */ | |
224 | @Override | |
225 | public int hashCode() { | |
226 | return value; | |
227 | } | |
228 | ||
7b5261bc KL |
229 | /** |
230 | * Make human-readable description of this Color. | |
231 | * | |
232 | * @return displayable String "red", "blue", etc. | |
233 | */ | |
234 | @Override | |
235 | public String toString() { | |
236 | switch (value) { | |
237 | case SGRBLACK: | |
238 | return "black"; | |
239 | case SGRWHITE: | |
240 | return "white"; | |
241 | case SGRRED: | |
242 | return "red"; | |
243 | case SGRCYAN: | |
244 | return "cyan"; | |
245 | case SGRGREEN: | |
246 | return "green"; | |
247 | case SGRMAGENTA: | |
248 | return "magenta"; | |
249 | case SGRBLUE: | |
250 | return "blue"; | |
251 | case SGRYELLOW: | |
252 | return "yellow"; | |
e826b451 KL |
253 | default: |
254 | throw new IllegalArgumentException("Invalid Color value: " + value); | |
7b5261bc | 255 | } |
624ce48e | 256 | } |
7b5261bc | 257 | |
624ce48e | 258 | } |