css4j / css4j

CSS parser with Event and Object Model APIs, a DOM wrapper and a CSS-aware DOM implementation. Written in the Java™ language.

Home Page:https://css4j.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement the `color()` function from Color Level 4

carlosame opened this issue · comments

The color() function is specified by Color Level 4, and is closely related to the @color-profile rule that nobody has implemented yet.

Browser support

The function was implemented by Webkit in 2016 (for p3 color space). The following are the relevant trackers for Chrome and Firefox:

I could not find any bug for implementing @color-profile in Chrome nor in Webkit, and the Firefox bug is quiet, so at this point I'm not implementing a specific @color-profile rule (it can always be handled as a generic "ignorable rule" in NSAC, or a "unknown rule" in css4j's CSSOM).

Implementation details

The idea is that RGB colors implement the RGBAColor interface regardless of how they were specified. That is, the following color values are implementing RGBAColor:

  • #00f.
  • rgb(0 0 255).
  • color(srgb 0 0 1).
  • color(display-p3 0 0 1).

This is different to the approach that Houdini's TypedOM is following, where RGB values specified via the color() function implement the CSSColor interface but not the CSSRGB one which is only implemented by rgb() values. See https://drafts.css-houdini.org/css-typed-om-1/#dom-csscolorvalue-colorspace for the color value-interface mapping in TypedOM.

In the implementation that this project is adopting, one cannot know whether a sRGB color was specified through a color(srgb ...) or rgb(...) function unless the color is serialized, but that's not really something new. Even with Houdini's TypedOM one cannot tell whether a color implementing CSSRGB was specified with a #00f or a rgb(0 0 255).

One consequence of the approach that I'm following is that the RGBColorValue interface is now deprecated. In fact, one no longer needs to cast color values to any CSSColorValue sub-interface, just cast the CSSColor returned by CSSColorValue.getColor() to the relevant RGBAColor, XYZColor etc., according to the value returned by CSSColorValue.getColorModel(). Or even do not cast at all, and access the color components through CSSColor.item().