RGBA to Hex, converting transparent to solid hex!
Designers in Figma sometimes use a transparent color on top of solid white. This little scripts converts it!
RGBA to Hex
function hexify(color: string): string {
// first replace case insensitive
const values = color
.replace(/rgba?\(/, '')
.replace(/\)/, '')
.replace(/[\s+]/g, '')
.split(',');
const a = parseFloat(values[3] || '1');
const r = Math.floor(a * parseInt(values[0], 10) + (1 - a) * 255);
const g = Math.floor(a * parseInt(values[1], 10) + (1 - a) * 255);
const b = Math.floor(a * parseInt(values[2], 10) + (1 - a) * 255);
return "#" +
("0" + r.toString(16)).slice(-2) +
("0" + g.toString(16)).slice(-2) +
("0" + b.toString(16)).slice(-2);
}
const hexed: string = hexify('rgba(216, 210, 187, 0.20)');
console.log(hexed);
// #f7f6f1