Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *Define color based on color contrast
- */
- String.prototype.contrast = function() {
- var t = this.toString().replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, function(t, n, r, a) {
- return n + n + r + r + a + a
- }),
- n = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);
- return out = n ? {
- r: parseInt(n[1], 16),
- g: parseInt(n[2], 16),
- b: parseInt(n[3], 16)
- } : null, out = Math.round((299 * out.r + 587 * out.g + 114 * out.b) / 1e3), contrast = out < 125 ? "#fff" : "#000", contrast
- };
- /** EG: **/
- let hexColor = "#fff"
- console.log({contrast:hexColor.contrast()});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement