Skip to content

Commit 3df52e7

Browse files
committed
[css-color-4] Correct HSL & HWB code, fix w3c#6570
1 parent 83038d4 commit 3df52e7

5 files changed

Lines changed: 78 additions & 47 deletions

File tree

css-color-4/Overview.bs

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,36 +2314,23 @@ Converting HSL colors to sRGB colors</h3>
23142314

23152315
Converting an HSL color to sRGB is straightforward mathematically.
23162316
Here's a simple implementation of the conversion algorithm in JavaScript.
2317-
For simplicity, this algorithm assumes that the hue has been normalized to
2318-
a number in the half-open range [0, 6),
2319-
and the saturation and lightness have been normalized to the range [0, 1].
23202317
It returns an array of three numbers
23212318
representing the red, green, and blue channels of the colors,
23222319
normalized to the range [0, 1].
23232320

2324-
<pre class='lang-javascript'>
2325-
function hslToRgb(hue, sat, light) {
2326-
if( light &lt;= .5 ) {
2327-
var t2 = light * (sat + 1);
2328-
} else {
2329-
var t2 = light + sat - (light * sat);
2330-
}
2331-
var t1 = light * 2 - t2;
2332-
var r = hueToRgb(t1, t2, hue + 2);
2333-
var g = hueToRgb(t1, t2, hue);
2334-
var b = hueToRgb(t1, t2, hue - 2);
2335-
return [r,g,b];
2336-
}
2321+
<pre class="include-code lang-javascript">
2322+
path: hslToRgb.js
2323+
highlight: js
2324+
</pre>
23372325

2338-
function hueToRgb(t1, t2, hue) {
2339-
if(hue &lt; 0) hue += 6;
2340-
if(hue >= 6) hue -= 6;
2326+
<h3 id='rgb-to-hsl'>
2327+
Converting sRGB colors to HSL colors</h3>
23412328

2342-
if(hue &lt; 1) return (t2 - t1) * hue + t1;
2343-
else if(hue &lt; 3) return t2;
2344-
else if(hue &lt; 4) return (t2 - t1) * (4 - hue) + t1;
2345-
else return t1;
2346-
}
2329+
Conversion in the reverse direction proceeds similarly.
2330+
2331+
<pre class="include-code lang-javascript">
2332+
path: rgbToHsl.js
2333+
highlight: js
23472334
</pre>
23482335

23492336
<h3 id='hsl-examples'>
@@ -2709,35 +2696,19 @@ Converting HWB colors to sRGB colors</h3>
27092696
first normalizes the white and black components,
27102697
so their sum is no larger than 100%.
27112698

2712-
<pre class="lang-javascript">
2713-
white /= 100;
2714-
black /= 100;
2715-
if (white + black >= 1) {
2716-
var gray = white / (white + black);
2717-
return [gray, gray, gray];
2718-
}
2719-
function hwbToRgb(hue, white, black) {
2720-
var rgb = hslToRgb(hue, 1, .5);
2721-
for(var i = 0; i &lt; 3; i++) {
2722-
rgb[i] *= (1 - white - black);
2723-
rgb[i] += white;
2724-
}
2725-
return rgb;
2726-
}
2699+
<pre class="include-code lang-javascript">
2700+
path: hwbToRgb.js
2701+
highlight: js
27272702
</pre>
27282703

27292704
<h3 id="rgb-to-hwb">Converting sRGB colors to HWB colors</h3>
27302705

27312706
Conversion in the reverse direction proceeds similarly.
27322707

2733-
<pre class="lang-javascript">
2734-
function rgbToHwb(red, green, blue) {
2735-
var hsl = rgbToHsl(rgb);
2736-
var white = Math.min(...rgb);
2737-
var black = 1 - Math.max(...rgb);
2738-
return([hsl[0], white*100, black*100]);
2739-
}
2740-
</pre>
2708+
<pre class="include-code lang-javascript">
2709+
path: rgbToHwb.js
2710+
highlight: js
2711+
</pre>
27412712

27422713
<h3 id='hwb-examples'>
27432714
Examples of HWB Colors</h3>

css-color-4/hslToRgb.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function hslToRgb (hue, sat, light) {
2+
hue = hue % 360;
3+
4+
if (hue < 0) {
5+
hue += 360;
6+
}
7+
8+
sat /= 100;
9+
light /= 100;
10+
11+
function f(n) {
12+
let k = (n + hue/30) % 12;
13+
let a = sat * Math.min(light, 1 - light);
14+
return light - a * Math.max(-1, Math.min(k - 3, 9 - k, 1));
15+
}
16+
17+
return [f(0), f(8), f(4)];
18+
}

css-color-4/hwbToRgb.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function hwbToRgb(hue, white, black) {
2+
white /= 100;
3+
black /= 100;
4+
if (white + black >= 1) {
5+
let gray = white / (white + black);
6+
return [gray, gray, gray];
7+
}
8+
let rgb = hslToRgb(hue, 100, 50);
9+
for (let i = 0; i < 3; i++) {
10+
rgb[i] *= (1 - white - black);
11+
rgb[i] += white;
12+
}
13+
return rgb;
14+
}

css-color-4/rgbToHsl.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function rgbToHsl (red, green, blue) {
2+
let max = Math.max(red, green, blue);
3+
let min = Math.min(red, green, blue);
4+
let [hue, sat, light] = [NaN, 0, (min + max)/2];
5+
let d = max - min;
6+
7+
if (d !== 0) {
8+
sat = (light === 0 || light === 1)
9+
? 0
10+
: (max - light) / Math.min(light, 1 - light);
11+
12+
switch (max) {
13+
case red: hue = (green - blue) / d + (green < blue ? 6 : 0); break;
14+
case green: hue = (blue - red) / d + 2; break;
15+
case blue: hue = (red - green) / d + 4;
16+
}
17+
18+
hue = hue * 60;
19+
}
20+
21+
return [hue, sat * 100, light * 100];
22+
}

css-color-4/rgbToHwb.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function rgbToHwb(red, green, blue) {
2+
var hsl = rgbToHsl(red, green, blue);
3+
var white = Math.min(red, green, blue);
4+
var black = 1 - Math.max(red, green, blue);
5+
return([hsl[0], white*100, black*100]);
6+
}

0 commit comments

Comments
 (0)