X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fbits%2FColor.java;h=707b244b12664aa04a2c691cb73cfa3973d06e27;hb=7b5261bc5b641e0769902f014e3b21f61050b02b;hp=dd3b93de18c72dd2005e1a5bd01e45f02aca7f77;hpb=9edb442b712de01d1b7af81d1d57a29c2c6e7871;p=nikiroo-utils.git diff --git a/src/jexer/bits/Color.java b/src/jexer/bits/Color.java index dd3b93d..707b244 100644 --- a/src/jexer/bits/Color.java +++ b/src/jexer/bits/Color.java @@ -1,16 +1,11 @@ /** * Jexer - Java Text User Interface * - * Version: $Id$ - * - * Author: Kevin Lamonte, kevin.lamonte@gmail.com - * * License: LGPLv3 or later * - * Copyright: This module is licensed under the GNU Lesser General - * Public License Version 3. Please see the file "COPYING" in this - * directory for more information about the GNU Lesser General Public - * License Version 3. + * This module is licensed under the GNU Lesser General Public License + * Version 3. Please see the file "COPYING" in this directory for more + * information about the GNU Lesser General Public License Version 3. * * Copyright (C) 2015 Kevin Lamonte * @@ -29,113 +24,223 @@ * http://www.gnu.org/licenses/, or write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA + * + * @author Kevin Lamonte [kevin.lamonte@gmail.com] + * @version 1 */ package jexer.bits; /** * A text cell color. */ -public class Color { +public final class Color { + + /** + * The color value. Default is SGRWHITE. + */ + private int value = SGRWHITE; + + /** + * Get color value. Note that these deliberately match the color values + * of the ECMA-48 / ANSI X3.64 / VT100-ish SGR function ("ANSI colors"). + * + * @return the value + */ + public int getValue() { + return value; + } /** - * The color value. Default is WHITE. + * Private constructor used to make the static Color instances. + * + * @param value the integer Color value */ - public int value = 7; + private Color(final int value) { + this.value = value; + } /** - * Public constructor + * Public constructor returns one of the static Color instances. + * + * @param colorName "red", "blue", etc. + * @return Color.RED, Color.BLUE, etc. */ - public Color(int value) { - this.value = value; + static Color getColor(final String colorName) { + switch (colorName.toLowerCase()) { + case "black": + return Color.BLACK; + case "white": + return Color.WHITE; + case "red": + return Color.RED; + case "cyan": + return Color.CYAN; + case "green": + return Color.GREEN; + case "magenta": + return Color.MAGENTA; + case "blue": + return Color.BLUE; + case "yellow": + return Color.YELLOW; + case "brown": + return Color.YELLOW; + default: + // Let unknown strings become white + return Color.WHITE; + } } - // The color integer values. NOT EXPOSED. - static private final int black = 0; - static private final int red = 1; - static private final int green = 2; - static private final int yellow = 3; - static private final int blue = 4; - static private final int magenta = 5; - static private final int cyan = 6; - static private final int white = 7; + /** + * SGR black value = 0. + */ + private static final int SGRBLACK = 0; + + /** + * SGR red value = 1. + */ + private static final int SGRRED = 1; + + /** + * SGR green value = 2. + */ + private static final int SGRGREEN = 2; + + /** + * SGR yellow value = 3. + */ + private static final int SGRYELLOW = 3; + + /** + * SGR blue value = 4. + */ + private static final int SGRBLUE = 4; + + /** + * SGR magenta value = 5. + */ + private static final int SGRMAGENTA = 5; + + /** + * SGR cyan value = 6. + */ + private static final int SGRCYAN = 6; + + /** + * SGR white value = 7. + */ + private static final int SGRWHITE = 7; /** * Black. Bold + black = dark grey */ - static public final Color BLACK = new Color(black); + public static final Color BLACK = new Color(SGRBLACK); /** - * Red + * Red. */ - static public final Color RED = new Color(red); + public static final Color RED = new Color(SGRRED); /** - * Green + * Green. */ - static public final Color GREEN = new Color(green); + public static final Color GREEN = new Color(SGRGREEN); /** * Yellow. Sometimes not-bold yellow is brown. */ - static public final Color YELLOW = new Color(yellow); + public static final Color YELLOW = new Color(SGRYELLOW); /** - * Blue + * Blue. */ - static public final Color BLUE = new Color(blue); + public static final Color BLUE = new Color(SGRBLUE); /** - * Magenta (purple) + * Magenta (purple). */ - static public final Color MAGENTA = new Color(magenta); + public static final Color MAGENTA = new Color(SGRMAGENTA); /** - * Cyan (blue-green) + * Cyan (blue-green). */ - static public final Color CYAN = new Color(cyan); + public static final Color CYAN = new Color(SGRCYAN); /** - * White + * White. */ - static public final Color WHITE = new Color(white); + public static final Color WHITE = new Color(SGRWHITE); /** * Invert a color in the same way as (CGA/VGA color XOR 0x7). + * * @return the inverted color */ public Color invert() { - switch (value) { - case black: - return Color.WHITE; - case white: - return Color.BLACK; - case red: - return Color.CYAN; - case cyan: - return Color.RED; - case green: - return Color.MAGENTA; - case magenta: - return Color.GREEN; - case blue: - return Color.YELLOW; - case yellow: - return Color.BLUE; - } - throw new IllegalArgumentException("Invalid Color value: " + - value); + switch (value) { + case SGRBLACK: + return Color.WHITE; + case SGRWHITE: + return Color.BLACK; + case SGRRED: + return Color.CYAN; + case SGRCYAN: + return Color.RED; + case SGRGREEN: + return Color.MAGENTA; + case SGRMAGENTA: + return Color.GREEN; + case SGRBLUE: + return Color.YELLOW; + case SGRYELLOW: + return Color.BLUE; + default: + throw new IllegalArgumentException("Invalid Color value: " + value); + } } /** - * Comparison. All fields must match to return true. + * Comparison check. All fields must match to return true. + * + * @param rhs another Color instance + * @return true if all fields are equal */ @Override - public boolean equals(Object rhs) { - if (!(rhs instanceof Color)) { - return false; - } + public boolean equals(final Object rhs) { + if (!(rhs instanceof Color)) { + return false; + } + + Color that = (Color) rhs; + return (value == that.value); + } - Color that = (Color)rhs; - return (value == that.value); + /** + * Make human-readable description of this Color. + * + * @return displayable String "red", "blue", etc. + */ + @Override + public String toString() { + switch (value) { + case SGRBLACK: + return "black"; + case SGRWHITE: + return "white"; + case SGRRED: + return "red"; + case SGRCYAN: + return "cyan"; + case SGRGREEN: + return "green"; + case SGRMAGENTA: + return "magenta"; + case SGRBLUE: + return "blue"; + case SGRYELLOW: + return "yellow"; + } + throw new IllegalArgumentException("Invalid Color value: " + value); } + }