Determine if a set of values could be an RGB or RGBA color
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.
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.
npm install --save @chriscodesthings/color-looks-like-rgba
import colorLooksLikeRGBA from '@chriscodesthings/color-looks-like-rgba';
console.log(colorLooksLikeRGBA([100, 149, 237, 1])); // cornflowerblue
// => true
This package uses types from:
colorLooksLikeRGBA([r, g, b, (a)]);
- r, g, b: red, green and blue values in the range 0-255
- a (optional): alpha value in the range 0-1
Returns true
if the values could be an RGBA color, false
otherwise.
// called when some input changes
function setNewColour(r, g, b, a) {
if( !colorLooksLikeRGBA[r, g, b, a]) {
return;
}
// do something
}