You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2020/03/17 19:20:42 UTC

svn commit: r1875319 - in /pivot/trunk/wtk/src/org/apache/pivot/wtk: CSSColor.java util/ColorUtilities.java

Author: rwhitcomb
Date: Tue Mar 17 19:20:42 2020
New Revision: 1875319

URL: http://svn.apache.org/viewvc?rev=1875319&view=rev
Log:
Change / simplify the way we initialize the CSSColor values by using
a private static class for the lookup maps, and using an alternate
constructor that creates the Color value itself.
Add a method to ColorUtilities to translate a CSSColor to a string value.

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/CSSColor.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/util/ColorUtilities.java

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/CSSColor.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/CSSColor.java?rev=1875319&r1=1875318&r2=1875319&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/CSSColor.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/CSSColor.java Tue Mar 17 19:20:42 2020
@@ -34,162 +34,189 @@ import org.apache.pivot.wtk.util.ColorUt
  * and {@link org.apache.pivot.wtk.content.ColorItem#allCSSColors} methods.
  */
 public enum CSSColor {
-    AliceBlue           (new Color(240, 248, 255)),
-    AntiqueWhite        (new Color(250, 235, 215)),
-    Aqua                (new Color(  0, 255, 255)),
-    Aquamarine          (new Color(127, 255, 212)),
-    Azure               (new Color(240, 255, 255)),
-    Beige               (new Color(245, 245, 220)),
-    Bisque              (new Color(255, 228, 196)),
+    AliceBlue           (240, 248, 255),
+    AntiqueWhite        (250, 235, 215),
+    Aqua                (  0, 255, 255),
+    Aquamarine          (127, 255, 212),
+    Azure               (240, 255, 255),
+    Beige               (245, 245, 220),
+    Bisque              (255, 228, 196),
     Black               (Color.BLACK),
-    BlanchedAlmond      (new Color(255, 235, 205)),
+    BlanchedAlmond      (255, 235, 205),
     Blue                (Color.BLUE),
-    BlueViolet          (new Color(138,  43, 226)),
-    Brown               (new Color(165,  42,  42)),
-    Burlywood           (new Color(222, 184, 135)),
-    CadetBlue           (new Color( 95, 158, 160)),
-    Chartreuse          (new Color(127, 255,   0)),
-    Chocolate           (new Color(210, 105,  30)),
-    Coral               (new Color(255, 127,  80)),
-    CornflowerBlue      (new Color(100, 149, 237)),
-    Cornsilk            (new Color(255, 248, 220)),
-    Crimson             (new Color(220,  20,  60)),
+    BlueViolet          (138,  43, 226),
+    Brown               (165,  42,  42),
+    Burlywood           (222, 184, 135),
+    CadetBlue           ( 95, 158, 160),
+    Chartreuse          (127, 255,   0),
+    Chocolate           (210, 105,  30),
+    Coral               (255, 127,  80),
+    CornflowerBlue      (100, 149, 237),
+    Cornsilk            (255, 248, 220),
+    Crimson             (220,  20,  60),
     Cyan                (Color.CYAN),
-    DarkBlue            (new Color(  0,   0, 139)),
-    DarkCyan            (new Color(  0, 139, 139)),
-    DarkGoldenrod       (new Color(184, 134,  11)),
+    DarkBlue            (  0,   0, 139),
+    DarkCyan            (  0, 139, 139),
+    DarkGoldenrod       (184, 134,  11),
     DarkGray            (Color.DARK_GRAY),
-    DarkGreen           (new Color(  0, 100,   0)),
+    DarkGreen           (  0, 100,   0),
     DarkGrey            (Color.DARK_GRAY),
-    DarkKhaki           (new Color(189, 183, 107)),
-    DarkMagenta         (new Color(139,   0, 139)),
-    DarkOliveGreen      (new Color( 85, 107,  47)),
-    DarkOrange          (new Color(255, 140,   0)),
-    DarkOrchid          (new Color(153,  50, 204)),
-    DarkRed             (new Color(139,   0,   0)),
-    DarkSalmon          (new Color(233, 150, 122)),
-    DarkSeaGreen        (new Color(143, 188, 143)),
-    DarkSlateBlue       (new Color( 72,  61, 139)),
-    DarkSlateGray       (new Color( 47,  79,  79)),
-    DarkSlateGrey       (new Color( 47,  79,  79)),
-    DarkTurquoise       (new Color(  0, 206, 209)),
-    DarkViolet          (new Color(148,   0, 211)),
-    DeepPink            (new Color(255,  20, 147)),
-    DeepSkyBlue         (new Color(  0, 191, 255)),
-    DimGray             (new Color(105, 105, 105)),
-    DimGrey             (new Color(105, 105, 105)),
-    DodgerBlue          (new Color( 30, 144, 255)),
-    FireBrick           (new Color(178,  34,  34)),
-    FloralWhite         (new Color(255, 250, 240)),
-    ForestGreen         (new Color( 34, 139,  34)),
-    Fuchsia             (new Color(255,   0, 255)),
-    Gainsboro           (new Color(220, 220, 220)),
-    GhostWhite          (new Color(248, 248, 255)),
-    Gold                (new Color(255, 215,   0)),
-    Goldenrod           (new Color(218, 165,  32)),
+    DarkKhaki           (189, 183, 107),
+    DarkMagenta         (139,   0, 139),
+    DarkOliveGreen      ( 85, 107,  47),
+    DarkOrange          (255, 140,   0),
+    DarkOrchid          (153,  50, 204),
+    DarkRed             (139,   0,   0),
+    DarkSalmon          (233, 150, 122),
+    DarkSeaGreen        (143, 188, 143),
+    DarkSlateBlue       ( 72,  61, 139),
+    DarkSlateGray       ( 47,  79,  79),
+    DarkSlateGrey       ( 47,  79,  79),
+    DarkTurquoise       (  0, 206, 209),
+    DarkViolet          (148,   0, 211),
+    DeepPink            (255,  20, 147),
+    DeepSkyBlue         (  0, 191, 255),
+    DimGray             (105, 105, 105),
+    DimGrey             (105, 105, 105),
+    DodgerBlue          ( 30, 144, 255),
+    FireBrick           (178,  34,  34),
+    FloralWhite         (255, 250, 240),
+    ForestGreen         ( 34, 139,  34),
+    Fuchsia             (255,   0, 255),
+    Gainsboro           (220, 220, 220),
+    GhostWhite          (248, 248, 255),
+    Gold                (255, 215,   0),
+    Goldenrod           (218, 165,  32),
     Gray                (Color.GRAY),
     Green               (Color.GREEN),
-    GreenYellow         (new Color(173, 255,  47)),
+    GreenYellow         (173, 255,  47),
     Grey                (Color.GRAY),
-    Honeydew            (new Color(240, 255, 240)),
-    HotPink             (new Color(255, 105, 180)),
-    IndianRed           (new Color(205,  92,  92)),
-    Indigo              (new Color( 75,   0, 130)),
-    Ivory               (new Color(255, 255, 240)),
-    Khaki               (new Color(240, 230, 140)),
-    Lavender            (new Color(230, 230, 250)),
-    LavenderBlush       (new Color(255, 240, 245)),
-    LawnGreen           (new Color(124, 252,   0)),
-    LemonChiffon        (new Color(255, 250, 205)),
-    LightBlue           (new Color(173, 216, 230)),
-    LightCoral          (new Color(240, 128, 128)),
-    LightCyan           (new Color(224, 255, 255)),
-    LightGoldenrodYellow(new Color(250, 250, 210)),
+    Honeydew            (240, 255, 240),
+    HotPink             (255, 105, 180),
+    IndianRed           (205,  92,  92),
+    Indigo              ( 75,   0, 130),
+    Ivory               (255, 255, 240),
+    Khaki               (240, 230, 140),
+    Lavender            (230, 230, 250),
+    LavenderBlush       (255, 240, 245),
+    LawnGreen           (124, 252,   0),
+    LemonChiffon        (255, 250, 205),
+    LightBlue           (173, 216, 230),
+    LightCoral          (240, 128, 128),
+    LightCyan           (224, 255, 255),
+    LightGoldenrodYellow(250, 250, 210),
     LightGray           (Color.LIGHT_GRAY),
-    LightGreen          (new Color(144, 238, 144)),
+    LightGreen          (144, 238, 144),
     LightGrey           (Color.LIGHT_GRAY),
-    LightPink           (new Color(255, 182, 193)),
-    LightSalmon         (new Color(255, 160, 122)),
-    LightSeaGreen       (new Color( 32, 178, 170)),
-    LightSkyBlue        (new Color(135, 206, 250)),
-    LightSlateGray      (new Color(119, 136, 153)),
-    LightSlateGrey      (new Color(119, 136, 153)),
-    LightSteelBlue      (new Color(176, 196, 222)),
-    LightYellow         (new Color(255, 255, 224)),
-    Lime                (new Color(  0, 255,   0)),
-    LimeGreen           (new Color( 50, 205,  50)),
-    Linen               (new Color(250, 240, 230)),
+    LightPink           (255, 182, 193),
+    LightSalmon         (255, 160, 122),
+    LightSeaGreen       ( 32, 178, 170),
+    LightSkyBlue        (135, 206, 250),
+    LightSlateGray      (119, 136, 153),
+    LightSlateGrey      (119, 136, 153),
+    LightSteelBlue      (176, 196, 222),
+    LightYellow         (255, 255, 224),
+    Lime                (  0, 255,   0),
+    LimeGreen           ( 50, 205,  50),
+    Linen               (250, 240, 230),
     Magenta             (Color.MAGENTA),
-    Maroon              (new Color(128,   0,   0)),
-    MediumAquamarine    (new Color(102, 205, 170)),
-    MediumBlue          (new Color(  0,   0, 205)),
-    MediumOrchid        (new Color(186,  85, 211)),
-    MediumPurple        (new Color(147, 112, 219)),
-    MediumSeaGreen      (new Color( 60, 179, 113)),
-    MediumSlateBlue     (new Color(123, 104, 238)),
-    MediumSpringGreen   (new Color(  0, 250, 154)),
-    MediumTurquoise     (new Color( 72, 209, 204)),
-    MediumVioletRed     (new Color(199,  21, 133)),
-    MidnightBlue        (new Color( 25,  25, 112)),
-    MintCream           (new Color(245, 255, 250)),
-    MistyRose           (new Color(255, 228, 225)),
-    Moccasin            (new Color(255, 228, 181)),
-    NavajoWhite         (new Color(255, 222, 173)),
-    Navy                (new Color(  0,   0, 128)),
-    OldLace             (new Color(253, 245, 230)),
-    Olive               (new Color(128, 128,   0)),
-    OliveDrab           (new Color(107, 142,  35)),
+    Maroon              (128,   0,   0),
+    MediumAquamarine    (102, 205, 170),
+    MediumBlue          (  0,   0, 205),
+    MediumOrchid        (186,  85, 211),
+    MediumPurple        (147, 112, 219),
+    MediumSeaGreen      ( 60, 179, 113),
+    MediumSlateBlue     (123, 104, 238),
+    MediumSpringGreen   (  0, 250, 154),
+    MediumTurquoise     ( 72, 209, 204),
+    MediumVioletRed     (199,  21, 133),
+    MidnightBlue        ( 25,  25, 112),
+    MintCream           (245, 255, 250),
+    MistyRose           (255, 228, 225),
+    Moccasin            (255, 228, 181),
+    NavajoWhite         (255, 222, 173),
+    Navy                (  0,   0, 128),
+    OldLace             (253, 245, 230),
+    Olive               (128, 128,   0),
+    OliveDrab           (107, 142,  35),
     Orange              (Color.ORANGE),
-    OrangeRed           (new Color(255,  69,   0)),
-    Orchid              (new Color(218, 112, 214)),
-    PaleGoldenrod       (new Color(238, 232, 170)),
-    PaleGreen           (new Color(152, 251, 152)),
-    PaleTurquoise       (new Color(175, 238, 238)),
-    PaleVioletRed       (new Color(219, 112, 147)),
-    PapayaWhip          (new Color(255, 239, 213)),
-    PeachPuff           (new Color(255, 218, 185)),
-    Peru                (new Color(205, 133,  63)),
+    OrangeRed           (255,  69,   0),
+    Orchid              (218, 112, 214),
+    PaleGoldenrod       (238, 232, 170),
+    PaleGreen           (152, 251, 152),
+    PaleTurquoise       (175, 238, 238),
+    PaleVioletRed       (219, 112, 147),
+    PapayaWhip          (255, 239, 213),
+    PeachPuff           (255, 218, 185),
+    Peru                (205, 133,  63),
     Pink                (Color.PINK),
-    Plum                (new Color(221, 160, 221)),
-    PowderBlue          (new Color(176, 224, 230)),
-    Purple              (new Color(128,   0, 128)),
+    Plum                (221, 160, 221),
+    PowderBlue          (176, 224, 230),
+    Purple              (128,   0, 128),
     Red                 (Color.RED),
-    RosyBrown           (new Color(188, 143, 143)),
-    RoyalBlue           (new Color( 65, 105, 225)),
-    SaddleBrown         (new Color(139,  69,  19)),
-    Salmon              (new Color(250, 128, 114)),
-    SandyBrown          (new Color(244, 164,  96)),
-    SeaGreen            (new Color( 46, 139,  87)),
-    Seashell            (new Color(255, 245, 238)),
-    Sienna              (new Color(160,  82,  45)),
-    Silver              (new Color(192, 192, 192)),
-    SkyBlue             (new Color(135, 206, 235)),
-    SlateBlue           (new Color(106,  90, 205)),
-    SlateGray           (new Color(112, 128, 144)),
-    SlateGrey           (new Color(112, 128, 144)),
-    Snow                (new Color(255, 250, 250)),
-    SpringGreen         (new Color(  0, 255, 127)),
-    SteelBlue           (new Color( 70, 130, 180)),
-    Tan                 (new Color(210, 180, 140)),
-    Teal                (new Color(  0, 128, 128)),
-    Thistle             (new Color(216, 191, 216)),
-    Tomato              (new Color(255,  99,  71)),
-    Turquoise           (new Color( 64, 224, 208)),
-    Violet              (new Color(238, 130, 238)),
-    Wheat               (new Color(245, 222, 179)),
+    RosyBrown           (188, 143, 143),
+    RoyalBlue           ( 65, 105, 225),
+    SaddleBrown         (139,  69,  19),
+    Salmon              (250, 128, 114),
+    SandyBrown          (244, 164,  96),
+    SeaGreen            ( 46, 139,  87),
+    Seashell            (255, 245, 238),
+    Sienna              (160,  82,  45),
+    Silver              (192, 192, 192),
+    SkyBlue             (135, 206, 235),
+    SlateBlue           (106,  90, 205),
+    SlateGray           (112, 128, 144),
+    SlateGrey           (112, 128, 144),
+    Snow                (255, 250, 250),
+    SpringGreen         (  0, 255, 127),
+    SteelBlue           ( 70, 130, 180),
+    Tan                 (210, 180, 140),
+    Teal                (  0, 128, 128),
+    Thistle             (216, 191, 216),
+    Tomato              (255,  99,  71),
+    Turquoise           ( 64, 224, 208),
+    Violet              (238, 130, 238),
+    Wheat               (245, 222, 179),
     White               (Color.WHITE),
-    WhiteSmoke          (new Color(245, 245, 245)),
+    WhiteSmoke          (245, 245, 245),
     Yellow              (Color.YELLOW),
-    YellowGreen         (new Color(154, 205,  50));
+    YellowGreen         (154, 205,  50);
 
     /** The color value associated with this CSS color name. */
     private Color color;
     /** A standardized (lower-case) name for this color for matching. */
     private String colorName;
 
+    /**
+     * Private class that allows us to initialize lookup maps at constructor time,
+     * instead of in a static initializer block later.
+     */
+    private static class Lookup {
+        /** A map to translate from a color name to the enum value. */
+        private static Map<String, CSSColor> colorNameMap = new HashMap<>();
+        /** A map to translate from a color value to the enum value. */
+        private static Map<Color, CSSColor> colorValueMap = new HashMap<>();
+    }
+
+    /**
+     * Construct from integer R,G,B values.
+     * @param r The red component of the color.
+     * @param g The green component.
+     * @param b And finally the blue value.
+     */
+    CSSColor(final int r, final int g, final int b) {
+        this(new Color(r, g, b));
+    }
+
+    /**
+     * Construct from one of the {@link Color} equivalents.
+     * @param color The Java AWT color value this corresponds to.
+     */
     CSSColor(final Color color) {
         this.color = color;
         this.colorName = super.toString().toLowerCase(Locale.ENGLISH);
+        Lookup.colorNameMap.put(this.colorName, this);
+        Lookup.colorValueMap.put(this.color, this);
     }
 
     /**
@@ -216,7 +243,7 @@ public enum CSSColor {
      */
     public static CSSColor fromString(final String colorName) {
         String lowerName = colorName.toLowerCase(Locale.ENGLISH);
-        CSSColor color = colorNameMap.get(lowerName);
+        CSSColor color = Lookup.colorNameMap.get(lowerName);
         if (color == null) {
             throw new IllegalArgumentException("Incorrect Color format.  "
                 + "Color name \"" + colorName + "\" is not valid.");
@@ -233,7 +260,7 @@ public enum CSSColor {
      */
     public static CSSColor fromColor(final Color color) {
         Color solidColor = ColorUtilities.toSolidColor(color);
-        CSSColor cssColor = colorValueMap.get(solidColor);
+        CSSColor cssColor = Lookup.colorValueMap.get(solidColor);
         if (cssColor == null) {
             throw new IllegalArgumentException("Incorrect Color value.  "
                 + color.toString() + " does not match any CSS color.");
@@ -241,18 +268,4 @@ public enum CSSColor {
         return cssColor;
     }
 
-
-    /** A map to translate from a color name to the enum value. */
-    private static Map<String, CSSColor> colorNameMap;
-    /** A map to translate from a color value to the enum value. */
-    private static Map<Color, CSSColor> colorValueMap;
-
-    static {
-        colorNameMap = new HashMap<>();
-        colorValueMap = new HashMap<>();
-        for (CSSColor color : values()) {
-            colorNameMap.put(color.getColorName(), color);
-            colorValueMap.put(color.getColor(), color);
-        }
-    }
 }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/util/ColorUtilities.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/util/ColorUtilities.java?rev=1875319&r1=1875318&r2=1875319&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/util/ColorUtilities.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/util/ColorUtilities.java Tue Mar 17 19:20:42 2020
@@ -240,4 +240,16 @@ public final class ColorUtilities {
         }
     }
 
+    /**
+     * @return An encoded value for the given {@link CSSColor} in the form of:
+     * <tt>#RRGGBB</tt> only (since these are always solid colors by definition),
+     * which is suitable for lookup in the
+     * {@link org.apache.pivot.wtk.GraphicsUtilities#decodeColor(String)} method.
+     * @param color The input color to convert.
+     * @see #toStringValue(Color)
+     */
+    public static String toStringValue(final CSSColor color) {
+        return toStringValue(color.getColor());
+    }
+
 }