Skip to content

isDark.js is the shortest (yet still fast) way to know if a color is dark or light. It supports most color formats. No dependencies, crossbrowser and no uses of the DOM.

Notifications You must be signed in to change notification settings

JDMCreator/isDark.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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) :

About

isDark.js is the shortest (yet still fast) way to know if a color is dark or light. It supports most color formats. No dependencies, crossbrowser and no uses of the DOM.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published