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.

90 lines 3.04 kB
import { Color } from "./Color"; import { Utils } from "./Utils"; export class LabColor extends Color { constructor(value) { super(value); this.l = 0; this.a = -128; this.b = -128; this.alpha = 0; if (value == null) return; const incorrectLabColorMessage = `Incorrect LabColor format: ${JSON.stringify(value)}`; if (typeof value == "string") { if (!this._tryInitFromString(value)) console.warn(incorrectLabColorMessage); else this._previewFromServer = Utils.isValidRgbColor(this.preview); } else { try { this._init(value); } catch (e) { console.warn(incorrectLabColorMessage); } } } get type() { return "LabColor"; } _tryInitFromString(value) { LabColor._lab.lastIndex = 0; const match = LabColor._lab.exec(value); if (match != null && match.length === 8) { this._init({ l: +match[1], a: +match[2], b: +match[3], alpha: match[5] != null ? Utils.convertPercentToByte(match[5]) : 255, preview: match[7] != null ? match[7] : null }); return true; } return false; } clone() { return new LabColor(this); } equals(color) { if (color instanceof LabColor) { const baseEquals = super.equals(color); return baseEquals && color instanceof LabColor && this.l === color.l && this.a === color.a && this.b === color.b && this.alpha === color.alpha; } return false; } toString() { let result = `lab(${this.l},${this.a},${this.b},${Utils.convertByteToPercent(this.alpha)}`; if (this.preview != null) result += `,${this.preview}`; result += ")"; return result; } getData() { const data = super.getData(); data.l = this.l; data.a = this.a; data.b = this.b; data.alpha = this.alpha; return data; } _getPreview() { return this.preview; } _init(colorObject) { super._init(colorObject); colorObject.alpha = colorObject.alpha != null ? colorObject.alpha : 255; this._validateNumber(colorObject.l); this._validateNumber(colorObject.a, -128, 127); this._validateNumber(colorObject.b, -128, 127); this._validateNumber(colorObject.alpha); this.l = colorObject.l; this.a = colorObject.a; this.b = colorObject.b; this.alpha = colorObject.alpha; } } LabColor._lab = /^\s*lab\(\s*(\d{1,3})\s*,\s*(-*\d{1,3})\s*,\s*(-*\d{1,3})\s*(,\s*(\d{1,3})%)?\s*(\,\s*(.*)\s*)?\)\s*;{0,1}\s*$/g; //# sourceMappingURL=LabColor.js.map