color-to-vec4
Version:
:white_square_button: a color (rgb, rgba, hex) to vec4 convertor
22 lines (13 loc) • 402 B
JavaScript
// @flow
import mapChannel from './mapChannel';
import hexToRgb from './hexToRgb';
export default (color: string) => {
const isValid = /^#[0-9A-F]{6}$/i.test(color);
if (!isValid) {
throw new Error(`${color} is not a valid hex color`);
}
const colorObj = hexToRgb(color);
const {r, g, b} = colorObj;
const colors = [r, g, b];
return [...colors.map(c => mapChannel(c)), 1];
};