UNPKG

@peculiar/color

Version:

Library for color manipulation and conversion in JavaScript.

80 lines 3.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Color = void 0; var hex_to_rgb_1 = require("./hex_to_rgb"); var rgb_to_hsb_1 = require("./rgb_to_hsb"); var rgb_to_hex_1 = require("./rgb_to_hex"); var hsb_to_rgb_1 = require("./hsb_to_rgb"); var get_contrast_ratio_1 = require("./get_contrast_ratio"); var Color = /** @class */ (function () { function Color(color) { this.saturationMinMax = [10, 30]; this.brightnessMinMax = [15, 100]; var _a = typeof color === 'string' ? (0, hex_to_rgb_1.hexToRgb)(color) : color, r = _a[0], g = _a[1], b = _a[2]; this.red = r; this.green = g; this.blue = b; } Color.prototype.toHex = function () { return (0, rgb_to_hex_1.rgbToHex)(this.red, this.green, this.blue); }; Color.prototype.toHexString = function () { return "#".concat(this.toHex()); }; Color.prototype.toRgb = function () { return [this.red, this.green, this.blue]; }; Color.prototype.toRgbString = function () { return "rgb(".concat(this.toRgb().join(', '), ")"); }; Color.prototype.toHsb = function () { return (0, rgb_to_hsb_1.rgbToHsb)(this.red, this.green, this.blue); }; Color.prototype.palette = function () { var _a = this.toHsb(), h = _a[0], s = _a[1], b = _a[2]; var steps = 5; var saturationTintStep = (s - this.saturationMinMax[0]) / steps; var saturationShadeStep = (s - this.saturationMinMax[1]) / steps; var brightnessTintStep = (this.brightnessMinMax[1] - b) / steps; var brightnessShadeStep = (b - this.brightnessMinMax[0]) / steps; if (brightnessTintStep < 0) { brightnessTintStep = 0; } if (brightnessShadeStep < 0) { brightnessShadeStep = 0; } if (saturationTintStep < 0) { saturationTintStep = 0; } if (saturationShadeStep < 0) { saturationShadeStep = 0; } var tints = []; var shades = []; for (var i = 1; i <= steps; i += 1) { var tintRgb = (0, hsb_to_rgb_1.hsbToRgb)(h, Math.floor(s - saturationTintStep * i), Math.floor(b + brightnessTintStep * i)); var shadeRgb = (0, hsb_to_rgb_1.hsbToRgb)(h, Math.floor(s - saturationShadeStep * i), Math.floor(b - brightnessShadeStep * i)); tints.push(new Color(tintRgb)); shades.push(new Color(shadeRgb)); } return { tint5: tints[4], tint4: tints[3], tint3: tints[2], tint2: tints[1], tint1: tints[0], base: this, shade1: shades[0], shade2: shades[1], shade3: shades[2], shade4: shades[3], shade5: shades[4], }; }; Color.prototype.getContrastRatio = function (color) { return (0, get_contrast_ratio_1.getContrastRatio)([this.red, this.green, this.blue], color); }; return Color; }()); exports.Color = Color; //# sourceMappingURL=color.js.map