1.1.2 • Published 1 year ago
@chriscodesthings/color-looks-like-rgba v1.1.2
color-looks-like-rgba   
  
 
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] // HSLWhite in HSL turns into dark green as RGB but both are valid
[0, 0, 100, 1] // White as HSLA 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-rgbaUsage
import colorLooksLikeRGBA from '@chriscodesthings/color-looks-like-rgba';
console.log(colorLooksLikeRGBA([100, 149, 237, 1])); // cornflowerblue
// => trueTypes
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
}