Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. /*
  2. *Define color based on color contrast
  3. */
  4. String.prototype.contrast = function() {
  5. var t = this.toString().replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, function(t, n, r, a) {
  6. return n + n + r + r + a + a
  7. }),
  8. n = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);
  9. return out = n ? {
  10. r: parseInt(n[1], 16),
  11. g: parseInt(n[2], 16),
  12. b: parseInt(n[3], 16)
  13. } : null, out = Math.round((299 * out.r + 587 * out.g + 114 * out.b) / 1e3), contrast = out < 125 ? "#fff" : "#000", contrast
  14. };
  15.  
  16. /** EG: **/
  17. let hexColor = "#fff"
  18. console.log({contrast:hexColor.contrast()});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement