misc cleanup
[fanfix.git] / src / jexer / bits / Color.java
CommitLineData
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 */
31package jexer.bits;
32
33/**
34 * A text cell color.
35 */
7b5261bc
KL
36public 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
KL
68 static Color getColor(final String colorName) {
69 switch (colorName.toLowerCase()) {
70 case "black":
71 return Color.BLACK;
72 case "white":
73 return Color.WHITE;
74 case "red":
75 return Color.RED;
76 case "cyan":
77 return Color.CYAN;
78 case "green":
79 return Color.GREEN;
80 case "magenta":
81 return Color.MAGENTA;
82 case "blue":
83 return Color.BLUE;
84 case "yellow":
85 return Color.YELLOW;
86 case "brown":
87 return Color.YELLOW;
88 default:
89 // Let unknown strings become white
90 return Color.WHITE;
91 }
624ce48e
KL
92 }
93
7b5261bc
KL
94 /**
95 * SGR black value = 0.
96 */
97 private static final int SGRBLACK = 0;
98
99 /**
100 * SGR red value = 1.
101 */
102 private static final int SGRRED = 1;
103
104 /**
105 * SGR green value = 2.
106 */
107 private static final int SGRGREEN = 2;
108
109 /**
110 * SGR yellow value = 3.
111 */
112 private static final int SGRYELLOW = 3;
113
114 /**
115 * SGR blue value = 4.
116 */
117 private static final int SGRBLUE = 4;
118
119 /**
120 * SGR magenta value = 5.
121 */
122 private static final int SGRMAGENTA = 5;
123
124 /**
125 * SGR cyan value = 6.
126 */
127 private static final int SGRCYAN = 6;
128
129 /**
130 * SGR white value = 7.
131 */
132 private static final int SGRWHITE = 7;
624ce48e
KL
133
134 /**
135 * Black. Bold + black = dark grey
136 */
7b5261bc 137 public static final Color BLACK = new Color(SGRBLACK);
624ce48e
KL
138
139 /**
7b5261bc 140 * Red.
624ce48e 141 */
7b5261bc 142 public static final Color RED = new Color(SGRRED);
624ce48e
KL
143
144 /**
7b5261bc 145 * Green.
624ce48e 146 */
7b5261bc 147 public static final Color GREEN = new Color(SGRGREEN);
624ce48e
KL
148
149 /**
150 * Yellow. Sometimes not-bold yellow is brown.
151 */
7b5261bc 152 public static final Color YELLOW = new Color(SGRYELLOW);
624ce48e
KL
153
154 /**
7b5261bc 155 * Blue.
624ce48e 156 */
7b5261bc 157 public static final Color BLUE = new Color(SGRBLUE);
624ce48e
KL
158
159 /**
7b5261bc 160 * Magenta (purple).
624ce48e 161 */
7b5261bc 162 public static final Color MAGENTA = new Color(SGRMAGENTA);
624ce48e
KL
163
164 /**
7b5261bc 165 * Cyan (blue-green).
624ce48e 166 */
7b5261bc 167 public static final Color CYAN = new Color(SGRCYAN);
624ce48e
KL
168
169 /**
7b5261bc 170 * White.
624ce48e 171 */
7b5261bc 172 public static final Color WHITE = new Color(SGRWHITE);
624ce48e
KL
173
174 /**
175 * Invert a color in the same way as (CGA/VGA color XOR 0x7).
7b5261bc 176 *
624ce48e
KL
177 * @return the inverted color
178 */
4328bb42 179 public Color invert() {
7b5261bc
KL
180 switch (value) {
181 case SGRBLACK:
182 return Color.WHITE;
183 case SGRWHITE:
184 return Color.BLACK;
185 case SGRRED:
186 return Color.CYAN;
187 case SGRCYAN:
188 return Color.RED;
189 case SGRGREEN:
190 return Color.MAGENTA;
191 case SGRMAGENTA:
192 return Color.GREEN;
193 case SGRBLUE:
194 return Color.YELLOW;
195 case SGRYELLOW:
196 return Color.BLUE;
197 default:
198 throw new IllegalArgumentException("Invalid Color value: " + value);
199 }
4328bb42
KL
200 }
201
202 /**
7b5261bc
KL
203 * Comparison check. All fields must match to return true.
204 *
205 * @param rhs another Color instance
206 * @return true if all fields are equal
4328bb42
KL
207 */
208 @Override
7b5261bc
KL
209 public boolean equals(final Object rhs) {
210 if (!(rhs instanceof Color)) {
211 return false;
212 }
213
214 Color that = (Color) rhs;
215 return (value == that.value);
216 }
4328bb42 217
7b5261bc
KL
218 /**
219 * Make human-readable description of this Color.
220 *
221 * @return displayable String "red", "blue", etc.
222 */
223 @Override
224 public String toString() {
225 switch (value) {
226 case SGRBLACK:
227 return "black";
228 case SGRWHITE:
229 return "white";
230 case SGRRED:
231 return "red";
232 case SGRCYAN:
233 return "cyan";
234 case SGRGREEN:
235 return "green";
236 case SGRMAGENTA:
237 return "magenta";
238 case SGRBLUE:
239 return "blue";
240 case SGRYELLOW:
241 return "yellow";
242 }
243 throw new IllegalArgumentException("Invalid Color value: " + value);
624ce48e 244 }
7b5261bc 245
624ce48e 246}