Skip to content

Commit bb85755

Browse files
committed
[css-color-5] rgb and hsl examples
1 parent 8519b35 commit bb85755

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

css-color-4/utilities.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,34 @@ function LCH_to_r2020(LCH) {
9797
// so check for that :)
9898

9999
return gam_2020(XYZ_to_lin_2020(D50_to_D65(Lab_to_XYZ(LCH_to_Lab(LCH)))));
100-
}
100+
}
101+
102+
// this is straight from the CSS Color 4 spec
103+
104+
function hslToRgb(hue, sat, light) {
105+
// For simplicity, this algorithm assumes that the hue has been normalized
106+
// to a number in the half-open range [0, 6), and the saturation and lightness
107+
// have been normalized to the range [0, 1]. It returns an array of three numbers
108+
// representing the red, green, and blue channels of the colors,
109+
// normalized to the range [0, 1]
110+
if( light <= .5 ) {
111+
var t2 = light * (sat + 1);
112+
} else {
113+
var t2 = light + sat - (light * sat);
114+
}
115+
var t1 = light * 2 - t2;
116+
var r = hueToRgb(t1, t2, hue + 2);
117+
var g = hueToRgb(t1, t2, hue);
118+
var b = hueToRgb(t1, t2, hue - 2);
119+
return [r,g,b];
120+
}
121+
122+
function hueToRgb(t1, t2, hue) {
123+
if(hue < 0) hue += 6;
124+
if(hue >= 6) hue -= 6;
125+
126+
if(hue < 1) return (t2 - t1) * hue + t1;
127+
else if(hue < 3) return t2;
128+
else if(hue < 4) return (t2 - t1) * (4 - hue) + t1;
129+
else return t1;
130+
}

css-color-5/Overview.bs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,16 @@ When an origin color is present, the following keywords can also be used in this
371371
- 'b' is a <<percentage>> that corresponds to the origin color's blue channel after its conversion to sRGB
372372
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
373373

374+
<div class="example">
375+
To manipulate color channels in the sRGB colorspace:
376+
377+
<pre>
378+
rgb(from <span class="swatch" style="--color: indianred"></span> indianred, 255 g b)
379+
</pre>
380+
381+
This takes the sRGB value of indianred (205 92 92) and replaces the red channel with 255 to give <span class="swatch" style="--color: rgb(255 92 92)"></span> rgb(255 92 92).
382+
</div>
383+
374384
<h4 id="relative-HSL">Relative HSL colors</h4>
375385

376386
The grammar of the ''hsl()'' function is extended as follows:
@@ -387,6 +397,15 @@ When an origin color is present, the following keywords can also be used in this
387397
- 'l' is a <<percentage>> that corresponds to the origin color's HSL lightness after its conversion to sRGB
388398
- 'alpha' is a <<percentage>> that corresponds to the origin color's alpha transparency
389399

400+
<div class="example">
401+
This adds 180 degrees to the hue angle, giving a complementary color.
402+
<pre>
403+
--accent: <span class="swatch" style="--color: lightseagreen"></span> lightseagreen;
404+
--complement: <span class="swatch" style="--color: hsl(357deg 70% 41%)"></span> hsl(from var(--accent), calc(h+180) s l);
405+
</pre>
406+
lightseagreen is hsl(177deg 70% 41%), so --complement is <span class="swatch" style="--color: hsl(357deg 70% 41%)"></span> hsl(357deg 70% 41%)
407+
</div>
408+
390409
<h4 id="relative-HWB">Relative HWB colors</h4>
391410

392411
The grammar of the ''hwb()'' function is extended as follows:

0 commit comments

Comments
 (0)