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.

91 lines 3.65 kB
import { Color } from "./Color"; import { Utils } from "./Utils"; import { ColorFactory } from "./ColorFactory"; export class SpotColor extends Color { constructor(value) { super(value); if (value == null) return; const incorrectSpotColorMessage = `Incorrect SpotColor format: ${JSON.stringify(value)}`; if (typeof value == "string") { if (!this._tryInitFromString(value)) console.warn(incorrectSpotColorMessage); else this._previewFromServer = Utils.isValidRgbColor(this.preview); } else { try { this._init(value); } catch (e) { console.warn(incorrectSpotColorMessage); } } } get type() { return "SpotColor"; } _tryInitFromString(value) { SpotColor._spot.lastIndex = 0; const match = SpotColor._spot.exec(value); if (match != null && match.length === 10) { this._init({ inkName: match[1].replace(/'/g, "").replace(/\"/g, ""), inkColor: ColorFactory.createColor(match[2]), inkSolidity: match[4] != null ? +match[4] : 1, tint: match[7] != null ? Utils.convertPercentToByte(match[7]) : 255, preview: match[9] != null ? match[9] : null }); return true; } return false; } toString() { const inkColorWithoutPreview = this.inkColor.clone(); inkColorWithoutPreview.preview = null; return `spot('${this.inkName}',${inkColorWithoutPreview.toString()},${this.inkSolidity},${Utils.convertByteToPercent(this.tint)},${this.preview})`; } clone() { return new SpotColor(this); } equals(color) { if (color instanceof SpotColor) { const baseEquals = super.equals(color); return baseEquals && color instanceof SpotColor && this.inkName === color.inkName && this.inkColor.equals(color.inkColor) && this.inkSolidity === color.inkSolidity && this.alpha === color.alpha && this.tint === color.tint; } return false; } getData() { const data = super.getData(); data.inkName = this.inkName; data.inkColor = this.inkColor.getData(); data.inkSolidity = this.inkSolidity; data.alpha = this.alpha; data.tint = this.tint; return data; } _getPreview() { return this.preview; } _init(colorObject) { super._init(colorObject); colorObject.alpha = colorObject.alpha != null ? colorObject.alpha : 255; colorObject.tint = colorObject.tint != null ? colorObject.tint : 255; const inkColor = ColorFactory.createColor(colorObject.inkColor, true); this._validateString(colorObject.inkName); this._validateNumber(colorObject.inkSolidity, 0, 1); this._validateNumber(colorObject.alpha); this._validateNumber(colorObject.tint); this.inkName = colorObject.inkName; this.inkColor = inkColor; this.inkSolidity = colorObject.inkSolidity; this.alpha = colorObject.alpha; this.tint = colorObject.tint; } } SpotColor._spot = /^\s*spot\(\s*([^()]*)\s*,\s*(.{3,4}\([^()]*\))\s*(,\s*(\d{1}(\.\d{1,})?))?\s*(,\s*(\d{1,3})%)?\s*(,\s*(.*)\s*)?\)\s*;{0,1}\s*$/g; ColorFactory.registerColor("SpotColor", "spot", SpotColor); //# sourceMappingURL=SpotColor.js.map