2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
34 public final class Color
{
36 // ------------------------------------------------------------------------
37 // Constants --------------------------------------------------------------
38 // ------------------------------------------------------------------------
41 * SGR black value = 0.
43 private static final int SGRBLACK
= 0;
48 private static final int SGRRED
= 1;
51 * SGR green value = 2.
53 private static final int SGRGREEN
= 2;
56 * SGR yellow value = 3.
58 private static final int SGRYELLOW
= 3;
63 private static final int SGRBLUE
= 4;
66 * SGR magenta value = 5.
68 private static final int SGRMAGENTA
= 5;
73 private static final int SGRCYAN
= 6;
76 * SGR white value = 7.
78 private static final int SGRWHITE
= 7;
81 * Black. Bold + black = dark grey
83 public static final Color BLACK
= new Color(SGRBLACK
);
88 public static final Color RED
= new Color(SGRRED
);
93 public static final Color GREEN
= new Color(SGRGREEN
);
96 * Yellow. Sometimes not-bold yellow is brown.
98 public static final Color YELLOW
= new Color(SGRYELLOW
);
103 public static final Color BLUE
= new Color(SGRBLUE
);
108 public static final Color MAGENTA
= new Color(SGRMAGENTA
);
113 public static final Color CYAN
= new Color(SGRCYAN
);
118 public static final Color WHITE
= new Color(SGRWHITE
);
120 // ------------------------------------------------------------------------
121 // Variables --------------------------------------------------------------
122 // ------------------------------------------------------------------------
125 * The color value. Default is SGRWHITE.
127 private int value
= SGRWHITE
;
129 // ------------------------------------------------------------------------
130 // Constructors -----------------------------------------------------------
131 // ------------------------------------------------------------------------
134 * Private constructor used to make the static Color instances.
136 * @param value the integer Color value
138 private Color(final int value
) {
142 // ------------------------------------------------------------------------
143 // Color ------------------------------------------------------------------
144 // ------------------------------------------------------------------------
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").
152 public int getValue() {
157 * Public constructor returns one of the static Color instances.
159 * @param colorName "red", "blue", etc.
160 * @return Color.RED, Color.BLUE, etc.
162 static Color
getColor(final String colorName
) {
163 String str
= colorName
.toLowerCase();
165 if (str
.equals("black")) {
167 } else if (str
.equals("white")) {
169 } else if (str
.equals("red")) {
171 } else if (str
.equals("cyan")) {
173 } else if (str
.equals("green")) {
175 } else if (str
.equals("magenta")) {
176 return Color
.MAGENTA
;
177 } else if (str
.equals("blue")) {
179 } else if (str
.equals("yellow")) {
181 } else if (str
.equals("brown")) {
184 // Let unknown strings become white
190 * Invert a color in the same way as (CGA/VGA color XOR 0x7).
192 * @return the inverted color
194 public Color
invert() {
205 return Color
.MAGENTA
;
213 throw new IllegalArgumentException("Invalid Color value: " + value
);
218 * Comparison check. All fields must match to return true.
220 * @param rhs another Color instance
221 * @return true if all fields are equal
224 public boolean equals(final Object rhs
) {
225 if (!(rhs
instanceof Color
)) {
229 Color that
= (Color
) rhs
;
230 return (value
== that
.value
);
234 * Hashcode uses all fields in equals().
239 public int hashCode() {
244 * Make human-readable description of this Color.
246 * @return displayable String "red", "blue", etc.
249 public String
toString() {
268 throw new IllegalArgumentException("Invalid Color value: " + value
);