UNPKG

@techmely/utils

Version:

Collection of helpful JavaScript / TypeScript utils

41 lines (36 loc) 853 B
/*! * @techmely/utils * Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com> * MIT Licensed */ import { hexColor } from "./chunk-B45TLRXM.mjs"; // src/colors.ts function isHex(hex) { return hexColor.test(hex); } function percentToHex(percent) { if (percent < 0 || percent > 100) { throw new Error("Value must in range [0, 100]"); } const intValue = Math.round(percent / 100 * 255); const hexValue = intValue.toString(16); return hexValue.padStart(2, "0").toUpperCase(); } var HEX_LENGTH = 6; var HEX_OPACITY_LENGTH = 8; function alphaHex(hex, alpha) { if (!hex) { throw new Error("Hex value is required"); } if (hex.length === HEX_OPACITY_LENGTH) { return `${hex.slice(0, HEX_LENGTH)}${percentToHex(alpha)}`; } return `${hex}${percentToHex(alpha)}`; } export { isHex, percentToHex, alphaHex };