Skip to content

Instantly share code, notes, and snippets.

@nhall
Last active June 7, 2019 17:39
Show Gist options
  • Save nhall/a423c1453ef38f34d58b4a52d4bbfddb to your computer and use it in GitHub Desktop.
Save nhall/a423c1453ef38f34d58b4a52d4bbfddb to your computer and use it in GitHub Desktop.
Automating contrast accessibility
function AccessibleColor(el) {
var backgroundRGB = el.css("backgroundColor");
backgroundRGB = backgroundRGB.substring(backgroundRGB.length - 1, 4).replace(/\s/g, "").split(",");
var sum = Math.round((
(parseInt(backgroundRGB[0]) * 299) +
(parseInt(backgroundRGB[1]) * 587) +
(parseInt(backgroundRGB[2]) * 114)) / 1000);
if (sum > 128) {
el.style.color = "black";
} else {
el.style.color = "white";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment