Answers for "javascript add alpha to hex"

0

javascript add alpha to hex

function addAlpha(color: string, opacity: number): string {
    // coerce values so ti is between 0 and 1.
    const _opacity = Math.round(Math.min(Math.max(opacity || 1, 0), 1) * 255);
    return color + _opacity.toString(16).toUpperCase();
}
addAlpha('FF0000', 1); // returns 'FF0000FF'
addAlpha('FF0000', 0.5); // returns 'FF000080'
Posted by: Guest on November-29-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language