2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
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.
10 * Copyright (C) 2015 Kevin Lamonte
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.
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.
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
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
36 public final class Color
{
39 * The color value. Default is SGRWHITE.
41 private int value
= SGRWHITE
;
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").
49 public int getValue() {
54 * Private constructor used to make the static Color instances.
56 * @param value the integer Color value
58 private Color(final int value
) {
63 * Public constructor returns one of the static Color instances.
65 * @param colorName "red", "blue", etc.
66 * @return Color.RED, Color.BLUE, etc.
68 static Color
getColor(final String colorName
) {
69 String str
= colorName
.toLowerCase();
71 if (str
.equals("black")) {
73 } else if (str
.equals("white")) {
75 } else if (str
.equals("red")) {
77 } else if (str
.equals("cyan")) {
79 } else if (str
.equals("green")) {
81 } else if (str
.equals("magenta")) {
83 } else if (str
.equals("blue")) {
85 } else if (str
.equals("yellow")) {
87 } else if (str
.equals("brown")) {
90 // Let unknown strings become white
96 * SGR black value = 0.
98 private static final int SGRBLACK
= 0;
103 private static final int SGRRED
= 1;
106 * SGR green value = 2.
108 private static final int SGRGREEN
= 2;
111 * SGR yellow value = 3.
113 private static final int SGRYELLOW
= 3;
116 * SGR blue value = 4.
118 private static final int SGRBLUE
= 4;
121 * SGR magenta value = 5.
123 private static final int SGRMAGENTA
= 5;
126 * SGR cyan value = 6.
128 private static final int SGRCYAN
= 6;
131 * SGR white value = 7.
133 private static final int SGRWHITE
= 7;
136 * Black. Bold + black = dark grey
138 public static final Color BLACK
= new Color(SGRBLACK
);
143 public static final Color RED
= new Color(SGRRED
);
148 public static final Color GREEN
= new Color(SGRGREEN
);
151 * Yellow. Sometimes not-bold yellow is brown.
153 public static final Color YELLOW
= new Color(SGRYELLOW
);
158 public static final Color BLUE
= new Color(SGRBLUE
);
163 public static final Color MAGENTA
= new Color(SGRMAGENTA
);
168 public static final Color CYAN
= new Color(SGRCYAN
);
173 public static final Color WHITE
= new Color(SGRWHITE
);
176 * Invert a color in the same way as (CGA/VGA color XOR 0x7).
178 * @return the inverted color
180 public Color
invert() {
191 return Color
.MAGENTA
;
199 throw new IllegalArgumentException("Invalid Color value: " + value
);
204 * Comparison check. All fields must match to return true.
206 * @param rhs another Color instance
207 * @return true if all fields are equal
210 public boolean equals(final Object rhs
) {
211 if (!(rhs
instanceof Color
)) {
215 Color that
= (Color
) rhs
;
216 return (value
== that
.value
);
220 * Hashcode uses all fields in equals().
225 public int hashCode() {
230 * Make human-readable description of this Color.
232 * @return displayable String "red", "blue", etc.
235 public String
toString() {
254 throw new IllegalArgumentException("Invalid Color value: " + value
);