@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
52 lines • 2.09 kB
JavaScript
import { RgbColors } from "@aurigma/design-atoms-model/Colors";
export class ColorParser {
constructor(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)
};
}
parse(colorString) {
const result = this._tryParse(colorString);
if (result != null) {
return result;
}
console.warn(`Unable to parse color from string ${colorString}.`);
return this._defaultColor.clone();
}
_tryParse(colorString) {
var _a;
colorString = colorString.trim();
let 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);
}
_tryParseWithMappedParsers(colorString) {
const parserKey = Object.keys(this._mappedParsers).find(key => colorString.startsWith(key));
if (parserKey != null) {
return this._mappedParsers[parserKey].parse(colorString);
}
return null;
}
}
class SpotColorParserAdapter {
constructor(_spotColorParser, _colorParser) {
this._spotColorParser = _spotColorParser;
this._colorParser = _colorParser;
}
parse(colorString) {
return this._spotColorParser.parse(colorString, this._colorParser);
}
}
//# sourceMappingURL=ColorParser.js.map