jc-color
Version:
A JavaScript tool for color compute and console print tool both for NodeJS and Browser.
1 lines • 1.45 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.rgbToHex=exports.hslToHex=exports.channelsToHex=exports.hex3ToHex6=exports.computHex6=exports.computHex3=void 0;const jc_utils_1=require("@jcstdio/jc-utils"),regs_1=require("../regs"),types_1=require("../types"),toChannels_1=require("./toChannels");function computHex3(e){return e.red.toString(16)+e.green.toString(16)+e.blue.toString(16)}function computHex6(e){return e.red.toString(16)+e.green.toString(16)+e.blue.toString(16)}function hex3ToHex6(e){if(regs_1.regHex3.test(e))return e.replace(/^#?/,"#").split("").map(e=>e+e).join("");throw(0,jc_utils_1.ValueError)(`"${e}" is nat a valid 3-digit hexadecimal color string.`)}function channelsToHex(e){return"#"+e.red.toString(16).padStart(2,"0")+e.green.toString(16).padStart(2,"0")+e.blue.toString(16).padStart(2,"0")}function hslToHex(e){var t,r,o;if((0,types_1.isHSLColor)(e))return{red:t,green:r,blue:o}=(0,toChannels_1.hslToRgbChannels)(e),"#"+(t<<16|r<<8|o).toString(16).padStart(6,"0");throw(0,jc_utils_1.ValueError)(`"${e}" is nat a valid hsl color string.`)}function rgbToHex(e){if((0,types_1.isRgbColor)(e))return channelsToHex((0,toChannels_1.rgbToChannels)(e));throw(0,jc_utils_1.ValueError)(`"${e}" is nat a valid rgb color string.`)}exports.computHex3=computHex3,exports.computHex6=computHex6,exports.hex3ToHex6=hex3ToHex6,exports.channelsToHex=channelsToHex,exports.hslToHex=hslToHex,exports.rgbToHex=rgbToHex;