Re: [csswg-drafts] [css-colors-4][css-colors-5] rgb-to-hwb algorithm disagrees indirectly with relative-color-out-of-gamut and color-mix-out-of-gamut expectations (#10695)

I also think you analysis is correct :)

The flipping of the hue was introduced only for `hsl` if I recall correctly.
It was discussed here: https://github.com/w3c/csswg-drafts/issues/9222
As far as I can see `hwb` was not mentioned there.

So I think it was an oversight that the algorithm was changed without also considering the effects it would have on conversions to `hwb`.

I've tested your suggested fix and it seems fine to me.

-----

@squelart This variant is based on the WebKit code, after [your proposed changes would be applied](https://github.com/WebKit/WebKit/pull/31636/files)

```js
/**
 * @param {number} red - Red component 0..1
 * @param {number} green - Green component 0..1
 * @param {number} blue - Blue component 0..1
 * @return {number[]} Array of HWB values: Hue as degrees 0..360, Whiteness and Blackness in reference range [0,100]
 */
function rgbToHwb(red, green, blue) {
    var white = Math.min(red, green, blue);
    var black = 1 - Math.max(red, green, blue);
    return([rgbToHue(red, green, blue), white*100, black*100]);
}

/**
 * @param {number} red - Red component 0..1
 * @param {number} green - Green component 0..1
 * @param {number} blue - Blue component 0..1
 * @return {number} Hue as degrees 0..360
 */
function rgbToHue(red, green, blue) {
 const max = Math.max(red, green, blue);
 const min = Math.min(red, green, blue);
 let hue = NaN;
 const d = max - min;

 if (d !== 0) {
  switch (max) {
   case red: hue = ((green - blue) / d); break;
   case green: hue = (blue - red) / d + 2; break;
   case blue: hue = (red - green) / d + 4;
  }

  hue = hue * 60;
 }

 if (hue >= 360) {
  hue -= 360;
 } else if (hue < 0) {
  hue += 360;
 }

 return hue;
}
```

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


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Tuesday, 6 August 2024 14:09:10 UTC