Skip to content

Commit f7aa43f

Browse files
committed
[css-color-4] add reverse conversion, sRGB to HWB
1 parent ab1c1e8 commit f7aa43f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

css-color-4/Overview.bs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,8 @@ Converting HWB colors to sRGB colors</h3>
15701570

15711571
Converting an HWB color to sRGB is straightforward,
15721572
and related to how one converts HSL to RGB.
1573-
The following Javascript implementation of the algorithm assumes that the white and black components have already been normalized,
1573+
The following Javascript implementation of the algorithm
1574+
assumes that the white and black components have already been normalized,
15741575
so their sum is no larger than 100%,
15751576
and have been converted into numbers in the range [0,1].
15761577

@@ -1585,6 +1586,23 @@ Converting HWB colors to sRGB colors</h3>
15851586
}
15861587
</pre>
15871588

1589+
<h3 class="rgb-to-hwb">Converting sRGB colors to HWB colors</h3>
1590+
1591+
Conversion in the reverse direction proceeds similarly.
1592+
The following Javascript implementation
1593+
leaves white and black
1594+
in the range [0,1]
1595+
rather thn converting them to [0.0%,100.0%].
1596+
1597+
<pre class="lang-javascript">
1598+
function rgbToHwb(red, green, blue) {
1599+
var hsl = rgbToHsl(rgb);
1600+
var white = Math.min(...rgb);
1601+
var black = 1 - Math.max(...rgb);
1602+
return([hsl[0], white, black]);
1603+
}
1604+
</pre>
1605+
15881606
<h3 id='hwb-examples'>
15891607
Examples of HWB Colors</h3>
15901608

0 commit comments

Comments
 (0)