IsDark.js
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.
Install / Use
/learn @JDMCreator/IsDark.jsREADME
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()andHSLA()RGB()andRGBA()(including with percentage values)- Hexadecimal (
#RGB,#RGBA,#RRGGBBand#RRGGBBAA) - Every keyname, including
rebeccapurpleandtransparent(return bothtrue)
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 :
- 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) :
Related Skills
openhue
340.2kControl Philips Hue lights and scenes via the OpenHue CLI.
sag
340.2kElevenLabs text-to-speech with mac-style say UX.
weather
340.2kGet current weather and forecasts via wttr.in or Open-Meteo
tweakcc
1.5kCustomize Claude Code's system prompts, create custom toolsets, input pattern highlighters, themes/thinking verbs/spinners, customize input box & user message styling, support AGENTS.md, unlock private/unreleased features, and much more. Supports both native/npm installs on all platforms.
