Skip to content

Latest commit

 

History

History
61 lines (44 loc) · 1.78 KB

readme.md

File metadata and controls

61 lines (44 loc) · 1.78 KB

isDark.js

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() and HSLA()
  • RGB() and RGBA() (including with percentage values)
  • Hexadecimal (#RGB, #RGBA, #RRGGBB and #RRGGBBAA)
  • Every keyname, including rebeccapurple and transparent (return both true)

isDark.js, however, don't detect illegal and deprecated values. It doesn't support currentColor.

Security

Security issues has been solved. Even if the code uses eval, it is safe.

How to use

Just call window.isDark("black") // true.

Examples

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");

isLight()

function isLight(color){
    return !isDark(color);
}

License

This code is under MIT License.

Ressources

About colors and brightness :

About conversion (all conversion functions were modified) :