lib-colors
Version:
Simple node.js library for work with colors
16 lines (15 loc) • 596 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.rgbToHex = rgbToHex;
const alpha_is_set_helper_1 = require("../../helpers/alpha_is_set.helper");
function rgbToHex(rgb) {
const { r, g, b, a } = { ...rgb };
const rHex = toHexComponent(r);
const gHex = toHexComponent(g);
const bHex = toHexComponent(b);
const aHex = (0, alpha_is_set_helper_1.alphaIsSet)(a) ? toHexComponent((a || 1) * 256 - 1) : '';
return `#${rHex}${gHex}${bHex}${aHex}`;
}
function toHexComponent(color) {
return Math.round(color).toString(16).padStart(2, '0');
}