JSPM

@chriscodesthings/color-looks-like-rgba

1.1.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q37560F
  • License MIT

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

Package Exports

  • @chriscodesthings/color-looks-like-rgba
  • @chriscodesthings/color-looks-like-rgba/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@chriscodesthings/color-looks-like-rgba) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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...