lib-colors
Version:
Simple node.js library for work with colors
83 lines (82 loc) • 2.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RGB = void 0;
const cmyk_class_1 = require("../cmyk/cmyk.class");
const gray_class_1 = require("../gray/gray.class");
const alpha_is_set_helper_1 = require("../helpers/alpha_is_set.helper");
const clamp_helper_1 = require("../helpers/clamp.helper");
const hsl_class_1 = require("../hsl/hsl.class");
const lab_class_1 = require("../lab/lab.class");
const color_rgb_const_1 = require("./consts/color.rgb.const");
const max_rgb_const_1 = require("./consts/max.rgb.const");
const rgb_from_hex_helper_1 = require("./helpers/rgb_from_hex.helper");
const rgb_to_cmyk_helper_1 = require("./helpers/rgb_to_cmyk.helper");
const rgb_to_gray_helper_1 = require("./helpers/rgb_to_gray.helper");
const rgb_to_hex_helper_1 = require("./helpers/rgb_to_hex.helper");
const rgb_to_hsl_helper_1 = require("./helpers/rgb_to_hsl.helper");
const rgb_to_lab_helper_1 = require("./helpers/rgb_to_lab.helper");
const rgb_to_string_helper_1 = require("./helpers/rgb_to_string.helper");
class RGB {
color = { ...color_rgb_const_1.colorRGB };
max = max_rgb_const_1.maxRGB;
get r() {
return this.color.r;
}
set r(value) {
this.color.r = (0, clamp_helper_1.clamp)(value, 0, this.max.r);
}
get g() {
return this.color.g;
}
set g(value) {
this.color.g = (0, clamp_helper_1.clamp)(value, 0, this.max.g);
}
get b() {
return this.color.b;
}
set b(value) {
this.color.b = (0, clamp_helper_1.clamp)(value, 0, this.max.b);
}
get a() {
return this.color.a;
}
set a(value) {
this.color.a = (0, alpha_is_set_helper_1.alphaIsSet)(value) ? (0, clamp_helper_1.clamp)(Number(value), 0, 1) : undefined;
}
constructor(r = 0, g = 0, b = 0, a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
toString() {
return (0, rgb_to_string_helper_1.rgbToString)(this.color);
}
fromHex(hex) {
const { r, g, b, a } = (0, rgb_from_hex_helper_1.rgbFromHex)(hex);
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
toHex() {
return (0, rgb_to_hex_helper_1.rgbToHex)(this.color);
}
cmyk() {
const { c, m, y, k, a } = (0, rgb_to_cmyk_helper_1.rgbToCmyk)(this.color);
return new cmyk_class_1.CMYK(c, m, y, k, a);
}
gray() {
const { g, a } = (0, rgb_to_gray_helper_1.rgbToGray)(this.color);
return new gray_class_1.Gray(g, a);
}
hsl() {
const { h, s, l, a } = (0, rgb_to_hsl_helper_1.rgbToHsl)(this.color);
return new hsl_class_1.HSL(h, s, l, a);
}
lab() {
const { l, a, b, alpha } = (0, rgb_to_lab_helper_1.rgbToLab)(this.color);
return new lab_class_1.LAB(l, a, b, alpha);
}
}
exports.RGB = RGB;