@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
55 lines • 2.43 kB
JavaScript
import { RgbColors } from "@aurigma/design-atoms-model/Colors";
var ColorParser = /** @class */ (function () {
function ColorParser(namedColorConverter, rgbColorParser, cmykColorParser, labColorParser, spotColorParser) {
this._defaultColor = RgbColors.black;
this._namedColorConverter = namedColorConverter;
this._rgbColorParser = rgbColorParser;
this._cmykColorParser = cmykColorParser;
this._labColorParser = labColorParser;
this._spotColorParser = spotColorParser;
this._mappedParsers = {
"device-cmyk": this._cmykColorParser,
"cmyk": this._cmykColorParser,
"lab": this._labColorParser,
"spot": new SpotColorParserAdapter(this._spotColorParser, this)
};
}
ColorParser.prototype.parse = function (colorString) {
var result = this._tryParse(colorString);
if (result != null) {
return result;
}
console.warn("Unable to parse color from string " + colorString + ".");
return this._defaultColor.clone();
};
ColorParser.prototype._tryParse = function (colorString) {
var _a;
colorString = colorString.trim();
var result = this._tryParseWithMappedParsers(colorString);
if (result != null) {
return result;
}
colorString = (_a = this._namedColorConverter.convert(colorString)) !== null && _a !== void 0 ? _a : colorString;
return this._rgbColorParser.parse(colorString);
};
ColorParser.prototype._tryParseWithMappedParsers = function (colorString) {
var parserKey = Object.keys(this._mappedParsers).find(function (key) { return colorString.startsWith(key); });
if (parserKey != null) {
return this._mappedParsers[parserKey].parse(colorString);
}
return null;
};
return ColorParser;
}());
export { ColorParser };
var SpotColorParserAdapter = /** @class */ (function () {
function SpotColorParserAdapter(_spotColorParser, _colorParser) {
this._spotColorParser = _spotColorParser;
this._colorParser = _colorParser;
}
SpotColorParserAdapter.prototype.parse = function (colorString) {
return this._spotColorParser.parse(colorString, this._colorParser);
};
return SpotColorParserAdapter;
}());
//# sourceMappingURL=ColorParser.js.map