JDK-8317690 : Add named colors from CSS Color Module Level 4
  • Type: CSR
  • Component: client-libs
  • Sub-Component: javax.swing
  • Priority: P3
  • Status: Draft
  • Resolution: Unresolved
  • Fix Versions: 22
  • Submitted: 2023-10-06
  • Updated: 2023-10-06
Related Reports
CSR :  
Description
Summary
-------

The Swing HTML StyleSheet.stringToColor()  method is updated to support CSS 4 color names and hexadecimal strings supporting alpha.

Problem
-------

The Swing HTML StyleSheet.stringToColor() method is specified as supporting only HTML 3.2 color strings, and hex strings without alpha support. CSS 4 has many additional color strings that can't be used.

Solution
--------

Update the implementation and specification to support CSS4 color names and hex strings. This does not advance anything else in the Swing HTML support beyond HTML 3.2

Specification
-------------

Update the method 
java.awt.Color javax.swing.html.StyleSheet.stringToColor(String string)
<pre>
     /**
-     * Converts a color string such as "RED" or "#NNNNNN" to a Color.
-     * Note: This will only convert the HTML3.2 color strings
-     *       or a string of length 7;
-     *       otherwise, it will return null.
+     * Converts a color string such as "RED",  "rgb(r g b)", "rgb(r g b a)",
+     * "rgba(r g b a)" or "#NNN", "#NNNN", "#NNNNNN",
+     * "#NNNNNNNN" to a Color.
+     * 
+     * Note: This will only convert strings which use any of the following:
+     * 
+     *   https://www.w3.org/TR/css-color-4/#named-colors
+     *   https://www.w3.org/TR/css-color-4/#hex-notation"
+     * starting with {@code #} followed by 3, 4, 6, or 8 hexadecimal digits,
+     *   https://www.w3.org/TR/css-color-4/#rgb-functions" `rgb()` and `rgba()`
+     * functions
+     *
+     * as specified by the CSS Color Module Level 4 https://www.w3.org/TR/css-color-4
+     * Otherwise, it will return null.
+     *
+     * This method is case-insensitive.
+     *
+     * The following code defines instances of the same color :
+     * {@snippet lang="java" :
+     *   import java.awt.Color;
+     *   import javax.swing.text.html.StyleSheet;
+     *   StyleSheet styleSheet = new StyleSheet();
+     *   // An opaque lightseagreen
+     *   Color color0 = styleSheet.stringToColor("Lightseagreen");
+     *   Color color1 = styleSheet.stringToColor("#20b2aa");
+     * }
      *
-     * @param string color string such as "RED" or "#NNNNNN"
+     * @param string color, string such as "RED" or "rgb(r g b)",  "rgba(r g b a)"
+     * or "#NNN", "#NNNN", "#NNNNNN", "#NNNNNNNN".
      * @return the color
      */
     public Color stringToColor(String string)
</pre>