@chriscodesthings/rgb-color-is-dark
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

rgb-color-is-dark
Test workflow status NPM Version License: MIT

Determine if a color in RGB format is dark using YIQ calculation

Description

Determine if a colour is dark using the YIQ calculation.

See...


Install

npm install --save @chriscodesthings/rgb-color-is-dark

Usage

import rgbColorIsDark from '@chriscodesthings/rgb-color-is-dark';

console.log(rgbColorIsDark([100, 149, 237])); // cornflowerblue
// => false

Syntax

rgbColorIsDark([r, g, b, (a)]);

Parameters

  • r, g, b: Red, green and blue color components
  • a (optional): Alpha value is ignored if present

Return Value

Returns boolean true if color is dark based on YIQ calculation, false otherwise.

Examples

// 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];
} 

See Also...

Readme

Keywords

Package Sidebar

Install

npm i @chriscodesthings/rgb-color-is-dark

Weekly Downloads

13

Version

2.1.0

License

MIT

Unpacked Size

7.55 kB

Total Files

8

Last publish

Collaborators

  • chriscodesthings