Back to home

InstallationExample codeUsage
Contrast Colors main image

Contrast Colors

Short and sharp. I needed to use something to help auto-determine what text color to use for components based on the background color. Also - American spelling for those Australians out there. I know.

Installation

1 2 3 4 # npm npm install --save get-contrast # or yarn yarn add get-contrast

Example code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 const contrast = require("get-contrast"); const arg = process.argv[2]; if (!arg) { console.error('Error: Color arg required eg. "#000"'); process.exit(1); } const isLight = contrast.isAccessible(arg, "#000"); const res = `${arg} is light: ${isLight}. You should use ${ isLight ? "black" : "white" } for the font.`; console.log(res);

Usage

1 2 3 4 5 6 node index.js "#666" #666 is light: false. You should use white for the font. node index.js "#000" #000 is light: false. You should use white for the font. node index.js "#888" #888 is light: true. You should use black for the font.

Original code can be found on my Github account.

Hello is a series that is about short, sharp examples. Read more on this series to find small gems to add your toolset.

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.