2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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
{
37 * The color value. Default is SGRWHITE.
39 private int value
= SGRWHITE
;
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").
47 public int getValue() {
52 * Private constructor used to make the static Color instances.
54 * @param value the integer Color value
56 private Color(final int value
) {
61 * Public constructor returns one of the static Color instances.
63 * @param colorName "red", "blue", etc.
64 * @return Color.RED, Color.BLUE, etc.
66 static Color
getColor(final String colorName
) {
67 String str
= colorName
.toLowerCase();
69 if (str
.equals("black")) {
71 } else if (str
.equals("white")) {
73 } else if (str
.equals("red")) {
75 } else if (str
.equals("cyan")) {
77 } else if (str
.equals("green")) {
79 } else if (str
.equals("magenta")) {
81 } else if (str
.equals("blue")) {
83 } else if (str
.equals("yellow")) {
85 } else if (str
.equals("brown")) {
88 // Let unknown strings become white
94 * SGR black value = 0.
96 private static final int SGRBLACK
= 0;
101 private static final int SGRRED
= 1;
104 * SGR green value = 2.
106 private static final int SGRGREEN
= 2;
109 * SGR yellow value = 3.
111 private static final int SGRYELLOW
= 3;
114 * SGR blue value = 4.
116 private static final int SGRBLUE
= 4;
119 * SGR magenta value = 5.
121 private static final int SGRMAGENTA
= 5;
124 * SGR cyan value = 6.
126 private static final int SGRCYAN
= 6;
129 * SGR white value = 7.
131 private static final int SGRWHITE
= 7;
134 * Black. Bold + black = dark grey
136 public static final Color BLACK
= new Color(SGRBLACK
);
141 public static final Color RED
= new Color(SGRRED
);
146 public static final Color GREEN
= new Color(SGRGREEN
);
149 * Yellow. Sometimes not-bold yellow is brown.
151 public static final Color YELLOW
= new Color(SGRYELLOW
);
156 public static final Color BLUE
= new Color(SGRBLUE
);
161 public static final Color MAGENTA
= new Color(SGRMAGENTA
);
166 public static final Color CYAN
= new Color(SGRCYAN
);
171 public static final Color WHITE
= new Color(SGRWHITE
);
174 * Invert a color in the same way as (CGA/VGA color XOR 0x7).
176 * @return the inverted color
178 public Color
invert() {
189 return Color
.MAGENTA
;
197 throw new IllegalArgumentException("Invalid Color value: " + value
);
202 * Comparison check. All fields must match to return true.
204 * @param rhs another Color instance
205 * @return true if all fields are equal
208 public boolean equals(final Object rhs
) {
209 if (!(rhs
instanceof Color
)) {
213 Color that
= (Color
) rhs
;
214 return (value
== that
.value
);
218 * Hashcode uses all fields in equals().
223 public int hashCode() {
228 * Make human-readable description of this Color.
230 * @return displayable String "red", "blue", etc.
233 public String
toString() {
252 throw new IllegalArgumentException("Invalid Color value: " + value
);