JSFiddle - React, Tailwind, and code Playground
HTML
<div style="background: rgb(180, 136, 148);"></div>
<div style="background: rgb(74, 77, 198);"></div>
<div style="background: rgb(4, 194, 208);"></div>
<div style="background: rgb(181, 12, 106);"></div>
<div style="background: rgb(220, 202, 95);"></div>
<div style="background: rgb(245, 222, 14);"></div>
<div style="background: rgb(242, 93, 134);"></div>
<div style="background: rgb(196, 14, 151);"></div>
<div style="background: rgb(120, 76, 159);"></div>
<div style="background: rgb(165, 67, 71);"></div>
<div style="background: rgb(212, 210, 150);"></div>
<div style="background: rgb(251, 232, 58);"></div>
<div style="background: rgb(177, 250, 62);"></div>
<div style="background: rgb(136, 27, 255);"></div>
<div style="background: rgb(172, 4, 147);"></div>
<div style="background: rgb(26, 26, 204);"></div>
CSS
div {
width: 130px;
font-weight: bold;
font-family: Verdana;
font-size: 14px;
padding: 4px 0;
text-align: center;
}
JavaScript
/*
// Rec. 601 (NTSC) luma coef
var Pr = .299,
Pg = .587,
Pb = .114;
*/
// Rec. 709 (sRGB) luma coef
var Pr = .2126,
Pg = .7152,
Pb = .0722;
function rgb2hsv(r, g, b) {
r /= 255; g /= 255; b /= 255;
var max, min, delta, f, i;
max = Math.max(r, g, b);
min = Math.min(r, g, b);
delta = max - min;
if (delta == 0) return [undefined, 0, max];
f = (r == min) ? g - b : ((g == min) ? b - r : r - g);
i = (r == min) ? 3 : ((g == min) ? 5 : 1);
return [60*(i - f/delta), delta/max, max];
}
function rgb2hsp(r, g, b) {
var hsv = rgb2hsv(r, g, b);
r /= 255; g /= 255; b /= 255;
var p = Math.sqrt(
Pr * r*r +
Pg * g*g +
Pb * b*b
);
return [hsv[0],hsv[1], p];
}
$(document).ready(function() {
$("div").text("quick brown fox").each(function(){
var $t = $(this),
rgb = $t.css("background-color").match(/[\d, ]+/)[0].split(", "),
hsp = rgb2hsp(rgb[0],rgb[1],rgb[2]),
lum = Math.round(hsp[2]*255);
$t.css("color", "rgb(" + [lum,lum,lum] + ")");
});
});