Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[fanfix.git] / src / jexer / bits / Color.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.bits;
30
31 /**
32 * A text cell color.
33 */
34 public final class Color {
35
36 // ------------------------------------------------------------------------
37 // Constants --------------------------------------------------------------
38 // ------------------------------------------------------------------------
39
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;
79
80 /**
81 * Black. Bold + black = dark grey
82 */
83 public static final Color BLACK = new Color(SGRBLACK);
84
85 /**
86 * Red.
87 */
88 public static final Color RED = new Color(SGRRED);
89
90 /**
91 * Green.
92 */
93 public static final Color GREEN = new Color(SGRGREEN);
94
95 /**
96 * Yellow. Sometimes not-bold yellow is brown.
97 */
98 public static final Color YELLOW = new Color(SGRYELLOW);
99
100 /**
101 * Blue.
102 */
103 public static final Color BLUE = new Color(SGRBLUE);
104
105 /**
106 * Magenta (purple).
107 */
108 public static final Color MAGENTA = new Color(SGRMAGENTA);
109
110 /**
111 * Cyan (blue-green).
112 */
113 public static final Color CYAN = new Color(SGRCYAN);
114
115 /**
116 * White.
117 */
118 public static final Color WHITE = new Color(SGRWHITE);
119
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
189 /**
190 * Invert a color in the same way as (CGA/VGA color XOR 0x7).
191 *
192 * @return the inverted color
193 */
194 public Color invert() {
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 }
215 }
216
217 /**
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
222 */
223 @Override
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 }
232
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
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";
267 default:
268 throw new IllegalArgumentException("Invalid Color value: " + value);
269 }
270 }
271
272 }