Skip to content

Instantly share code, notes, and snippets.

@xhinking
Created December 26, 2014 06:21
Show Gist options
  • Save xhinking/e362ff63d62b6593dc03 to your computer and use it in GitHub Desktop.
Save xhinking/e362ff63d62b6593dc03 to your computer and use it in GitHub Desktop.
Determine if the given hex color is light or dark. Helpful to determining foreground color against a background color.
// https://github.com/scottcorgan/contrast
var hexToRgb = require('hex-to-rgb');
module.exports = function contrast (hex) {
var rgb = hexToRgb(hex);
var o = Math.round(((parseInt(rgb[0]) * 299) + (parseInt(rgb[1]) * 587) + (parseInt(rgb[2]) * 114)) /1000);
return (o <= 180) ? 'dark' : 'light';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment