Determine if a color in RGB format is dark using YIQ calculation
Determine if a colour is dark using the YIQ calculation.
npm install --save @chriscodesthings/rgb-color-is-dark
import rgbColorIsDark from '@chriscodesthings/rgb-color-is-dark';
console.log(rgbColorIsDark([100, 149, 237])); // cornflowerblue
// => false
rgbColorIsDark([r, g, b, (a)]);
- r, g, b: Red, green and blue color components
- a (optional): Alpha value is ignored if present
Returns boolean true
if color is dark based on YIQ calculation, false
otherwise.
// get contrasting text colour for a given background colour
function getTextColourForBackground(r, g, b) {
if( rgbColorIsDark([r, g, b])) {
// colour is dark, return white
return [255, 255, 255];
}
// colour is light, return black
return [0, 0, 0];
}