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.

86 lines 3.65 kB
import { Color } from "./Color"; import { RgbColor } from "./RgbColor"; import { CmykColor } from "./CmykColor"; import { GrayscaleColor } from "./GrayscaleColor"; import { LabColor } from "./LabColor"; export class ColorFactory { static registerColor(typeName, shortName, colorConstructor) { if (typeName) ColorFactory._createFunctionByType[typeName] = colorConstructor; if (shortName) ColorFactory._createFunctionByName[shortName] = colorConstructor; } static set rgbColorProfileId(value) { ColorFactory._rgbColorProfileId = value; } static _updateRgbColor(rgbColor) { const profileId = rgbColor === null || rgbColor === void 0 ? void 0 : rgbColor.profileId; if (profileId == null || profileId === "") { rgbColor.profileId = ColorFactory._rgbColorProfileId; } return rgbColor; } static createColor(value, throwException = false, 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)"); } } static _createColorByName(name, value) { if (!ColorFactory._createFunctionByName[name]) throw `Color with name '${name}' was not registered in ColorFactory`; const colorConstructor = ColorFactory._createFunctionByName[name]; return new colorConstructor(value); } static _createColorByType(name, value) { if (!ColorFactory._createFunctionByType[name]) throw `Color with type name '${name}' was not registered in ColorFactory`; const colorConstructor = ColorFactory._createFunctionByType[name]; return new colorConstructor(value); } static _fromData(data, attachRgbColorProfile = false) { if (data == null) throw `Value cannot be null`; const 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 = {}; //# sourceMappingURL=ColorFactory.js.map