Re: [csswg-drafts] [css-color-5] color-contrast() needs resolution logic if 2 or more have the same contrast (#4732)

> makes me wonder where the implementers will get their scoring algorithm from 

The algorithm is given in WCAG 2.1 and the inputs are the relative luminances of the two colors being compared.

In [utilities.js](https://github.com/w3c/csswg-drafts/blob/master/css-color-4/utilities.js) I have added that function, for convenience, although the inputs are sRGB only (same as WCAG):

```
function contrast(RGB1, RGB2) {
    // return WCAG 2.1 contrast ratio
    // https://www.w3.org/TR/WCAG21/#dfn-contrast-ratio
    // for two sRGB values
    // given as arrays of 0.0 to 1.0

    var L1 = sRGB_to_luminance(RGB1);
    var L2 = sRGB_to_luminance(RGB2);

    if (L1 > L2) return (L1 + 0.05) / (L2 + 0.05);
    return (L2 + 0.05) / (L1 + 0.05);
}
```
I also have a `function sRGB_to_luminance(RGB)` in utilities.js, primarily to help writing examples.

Notice that relative luminance is just the `Y` in CIE XYZ and [conversions.js](https://github.com/w3c/csswg-drafts/blob/master/css-color-4/conversions.js) provides sample code to convert any of the predefined colorspaces to XYZ (for the rgb spaces, in two steps: undo gamma correction, then convert linear rgb to XYZ). The same goes for Lab to  XYZ (two steps, convert to D50 XYZ then adapt to D65 XYZ) and LCH to XYZ (three steps, first convert LCH to Lab). 

And even for calibrated spaces like CMYK etc, if a color profile is provided.

It is always just a case of computing two luminances, Y1 and Y2, then doing the simple ratio (with 5% flare compensation).

Luminance has been defined by an international standard since 1931, we don't need to define it in CSS just document how it is calculated, to help developers.

-- 
GitHub Notification of comment by svgeesus
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/4732#issuecomment-582582491 using your GitHub account

Received on Wednesday, 5 February 2020 19:53:32 UTC