Skip to content

Commit 3822c94

Browse files
committed
clarify that HSL, HWB and named colors resolve to sRGB. Update conversions.
1 parent d9d70d6 commit 3822c94

File tree

3 files changed

+81
-29
lines changed

3 files changed

+81
-29
lines changed

css-color-4/Overview.bs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ Resolving Color values</h2>
258258
<h2 id='numeric-rgb'>
259259
RGB Colors</h2>
260260

261-
There are several methods of directly specifying a color in terms of its RGBA channels.
261+
There are several methods of directly specifying an sRGB color in terms of its RGBA channels.
262262

263263
<h3 id='rgb-functions'>
264264
The RGB functions: ''rgb()'' and ''rgba()''</h3>
265265

266-
The ''rgb()'' function defines an RGB color by specifying the red, green, and blue channels directly.
266+
The ''rgb()'' function defines an sRGB color by specifying the red, green, and blue channels directly.
267267
Its syntax is:
268268

269269
<pre class='prod'>
@@ -396,6 +396,8 @@ Named Colors</h2>
396396
As usual for CSS-defined <<ident>>s,
397397
all of these keywords are case-insensitive.
398398

399+
The names resolve to colors in sRGB.
400+
399401
16 of CSS's named colors come from HTML originally:
400402
aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow.
401403
Most of the rest
@@ -842,6 +844,8 @@ HSL Colors: ''hsl()'' and ''hsla()'' functions</h2>
842844
It is also easier to create sets of matching colors
843845
(by keeping the hue the same and varying the saturation and lightness).
844846

847+
HSL colors resolve to sRGB.
848+
845849
<div class='example'>
846850
For example, the following colors can all be generated off of the basic "green" hue,
847851
just by varying the other two arguments:
@@ -868,9 +872,9 @@ HSL Colors: ''hsl()'' and ''hsla()'' functions</h2>
868872

869873

870874
<h3 id='hsl-to-rgb'>
871-
Converting HSL colors to RGB colors</h3>
875+
Converting HSL colors to sRGB colors</h3>
872876

873-
Converting an HSL color to RGB is straightforward mathematically.
877+
Converting an HSL color to sRGB is straightforward mathematically.
874878
Here's a simple implementation of the conversion algorithm in JavaScript.
875879
For simplicity, this algorithm assumes that the hue has been normalized to
876880
a number in the half-open range [0, 6),
@@ -922,6 +926,8 @@ Examples of HSL colors</h3>
922926
In each table, the X axis represents the saturation
923927
while the Y axis represents the lightness.
924928

929+
<div class="issue">The conversions in the table below are known to contain errors. They are copied from CSS Color 3, which aso had the same errors. Those colors were supposedly computed by a program in ABC. A future spec will correctly compute those colors. Meanwhile, please note that thses conversions are non-normative examples.</div>
930+
925931
<div style="overflow: hidden;" class="color-table">
926932
<table>
927933
<tr>
@@ -1259,6 +1265,8 @@ HWB Colors: ''hwb()'' function</h2>
12591265
Many color-pickers are based on the HWB color system,
12601266
due to its intuitiveness.
12611267

1268+
HWB colors resolve to sRGB.
1269+
12621270
<figure>
12631271
<img src="images/color-picker.png">
12641272
<figcaption>
@@ -1300,9 +1308,9 @@ HWB Colors: ''hwb()'' function</h2>
13001308
without any hint of the chosen hue.
13011309

13021310
<h3 id='hwb-to-rgb'>
1303-
Converting HWB colors to RGB colors</h3>
1311+
Converting HWB colors to sRGB colors</h3>
13041312

1305-
Converting an HWB color to RGB is straightforward,
1313+
Converting an HWB color to sRGB is straightforward,
13061314
and related to how one converts HSL to RGB.
13071315
The following Javascript implementation of the algorithm assumes that the white and black components have already been normalized,
13081316
so their sum is no larger than 100%,

css-color-4/conversions.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ function lin_ProPhoto(RGB) {
100100
return RGB.map(function (val) {
101101
return Math.pow(val, 1.8);
102102
});
103-
}
103+
}
104104

105105
function gam_ProPhoto(RGB) {
106106
// convert an array of linear-light ProPhotoRGB in the range 0.0-1.0
107107
// to gamma corrected form
108108
return RGB.map(function (val) {
109109
return Math.pow(val, 1/1.8);
110110
});
111-
}
111+
}
112112

113113
function lin_ProPhoto_to_XYZ(rgb) {
114114
// convert an array of linear-light ProPhotoRGB values to CIE XYZ
@@ -121,15 +121,60 @@ function lin_ProPhoto_to_XYZ(rgb) {
121121
]);
122122

123123
return Math.multiply(M, rgb).valueOf();
124-
}
124+
}
125125

126126
function XYZ_to_lin_ProPhoto(XYZ) {
127127
// convert XYZ to linear-light ProPhotoRGB
128128
var M = Math.matrix([
129129
[ 1.3457989731028281, -0.25558010007997534, -0.05110628506753401 ],
130130
[ -0.5446224939028347, 1.5082327413132781, 0.02053603239147973 ],
131131
[ 0.0, 0.0, 1.2119675456389454 ]
132+
]);
133+
134+
return Math.multiply(M, XYZ).valueOf();
135+
}
136+
137+
// a98rgb functions
138+
139+
function lin_a98rgb(RGB) {
140+
// convert an array of a98rgb values in the range 0.0 - 1.0
141+
// to linear light (un-companded) form.
142+
return RGB.map(function (val) {
143+
return Math.pow(val, 563/256);
144+
});
145+
}
132146

147+
function gam_a98rgb(RGB) {
148+
// convert an array of linear-light a98rgb in the range 0.0-1.0
149+
// to gamma corrected form
150+
return RGB.map(function (val) {
151+
return Math.pow(val, 256/563);
152+
});
153+
}
154+
155+
function lin_a98rgb_to_XYZ(rgb) {
156+
// convert an array of linear-light ProPhotoRGB values to CIE XYZ
157+
// using D50 (so no chromatic adaptation needed afterwards)
158+
// http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
159+
var M = Math.matrix([
160+
[ 0.5766690429101305, 0.1855582379065463, 0.1882286462349947 ],
161+
[ 0.29734497525053605, 0.6273635662554661, 0.07529145849399788 ],
162+
[ 0.02703136138641234, 0.07068885253582723, 0.9913375368376388 ]
163+
]);
164+
165+
return Math.multiply(M, rgb).valueOf();
166+
}
167+
168+
function XYZ_to_lin_a98rgb(XYZ) {
169+
// convert XYZ to linear-light ProPhotoRGB
170+
var M = Math.matrix([
171+
[ 2.0415879038107465, -0.5650069742788596, -0.34473135077832956 ],
172+
[ -0.9692436362808795, 1.8759675015077202, 0.04155505740717557 ],
173+
[ 0.013444280632031142, -0.11836239223101838, 1.0151749943912054 ]
174+
]);
175+
176+
return Math.multiply(M, XYZ).valueOf();
177+
}
133178

134179
//Rec. 2020-related functions
135180

css-color-4/matrixmaker.html

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313

1414

1515
// these are for ITU Rec BT.2020
16-
/*
17-
const xred=0.708 ;
18-
const yred=0.292 ;
16+
// const xred=0.708 ;
17+
// const yred=0.292 ;
1918

20-
const xgreen=0.170 ;
21-
const ygreen=0.797 ;
19+
// const xgreen=0.170 ;
20+
// const ygreen=0.797 ;
21+
22+
// const xblue=0.131 ;
23+
// const yblue=0.046 ;
2224

23-
const xblue=0.131 ;
24-
const yblue=0.046 ;
25-
*/
2625

2726
// these are for DCI P3
2827
// const xred=0.680 ;
@@ -35,17 +34,17 @@
3534
// const yblue=0.060 ;
3635

3736
// ProPhoto
38-
const xred=0.7347 ;
39-
const yred=0.2653 ;
37+
// const xred=0.7347 ;
38+
// const yred=0.2653 ;
4039

41-
const xgreen=0.1596 ;
42-
const ygreen=0.8404 ;
40+
// const xgreen=0.1596 ;
41+
// const ygreen=0.8404 ;
4342

44-
const xblue=0.0366 ;
45-
const yblue=0.0001 ;
43+
// const xblue=0.0366 ;
44+
// const yblue=0.0001 ;
4645

4746
// AdobeRGB, for testing
48-
/*
47+
4948
const xred=0.640 ;
5049
const yred=0.330 ;
5150

@@ -54,15 +53,15 @@
5453

5554
const xblue=0.150 ;
5655
const yblue=0.060 ;
57-
*/
56+
5857

5958
// D65
60-
// const xwhite=0.3127 ;
61-
// const ywhite=0.3290 ;
59+
const xwhite=0.3127 ;
60+
const ywhite=0.3290 ;
6261

6362
// D50
64-
const xwhite=0.3457 ;
65-
const ywhite=0.3585 ;
63+
// const xwhite=0.3457 ;
64+
// const ywhite=0.3585 ;
6665

6766

6867
// Relative XYZ values. Copy-paste-o-rama

0 commit comments

Comments
 (0)