Function to calculate the YIQ value of a color in RGB format
Determines the perceived brightness of a colour based on the YIQ calculation.
npm install --save @chriscodesthings/rgb-color-yiq-value
import rgbColorYIQValue from '@chriscodesthings/rgb-color-yiq-value';
console.log(rgbColorYIQValue([100, 149, 237])); // cornflowerblue
// => 144.381
This package uses types from:
rgbYIQValue([r, g, b, (a)]);
- r, g, b: red green and blue color components
- a (optional): Alpha value is ignored if present
Returns calculated YIQ value of the color.
// get contrasting text colour for a given background colour
function getTextColourForBackground(r, g, b) {
if( rgbYIQValue([r, g, b]) > 128) {
// colour is light, return black
return [0, 0, 0];
}
// colour is dark, return white
return [255, 255, 255];
}