Skip to content

Commit 8d04259

Browse files
committed
[css-color-5] complete contrast example
1 parent f718958 commit 8d04259

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

css-color-4/contrast.html

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
<h2>Contrast ratio of two colors</h2>
1818
<p>Pick a foreground and background color</p>
1919

20-
<input type="color" id="fg" value="#B7C9A3">
21-
<label for="fg">Foreground</label>
22-
<input type="color" id="bg" value="#104425">
23-
<label for="bg">Background</label>
20+
<div class="picker">
21+
<input type="color" id="fg" value="#B7C9A3">
22+
<label for="fg">Foreground</label>
23+
<p>Luminance: <output id="fgLuminance"></output></p>
24+
</div>
25+
<div class="picker">
26+
<input type="color" id="bg" value="#104425">
27+
<label for="bg">Background</label>
28+
<p>Luminance: <output id="bgLuminance"></output></p>
29+
</div>
2430

2531
<div id="result">
2632
<p>
@@ -36,6 +42,8 @@ <h2>Contrast ratio of two colors</h2>
3642

3743
var fgInput = document.querySelector("#fg");
3844
var bgInput = document.querySelector("#bg");
45+
var fgLuminanceOutput = document.querySelector("#fgLuminance");
46+
var bgLuminanceOutput = document.querySelector("#bgLuminance");
3947
var contrastOutput = document.querySelector("#contrastResult");
4048
var bgColor, fgColor;
4149

@@ -44,11 +52,10 @@ <h2>Contrast ratio of two colors</h2>
4452
(fgInput.oninput = bgInput.oninput = function (event) {
4553
bgColor = hexToArray(bgInput.value);
4654
fgColor = hexToArray(fgInput.value);
55+
bgLuminanceOutput.value = sRGB_to_luminance(bgColor);
56+
fgLuminanceOutput.value = sRGB_to_luminance(fgColor);
4757
console.log("watch", bgColor, fgColor);
4858
contrastOutput.value = contrast(bgColor, fgColor);
49-
// convert from string to array of number
50-
// compute contrast
51-
// update span
5259
})();
5360

5461

css-color-5/Overview.bs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Introduction {#intro}
3838

3939
This module adds three functions:
4040
''color-mix'',
41-
''color-contrast'' and
41+
''color-contrast'' and
4242
''color-adjust''.
4343

4444
The perceptually uniform ``lch()`` colorspace
@@ -129,7 +129,13 @@ Selecting the most contrasting color: the ''color-contrast()'' function {#colorc
129129
<pre class="lang-css">color-contrast(wheat tan, sienna, var(--myAccent), #d2691e)</pre>
130130

131131
The calculation is as follows:
132-
* wheat (#f5deb3) has relative luminance
132+
* wheat (#f5deb3), the background, has relative luminance 0.749
133+
* tan (#d2b48c) has relative luminance 0.482 and contrast ratio 1.501
134+
* sienna (#a0522d) has relative luminance 0.137 and contrast ratio 4.273
135+
* suppose myAccent has the value #b22222
136+
* #b22222 has relative luminance 0.107 and contrast ratio 5.081
137+
* #d2691e has relative luminance 0.305 and contrast ratio 2.249
138+
* The highest contrast ratio is 5.081 so var(--myAccent) wins
133139

134140
</div>
135141

0 commit comments

Comments
 (0)