| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * License: LGPLv3 or later |
| 5 | * |
| 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. |
| 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 |
| 27 | * |
| 28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 29 | * @version 1 |
| 30 | */ |
| 31 | package jexer.bits; |
| 32 | |
| 33 | /** |
| 34 | * A text cell color. |
| 35 | */ |
| 36 | public 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 | } |
| 52 | |
| 53 | /** |
| 54 | * Private constructor used to make the static Color instances. |
| 55 | * |
| 56 | * @param value the integer Color value |
| 57 | */ |
| 58 | private Color(final int value) { |
| 59 | this.value = value; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Public constructor returns one of the static Color instances. |
| 64 | * |
| 65 | * @param colorName "red", "blue", etc. |
| 66 | * @return Color.RED, Color.BLUE, etc. |
| 67 | */ |
| 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 | } |
| 92 | } |
| 93 | |
| 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; |
| 133 | |
| 134 | /** |
| 135 | * Black. Bold + black = dark grey |
| 136 | */ |
| 137 | public static final Color BLACK = new Color(SGRBLACK); |
| 138 | |
| 139 | /** |
| 140 | * Red. |
| 141 | */ |
| 142 | public static final Color RED = new Color(SGRRED); |
| 143 | |
| 144 | /** |
| 145 | * Green. |
| 146 | */ |
| 147 | public static final Color GREEN = new Color(SGRGREEN); |
| 148 | |
| 149 | /** |
| 150 | * Yellow. Sometimes not-bold yellow is brown. |
| 151 | */ |
| 152 | public static final Color YELLOW = new Color(SGRYELLOW); |
| 153 | |
| 154 | /** |
| 155 | * Blue. |
| 156 | */ |
| 157 | public static final Color BLUE = new Color(SGRBLUE); |
| 158 | |
| 159 | /** |
| 160 | * Magenta (purple). |
| 161 | */ |
| 162 | public static final Color MAGENTA = new Color(SGRMAGENTA); |
| 163 | |
| 164 | /** |
| 165 | * Cyan (blue-green). |
| 166 | */ |
| 167 | public static final Color CYAN = new Color(SGRCYAN); |
| 168 | |
| 169 | /** |
| 170 | * White. |
| 171 | */ |
| 172 | public static final Color WHITE = new Color(SGRWHITE); |
| 173 | |
| 174 | /** |
| 175 | * Invert a color in the same way as (CGA/VGA color XOR 0x7). |
| 176 | * |
| 177 | * @return the inverted color |
| 178 | */ |
| 179 | public Color invert() { |
| 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 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 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 |
| 207 | */ |
| 208 | @Override |
| 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 | } |
| 217 | |
| 218 | /** |
| 219 | * Hashcode uses all fields in equals(). |
| 220 | * |
| 221 | * @return the hash |
| 222 | */ |
| 223 | @Override |
| 224 | public int hashCode() { |
| 225 | return value; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Make human-readable description of this Color. |
| 230 | * |
| 231 | * @return displayable String "red", "blue", etc. |
| 232 | */ |
| 233 | @Override |
| 234 | public String toString() { |
| 235 | switch (value) { |
| 236 | case SGRBLACK: |
| 237 | return "black"; |
| 238 | case SGRWHITE: |
| 239 | return "white"; |
| 240 | case SGRRED: |
| 241 | return "red"; |
| 242 | case SGRCYAN: |
| 243 | return "cyan"; |
| 244 | case SGRGREEN: |
| 245 | return "green"; |
| 246 | case SGRMAGENTA: |
| 247 | return "magenta"; |
| 248 | case SGRBLUE: |
| 249 | return "blue"; |
| 250 | case SGRYELLOW: |
| 251 | return "yellow"; |
| 252 | default: |
| 253 | throw new IllegalArgumentException("Invalid Color value: " + value); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | } |