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.

68 lines 2.21 kB
import { ColorFactory } from "./index"; export class HsbColor { constructor(h = 0, s = 0, b = 0, a = 0) { this.H = h; this.S = s; this.B = b; this.A = a; } static fromRgb(rgbColor) { const r = rgbColor.r / 255; const g = rgbColor.g / 255; const b = rgbColor.b / 255; const max = Math.max(r, g, b); const min = Math.min(r, g, b); let h = max; const br = max; const d = max - min; const 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); } toRgb() { const 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]; const 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); } clone() { return new HsbColor(this.H, this.S, this.B, this.A); } getDarken(value) { const cloned = this.clone(); cloned.B = Math.max(cloned.B - value, 0); return cloned; } getLighten(value) { const cloned = this.clone(); cloned.B = Math.min(cloned.B + value, 1); return cloned; } getFaded(value) { const cloned = this.clone(); cloned.A = Math.max(cloned.A - value * 255, 0); return cloned; } } //# sourceMappingURL=HsbColor.js.map