UNPKG

@aurigma/design-atoms-model

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

74 lines 2.57 kB
import { ColorFactory } from "./index"; var HsbColor = /** @class */ (function () { function HsbColor(h, s, b, a) { if (h === void 0) { h = 0; } if (s === void 0) { s = 0; } if (b === void 0) { b = 0; } if (a === void 0) { a = 0; } this.H = h; this.S = s; this.B = b; this.A = a; } HsbColor.fromRgb = function (rgbColor) { var r = rgbColor.r / 255; var g = rgbColor.g / 255; var b = rgbColor.b / 255; var max = Math.max(r, g, b); var min = Math.min(r, g, b); var h = max; var br = max; var d = max - min; var s = max === 0 ? 0 : d / max; if (max === min) { h = 0; // achromatic } else { switch (max) { case r: h = (g - b) / d + (g < b ? 6 : 0); break; case g: h = (b - r) / d + 2; break; case b: h = (r - g) / d + 4; break; } h /= 6 % 360; } return new HsbColor(h, s, br, rgbColor.a); }; HsbColor.prototype.toRgb = function () { var h = this.H * 6, i = Math.floor(h), f = h - i, p = this.B * (1 - this.S), q = this.B * (1 - f * this.S), t = this.B * (1 - (1 - f) * this.S), mod = i % 6, r = [this.B, q, p, p, t, this.B][mod], g = [t, this.B, this.B, q, p, p][mod], b = [p, p, t, this.B, this.B, q][mod]; var color = { r: Math.round(r * 255), g: Math.round(g * 255), b: Math.round(b * 255), a: this.A, type: "RgbColor" }; return ColorFactory.createColor(color, false, true); }; HsbColor.prototype.clone = function () { return new HsbColor(this.H, this.S, this.B, this.A); }; HsbColor.prototype.getDarken = function (value) { var cloned = this.clone(); cloned.B = Math.max(cloned.B - value, 0); return cloned; }; HsbColor.prototype.getLighten = function (value) { var cloned = this.clone(); cloned.B = Math.min(cloned.B + value, 1); return cloned; }; HsbColor.prototype.getFaded = function (value) { var cloned = this.clone(); cloned.A = Math.max(cloned.A - value * 255, 0); return cloned; }; return HsbColor; }()); export { HsbColor }; //# sourceMappingURL=HsbColor.js.map