@rashedmakkouk/dev-utils
Version:
Utility library.
23 lines (22 loc) • 871 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/** Utilities */
const color_convert_1 = require("color-convert");
/**
* Converts color from 'keyword' (e.g. green) or 'hex' (e.g. #00FF00) to RGBa
* value. Useful when there is a need to apply color opacity.
*
* @param color - Color `Name` or `HEX` value.
* @param alpha - Alpha channel `opacity` between '0' and '1'; default 1.
*
* @returns RGBa value for valid colors else 'rgba(0,0,0,alpha)'.
*/
function toRGBa(color, alpha = 1) {
const numbers = color.startsWith('#')
? color_convert_1.hex.rgb(color)
: color_convert_1.keyword.rgb(color);
!numbers.length && numbers.push(0, 0, 0);
/* eslint-disable-next-line @typescript-eslint/restrict-template-expressions */
return `rgba(${numbers.map((num) => num)},${alpha ?? 1})`;
}
exports.default = toRGBa;