@chriscodesthings/color-looks-like-rgba
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

color-looks-like-rgba
Test workflow status NPM Version License: MIT

Determine if a set of values could be an RGB or RGBA color

Description

Function to determine if a given set of values could be an RGBA color.

The reason we can only say 'looks like', is because it's impossible to be sure.

Consider these examples:

Black - this is the only colour that works for both formats:

[0, 0, 0, 1] // RGBA
[0, 0, 0, 1] // HSL

White in HSL turns into dark green as RGB but both are valid

[0, 0, 100, 1] // White as HSL

A nice orange colour in HSL turns green as RGB, while brown as RGB turns green as HSL!

As you can see, while there are differences in the allowed range for each value, there is also a lot of crossover which produce very different colours.

See...


Install

npm install --save @chriscodesthings/color-looks-like-rgba

Usage

import colorLooksLikeRGBA from '@chriscodesthings/color-looks-like-rgba';

console.log(colorLooksLikeRGBA([100, 149, 237, 1])); // cornflowerblue
// => true

Types

This package uses types from:

Syntax

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

Parameters

  • r, g, b: red, green and blue values in the range 0-255
  • a (optional): alpha value in the range 0-1

Returns

Returns true if the values could be an RGBA color, false otherwise.

Examples

// called when some input changes
function setNewColour(r, g, b, a) {
    if( !colorLooksLikeRGBA[r, g, b, a]) {
        return;
    }

    // do something
}

See Also...

Readme

Keywords

Package Sidebar

Install

npm i @chriscodesthings/color-looks-like-rgba

Weekly Downloads

50

Version

1.1.2

License

MIT

Unpacked Size

8.1 kB

Total Files

8

Last publish

Collaborators

  • chriscodesthings