db9c0d9b2bac55f9bee47b84c72eebb1bcbf6231
[nikiroo-utils.git] / src / jexer / bits / Color.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
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.
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
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer.bits;
32
33 /**
34 * A text cell color.
35 */
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 }
52
53 /**
54 * Private constructor used to make the static Color instances.
55 *
56 * @param value the integer Color value
57 */
58 private Color(final int value) {
59 this.value = value;
60 }
61
62 /**
63 * Public constructor returns one of the static Color instances.
64 *
65 * @param colorName "red", "blue", etc.
66 * @return Color.RED, Color.BLUE, etc.
67 */
68 static Color getColor(final String colorName) {
69 String str = colorName.toLowerCase();
70
71 if (str.equals("black")) {
72 return Color.BLACK;
73 } else if (str.equals("white")) {
74 return Color.WHITE;
75 } else if (str.equals("red")) {
76 return Color.RED;
77 } else if (str.equals("cyan")) {
78 return Color.CYAN;
79 } else if (str.equals("green")) {
80 return Color.GREEN;
81 } else if (str.equals("magenta")) {
82 return Color.MAGENTA;
83 } else if (str.equals("blue")) {
84 return Color.BLUE;
85 } else if (str.equals("yellow")) {
86 return Color.YELLOW;
87 } else if (str.equals("brown")) {
88 return Color.YELLOW;
89 } else {
90 // Let unknown strings become white
91 return Color.WHITE;
92 }
93 }
94
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;
134
135 /**
136 * Black. Bold + black = dark grey
137 */
138 public static final Color BLACK = new Color(SGRBLACK);
139
140 /**
141 * Red.
142 */
143 public static final Color RED = new Color(SGRRED);
144
145 /**
146 * Green.
147 */
148 public static final Color GREEN = new Color(SGRGREEN);
149
150 /**
151 * Yellow. Sometimes not-bold yellow is brown.
152 */
153 public static final Color YELLOW = new Color(SGRYELLOW);
154
155 /**
156 * Blue.
157 */
158 public static final Color BLUE = new Color(SGRBLUE);
159
160 /**
161 * Magenta (purple).
162 */
163 public static final Color MAGENTA = new Color(SGRMAGENTA);
164
165 /**
166 * Cyan (blue-green).
167 */
168 public static final Color CYAN = new Color(SGRCYAN);
169
170 /**
171 * White.
172 */
173 public static final Color WHITE = new Color(SGRWHITE);
174
175 /**
176 * Invert a color in the same way as (CGA/VGA color XOR 0x7).
177 *
178 * @return the inverted color
179 */
180 public Color invert() {
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 }
201 }
202
203 /**
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
208 */
209 @Override
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 }
218
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
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";
253 default:
254 throw new IllegalArgumentException("Invalid Color value: " + value);
255 }
256 }
257
258 }