<div style="--background-color--: #ffffff; --color--: #000000;">
<h1 style="--text-align--: center;">getBlackOrWhite</h1>
<p style="--text-align--: center;">Automatically sets a black or white background color for the set text color.</p>
<p style="--text-align--: center;"><label>Text color: <input type="color" value="#000000" id="color" /></label></p>
<h2 style="--text-align--: center;">Dummy text</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<h3 style="--text-align--: center;">Pale color</h3>
<p class="pale-color">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<h3 style="--text-align--: center;">Extra pale color</h3>
<p class="x-pale-color">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<h3 style="--text-align--: center;">Double extra pale color</h3>
<p class="xx-pale-color">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<h3 style="--text-align--: center;">Triple extra pale color</h3>
<p class="xxx-pale-color">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
* {
box-sizing: border-box;
line-height: calc(1em + var(--leading));
}
::before,
::after {
box-sizing: inherit;
}
[style~="--background-color--:"] {
background-color: var(--background-color--);
}
[style~="--color--:"] {
color: var(--color--);
}
[style~="--text-align--:"] {
text-align: var(--text-align--);
text-wrap: balance;
}
html {
--leading: 0.75rem;
font-family: ui-sans-serif, system-ui;
}
body {
margin: 0;
}
div {
padding-block: 2rem;
padding-inline: max(2rem, 50% - 20rem);
}
h1 {
font-size: calc(12em / 6);
}
h2 {
font-size: calc(12em / 7);
}
h3 {
font-size: calc(12em / 8);
}
:is(h1, h2, h3, p) {
margin: 0;
}
* + :is(h1, h2, h3) {
margin-block-start: 4rem;
}
* + p {
margin-block-start: 2rem;
}
.pale-color {
color: color-mix(in srgb, var(--color--), var(--background-color--) 20%);
}
.x-pale-color {
color: color-mix(in srgb, var(--color--), var(--background-color--) 40%);
}
.xx-pale-color {
color: color-mix(in srgb, var(--color--), var(--background-color--) 60%);
}
.xxx-pale-color {
color: color-mix(in srgb, var(--color--), var(--background-color--) 80%);
}
((d) => {
/**
* @see https://w3c.github.io/wcag/techniques/general/G17
*/
function getBlackOrWhite(hex) {
const _rgb = {
r: parseInt(hex.substring(1, 3), 16) / 255,
g: parseInt(hex.substring(3, 5), 16) / 255,
b: parseInt(hex.substring(5, 7), 16) / 255
},
rgb = {};
for (const key in _rgb) {
rgb[key] =
_rgb[key] <= 0.04045
? _rgb[key] / 12.92
: ((_rgb[key] + 0.055) / 1.055) ** 2.4;
}
const contrastRatio =
1.05 / (0.2126 * rgb["r"] + 0.7152 * rgb["g"] + 0.0722 * rgb["b"] + 0.05);
return 7 > contrastRatio ? "black" : "white";
}
const color = d.getElementById("color");
color.addEventListener("change", (event) => {
const div = d.querySelector("div");
div.style.setProperty("--color--", color.value);
div.style.setProperty("--background-color--", getBlackOrWhite(color.value));
});
})(document);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.