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.

96 lines 4.2 kB
import { Color } from "./Color"; import { RgbColor } from "./RgbColor"; import { CmykColor } from "./CmykColor"; import { GrayscaleColor } from "./GrayscaleColor"; import { LabColor } from "./LabColor"; import * as _ from "underscore"; var ColorFactory = /** @class */ (function () { function ColorFactory() { } ColorFactory.registerColor = function (typeName, shortName, colorConstructor) { if (typeName) ColorFactory._createFunctionByType[typeName] = colorConstructor; if (shortName) ColorFactory._createFunctionByName[shortName] = colorConstructor; }; Object.defineProperty(ColorFactory, "rgbColorProfileId", { set: function (value) { ColorFactory._rgbColorProfileId = value; }, enumerable: true, configurable: true }); ColorFactory._updateRgbColor = function (rgbColor) { if (_.isEmpty(rgbColor === null || rgbColor === void 0 ? void 0 : rgbColor.profileId)) rgbColor.profileId = ColorFactory._rgbColorProfileId; return rgbColor; }; ColorFactory.createColor = function (value, throwException, attachRgbColorProfile) { if (throwException === void 0) { throwException = false; } if (attachRgbColorProfile === void 0) { attachRgbColorProfile = false; } try { if (value instanceof Color) { return value; } else if (typeof value === "string") { if (value.indexOf("device-cmyk") === 0 || value.indexOf("cmyk") === 0) return new CmykColor(value); else if (value.indexOf("lab") === 0) return new LabColor(value); else if (value.indexOf("spot") === 0) return ColorFactory._createColorByName("spot", value); else return attachRgbColorProfile ? ColorFactory._updateRgbColor(new RgbColor(value)) : new RgbColor(value); } else { return ColorFactory._fromData(value, attachRgbColorProfile); } } catch (e) { if (throwException !== false) throw e; console.warn("Unable to create color from value " + JSON.stringify(value) + ". Error: " + e); return new RgbColor("rgba(0,0,0,1)"); } }; ColorFactory._createColorByName = function (name, value) { if (!ColorFactory._createFunctionByName[name]) throw "Color with name '" + name + "' was not registered in ColorFactory"; var colorConstructor = ColorFactory._createFunctionByName[name]; return new colorConstructor(value); }; ColorFactory._createColorByType = function (name, value) { if (!ColorFactory._createFunctionByType[name]) throw "Color with type name '" + name + "' was not registered in ColorFactory"; var colorConstructor = ColorFactory._createFunctionByType[name]; return new colorConstructor(value); }; ColorFactory._fromData = function (data, attachRgbColorProfile) { if (attachRgbColorProfile === void 0) { attachRgbColorProfile = false; } if (data == null) throw "Value cannot be null"; var type = data.type; if (type === "RgbColor") { return attachRgbColorProfile ? ColorFactory._updateRgbColor(new RgbColor(data)) : new RgbColor(data); } else if (type === "CmykColor") { return new CmykColor(data); } else if (type === "GrayscaleColor") { return new GrayscaleColor(data); } else if (type === "LabColor") { return new LabColor(data); } else if (type === "SpotColor") { return ColorFactory._createColorByType("SpotColor", data); } else throw "Unsupported color type " + type; }; ColorFactory._createFunctionByType = {}; ColorFactory._createFunctionByName = {}; return ColorFactory; }()); export { ColorFactory }; //# sourceMappingURL=ColorFactory.js.map