@youtwitface/ntcjs
Version:
A Node CommonJS compatible wrapper for the Name That Color library (ntc js) - http://chir.ag/projects/ntc/
42 lines (41 loc) • 1.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.hsl = exports.rgb = void 0;
// adopted from: Farbtastic 1.2
// http://acko.net/dev/farbtastic
const rgb = (color, divider = 1) => {
return [
parseInt(color.slice(1, 3), 16) / divider,
parseInt(color.slice(3, 5), 16) / divider,
parseInt(color.slice(5, 7), 16) / divider,
];
};
exports.rgb = rgb;
// adopted from: Farbtastic 1.2
// http://acko.net/dev/farbtastic
const hsl = (color) => {
const [r, g, b] = exports.rgb(color, 255);
const min = Math.min(r, Math.min(g, b));
const max = Math.max(r, Math.max(g, b));
const delta = max - min;
let h = 0;
let s = 0;
const l = (min + max) / 2;
if (l > 0 && l < 1) {
s = delta / (l < 0.5 ? 2 * l : 2 - 2 * l);
}
if (delta > 0) {
if (max === r && max !== g) {
h += (g - b) / delta;
}
if (max === g && max !== b) {
h += 2 + (b - r) / delta;
}
if (max === b && max !== r) {
h += 4 + (r - g) / delta;
}
h /= 6;
}
return [h * 255, s * 255, l * 255];
};
exports.hsl = hsl;