isDark.js is the smallest code to know if any color is dark or light. 538 bytes gzipped // 727 bytes minified. No dependency and crossbrowser (IE9+). Will be adapted for Node.JS.
isDark.js supports every color :
HSL()
andHSLA()
RGB()
andRGBA()
(including with percentage values)- Hexadecimal (
#RGB
,#RGBA
,#RRGGBB
and#RRGGBBAA
) - Every keyname, including
rebeccapurple
andtransparent
(return bothtrue
)
isDark.js, however, don't detect illegal and deprecated values. It doesn't support currentColor
.
Security issues has been solved. Even if the code uses eval
, it is safe.
Just call window.isDark("black") // true
.
isDark("black");
isDark("white");
isDark("salmon");
isDark("rgb(200 24 37)");
isDark("rgba(200, 24, 37)");
isDark("rgb(0% 0% 0%)");
isDark("hsl(0 50% 100%)");
isDark("hsla(0, 50%, 100%, 1)");
isDark("#FFF");
isDark("FFF");
isDark("#FF0000");
isDark("FF000000");
function isLight(color){
return !isDark(color);
}
This code is under MIT License.
About colors and brightness :
- Calculating the Perceived Brightness of a Color (NbdTech)
- Techniques For Accessibility Evaluation And Repair Tools (W3C)
- CSS4 Color Module Level 4 (W3C)
About conversion (all conversion functions were modified) :