@primer/primitives
Version:
Typography, spacing, and color primitives for Primer design system
12 lines (11 loc) • 489 B
JavaScript
export const rgbaFloatToHex = ({ r, g, b, a }, alpha = true) => {
const values = [r, g, b, alpha === true && a && a < 1 ? a : undefined].filter(item => item !== undefined);
if (r > 1 || r < 0 || g > 1 || g < 0 || b > 1 || b < 0) {
throw new Error('Invalid RgbaFloat value. R, G and B values must be between 0 and 1.');
}
return `#${values
.map(v => Number(parseInt(`${v * 255}`, 10))
.toString(16)
.padStart(2, '0'))
.join('')}`;
};