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.

209 lines 7.55 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { Color } from "./Color"; var RgbColor = /** @class */ (function (_super) { __extends(RgbColor, _super); function RgbColor(value) { var _this = _super.call(this, value) || this; _this._r = 0; _this._g = 0; _this._b = 0; _this._a = 0; if (value == null) return _this; var incorrectRgbColorMessage = "Incorrect RgbColor format: " + JSON.stringify(value); if (typeof value == "string") { var colorString = value; if (value.indexOf("#") !== 0 && value.indexOf("rgb") !== 0) { colorString = _this._parseNamedColor(value); if (colorString == null) console.warn("Unknown rgb color name: " + value); } if (_this._tryInitFromRgba(colorString)) return _this; if (_this._tryInitFromRgb(colorString)) return _this; if (!_this._tryInitFromHex(colorString)) console.warn(incorrectRgbColorMessage); } else { try { _this._init(value); } catch (e) { console.warn(incorrectRgbColorMessage); } } return _this; } RgbColor.prototype._parseNamedColor = function (value) { if (window.getComputedStyle) { var div = document.createElement("div"); div.style.color = value; document.body.appendChild(div); var result = window.getComputedStyle(div).color; document.body.removeChild(div); return result; } else { console.warn("Unable to parse rgb color string " + value); return null; } }; RgbColor.prototype._tryInitFromRgba = function (value) { var match = RgbColor.matchRgba(value); if (match != null && match.length === 6) { var a = parseInt((parseFloat(match[4]) * 255).toFixed(0)); a = Math.max(a, 0); a = Math.min(a, 255); this._init({ r: +match[1], g: +match[2], b: +match[3], a: a }); return true; } return false; }; RgbColor.prototype._tryInitFromRgb = function (value) { var match = RgbColor.matchRgb(value); if (match != null && match.length === 4) { this._init({ r: +match[1], g: +match[2], b: +match[3], a: 255 }); return true; } return false; }; RgbColor.prototype._tryInitFromHex = function (value) { var shortHex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; value = value.replace(shortHex, function (m, r, g, b) { return r + r + g + g + b + b; }); var standardHex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i; var match = standardHex.exec(value); if (match != null && match.length === 4) { this._init({ r: parseInt(match[1], 16), g: parseInt(match[2], 16), b: parseInt(match[3], 16), a: 255 }); return true; } return false; }; Object.defineProperty(RgbColor.prototype, "type", { get: function () { return "RgbColor"; }, enumerable: true, configurable: true }); Object.defineProperty(RgbColor.prototype, "r", { get: function () { return this._r; }, set: function (value) { this._r = value; this._updatePreview(); }, enumerable: true, configurable: true }); ; ; Object.defineProperty(RgbColor.prototype, "g", { get: function () { return this._g; }, set: function (value) { this._g = value; this._updatePreview(); }, enumerable: true, configurable: true }); ; ; Object.defineProperty(RgbColor.prototype, "b", { get: function () { return this._b; }, set: function (value) { this._b = value; this._updatePreview(); }, enumerable: true, configurable: true }); ; ; Object.defineProperty(RgbColor.prototype, "a", { get: function () { return this._a; }, set: function (value) { this._a = value; this._updatePreview(); }, enumerable: true, configurable: true }); ; ; Object.defineProperty(RgbColor.prototype, "alpha", { get: function () { return this.a; }, enumerable: true, configurable: true }); RgbColor.matchRgba = function (string) { RgbColor._rgba.lastIndex = 0; return RgbColor._rgba.exec(string); }; RgbColor.matchRgb = function (string) { RgbColor._rgb.lastIndex = 0; return RgbColor._rgb.exec(string); }; RgbColor.prototype.getData = function () { var data = _super.prototype.getData.call(this); data.r = this.r; data.g = this.g; data.b = this.b; data.a = this.a; return data; }; RgbColor.prototype.clone = function () { return new RgbColor(this); }; RgbColor.prototype.toString = function () { return this._getPreview(); }; RgbColor.prototype.equals = function (other) { if (other instanceof RgbColor) { return _super.prototype.equals.call(this, other); } return false; }; RgbColor.prototype._getPreview = function () { if (this.a === 255) { return "rgb(" + this.r + "," + this.g + "," + this.b + ")"; } else { var a = (this.a / 255).toFixed(7); return "rgba(" + this.r + "," + this.g + "," + this.b + "," + a + ")"; } }; RgbColor.prototype._init = function (colorObject) { _super.prototype._init.call(this, colorObject); colorObject.a = colorObject.a != null ? colorObject.a : 255; this._validateNumber(colorObject.r); this._validateNumber(colorObject.g); this._validateNumber(colorObject.b); this._validateNumber(colorObject.a); this.r = colorObject.r; this.g = colorObject.g; this.b = colorObject.b; this.a = colorObject.a; this._updatePreview(); }; RgbColor.prototype._updatePreview = function () { this.preview = this._getPreview(); }; RgbColor._rgba = /^\s*rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\,\s*(\d{1,}(\.\d{1,})?)\s*\)\s*;{0,1}\s*$/g; RgbColor._rgb = /^\s*rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)\s*;{0,1}\s*$/g; return RgbColor; }(Color)); export { RgbColor }; //# sourceMappingURL=RgbColor.js.map