# Re: [csswg-drafts] [css-color-4] Some example HWB-to-RGB translations are incorrect (#5531)

• From: Chris Lilley via GitHub <sysbot+gh@w3.org>
• Date: Wed, 23 Sep 2020 15:33:12 +0000
• Message-ID: <issue_comment.created-697551300-1600875191-sysbot+gh@w3.org>
```> It may be worth solving this issue for implementers who copy the hwbToRgb() function by including the whiteness/blackness normalization logic in the function itself:

Agreed. Currently the spec handwaves here: [The following Javascript implementation of the algorithm assumes that the white and black components have already been normalized, so their sum is no larger than 100%, and have been converted into numbers in the range [0,1].](https://drafts.csswg.org/css-color-4/#hwb-to-rgb)

I tend to agree that it is better to do the normalization in the sample code.

Here is what [color.js does](https://github.com/LeaVerou/color.js/blob/master/src/spaces/hwb.js):

```js
from: {
srgb (rgb) {
let h = Color.spaces.hsl.from.srgb(rgb)[0];
// calculate white and black
let w = Math.min(...rgb);
let b = 1 - Math.max(...rgb);
w *= 100;
b *= 100;
return [h, w, b];
},

hsv (hsv) {
let [h, s, v] = hsv;

return [h, v * (100 - s) / 100, 100 - v];
}
},
```

and

```js
to: {
srgb (hwb) {
let [h, w, b] = hwb;

// Now convert percentages to [0..1]
w /=100;
b /= 100;

// Normalize so white plus black is no larger than 100
let sum = w + b;
if (sum > 1) {
w /= sum;
b /= sum;
}

// From https://drafts.csswg.org/css-color-4/#hwb-to-rgb
let rgb = Color.spaces.hsl.to.srgb([h, 100, 50]);
for (var i = 0; i < 3; i++) {
rgb[i] *= (1 - w - b);
rgb[i] += w;
}
return rgb;
}
},
```

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

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

Received on Wednesday, 23 September 2020 15:33:14 UTC