scientificware / jdk

Read-only mirror of https://hg.openjdk.java.net/jdk/jdk
GNU General Public License v2.0
0 stars 0 forks source link

JDK-8293776 : Adds CSS 4 and 8 digits hex coded Color #13

Open scientificware opened 1 year ago

scientificware commented 1 year ago

The purpose of this issue is to add CSS 4 and 8 digits hex coded Color in CSS.java.

This is referenced in Java Bug Database as

This is tracked in JBS as

Related Pull Request

Adds the 4 and 8 digits color hex notations to CSS.java, as described in : CSS Color Module Level 4 W3C Candidate Recommendation Snapshot, 5 July 2022 4.1 The <Color> syntax 6.2 The RGB Hexadecimal Notations: #RRGGBB

Status

Impacts of these modifications.

Details :

Implementation examples

The code to test the present implementation.

public class Hex3468DigitsColorInCSS {

public static void main(String[] args) {
    StringBuilder result = new StringBuilder();
    boolean passed = true;
    int red, green, blue, alpha;
    Color color;
    StyleSheet styleSheet = new StyleSheet();

    // #rgba Should be interpreted as #rrggbbaa according CSS Color Level 4.
    // Then expecting r=255 g=17 b=34 a=170
    color = styleSheet.stringToColor("#f12a");
    alpha = color.getAlpha();
    result.append("  Result for #f00a -> expected r=255 g=17 b=34 a=170 -> %s Alpha : %s".formatted(color.toString(), alpha));
    if (alpha != 170) {
       passed = false;
    }
    // In #rrggbbaa last two digits should be interpreted as Alpha value according CSS Color Level 4.
    // Then expecting r=255 g=17 b=34 a=170
    color = styleSheet.stringToColor("#ff1122aa");
    alpha = color.getAlpha();
    result.append("  Result for #ff1122aa -> expected r=255 g=17 b=34 a=170 -> %s Alpha : %s\n".formatted(color.toString(), alpha));
    if (alpha != 170) {
       passed = false;
    }
    result.insert(0, passed ? "Succeed " : "Failed :");
    System.out.println(result);
}

}

Implementation evaluations : according the number of tests to reach a case
|Nb of digits|Original Code|Binary tree|Sequential + duplicate| Duplicate only|
|----|---|---|---|----|
|0|4|2|1|5|
|1|4|2|1|5|
|2|4|2|1|5|
|**3**|**1**|**3**|**3**|**2**|
|**4**|**2**|**3**|**3**|**3**|
|5|4|3|2|5|
|**6**|**3**|**3**|**3**|**3**|
|7|4|3|3|5|
|**8**|**4**|**3**|**4**|**5**|
|9|4|3|4|5|
|...|4|3|4|5|

### Test results :

[scientificwaredev@localhost openjdk-compile-tests]$ ./ce_jdk_unpatched_test_Hex3468DigitsColorInCSS Failed : Result for #f00a -> expected r=255 g=17 b=34 a=170 -> java.awt.Color[r=0,g=241,b=42] Alpha : 255 Result for #ff1122aa -> expected r=255 g=17 b=34 a=170 -> java.awt.Color[r=255,g=17,b=34] Alpha : 255

[scientificwaredev@localhost openjdk-compile-tests]$ ./ce_jdk_patched_test_Hex3468DigitsColorInCSS Succeed Result for #f00a -> expected r=255 g=17 b=34 a=170 -> java.awt.Color[r=255,g=17,b=34] Alpha : 170 Result for #ff1122aa -> expected r=255 g=17 b=34 a=170 -> java.awt.Color[r=255,g=17,b=34] Alpha : 170


### Regression test
https://github.com/scientificware/jdk/blob/24040cf664ecdeb2678796761331edfc60a6b897/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java#L36-L92

[scientificwaredev@localhost openjdk-compile-tests]$ ./ce_jdk_unpatched_test_Hex3468DigitsColor Exception in thread "main" java.lang.RuntimeException: Failed : Test for #f00a, expected r=255 but r=0 found, expected g=17 but g=241 found, expected b=34 but b=42 found, expected a=170 but a=255 found Test for #ff1122aa, expected r=255 but r=0 found, expected g=17 but g=241 found, expected b=34 but b=42 found, expected a=170 but a=255 found at Hex3468DigitsColor.main(Hex3468DigitsColor.java:90)

[scientificwaredev@localhost openjdk-compile-tests]$ ./ce_jdk_patched_test_Hex3468DigitsColor


### About performances
- Enhancements must be at least without performance cost.
- What is tested :
  - 1 000 occurences of :
    - a set of 1 600 valid color notations, equi-distributed (Not Real WWW).
    - and 1 wrong color notation.

|Run|Nb Tests|First Imp. subString (ms)|Sugg Imp. char %n$s (ms)|Sugg. Imp. char %n$s dis. mis comb. 3 & 4 (ms)|Bin. Tree (ms)|Bin. Tree (ms)|Bin. Tree (ms)|Bin. Tree (ms)|dis. mis comb. 3 & 4 (ms)|Custom Pars. 1 Map (ms)|Before PR (ms)|
|----|----|----|----|----|----|----|----|----|----|----|----|
|format `%s`||:heavy_check_mark:||||:heavy_check_mark:||||||
|format `%n$s`|||:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:|||||||
|`""+r+...`|||||||:heavy_check_mark:||:heavy_check_mark:|||
|bit rotation||||||||:heavy_check_mark:||||
|hex Pattern||:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:||||
|||||||||:3rd_place_medal:|:2nd_place_medal:|:1st_place_medal:|||
|1|1&nbsp;601&nbsp;000|351|1&nbsp;790|1&nbsp;825|1&nbsp;860|374|178|180|110|36|229|
|2|1&nbsp;601&nbsp;000|346|1 253|1 297|1 913|385|177|180|110|35|233|
|3|1&nbsp;601&nbsp;000|365|1 783|1 763|1 887|369|179|185|108|36|228|
|4|1&nbsp;601&nbsp;000|346|1 824|1 698|1 820|375|178|184|109|36|230|
|5|1&nbsp;601&nbsp;000|364|1 767|1 823|1 958|375|180|178|111|37|229|

### CSS Level 4 details
 - defines color hex code as `#[2 digits Red][2 digits Green][2 digits Blue][2 digits Alpha]`. With digit 0 ... f.
 - allows, webpage passes 3, 4, 6 or 8 digit color code.
   - `3 digits #[R][G][B]` ........ represents `#[RR][GG][BB]FF`
   - 4 digits `#[R][G][B][A]` ..... represents `#[RR][GG][BB][AA]`
   - 6 digits `#[RR][GG][BB]` ..... represents `#[RR][GG][BB]FF`
   - 8 digits `#[RR][GG][BB][AA]` . represents `#[RR][GG][BB][AA]`

 Becareful ! In java.awt.Color hex `#[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue]`
 Since this method is defined in CSS.java, it must only take in charge CSS Level 4 color format.

 As explained in the code, the current OpenJDK implementation follows [CSS1 Recommandation](https://www.w3.org/TR/CSS1/#color-units)
 - 3 digits `#[R][G][B]`    represents `#[RR][GG][BB]FF`
 - 6 digits `#[R][G][B]`    represents `#[RR][GG][BB]FF`

[JDK-4895924 : Strings in format #rgb not handled by Color.decode() (affects CSS / Swing)](https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4895924)
[JDK-4895924 : Strings in format #rgb not handled by Color.decode() (affects CSS / Swing)](https://bugs.openjdk.org/browse/JDK-4895924)

----
|Id.|Title|Java Bug Data Base|OpenJDK Bug System|Status|
|----|----|----|----|----|
|JDK-4895924|Strings in format #rgb not handled by Color.decode() (affects CSS / Swing)|[Report](https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4895924)|[Details](https://bugs.openjdk.org/browse/JDK-4895924)|:closed_lock_with_key:|
|JDK-8149631|rgb(...) CSS color values are not parsed properly|[Report](https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8149631)|[Details](https://bugs.openjdk.org/browse/JDK-8149631)|:closed_lock_with_key:|