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