Created
June 10, 2016 12:36
-
-
Save getflourish/659b66490ab795c61408b27ffe477bce to your computer and use it in GitHub Desktop.
Check if a color is light or dark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var getRGB = function(b){ | |
var a; | |
if(b&&b.constructor==Array&&b.length==3)return b; | |
if(a=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(b))return[parseInt(a[1]),parseInt(a[2]),parseInt(a[3])]; | |
if(a=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(b))return[parseFloat(a[1])*2.55,parseFloat(a[2])*2.55,parseFloat(a[3])*2.55]; | |
if(a=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(b))return[parseInt(a[1],16),parseInt(a[2],16),parseInt(a[3], | |
16)]; | |
if(a=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(b))return[parseInt(a[1]+a[1],16),parseInt(a[2]+a[2],16),parseInt(a[3]+a[3],16)]; | |
return (typeof (colors) != "undefined")?colors[jQuery.trim(b).toLowerCase()]:null | |
}; | |
var luminance_get = function(color) { | |
var rgb = getRGB(color); | |
if (!rgb) return null; | |
return 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]; | |
} | |
var luminance = luminance_get("rgb( " + color.join(",") + ")"); | |
if (luminance > 180) { | |
// black on white | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment