@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
49 lines • 2.33 kB
JavaScript
import { RgbColor } from "@aurigma/design-atoms-model/Colors";
import { convertRelativeToByte } from "./Utils";
var RgbColorParser = /** @class */ (function () {
function RgbColorParser() {
this._mappedParsers = {
"#": this._parseHex,
"rgb(": this._parseRgb,
"rgba(": this._parseRgba
};
}
RgbColorParser.prototype.parse = function (colorString) {
colorString = colorString.trim().toLowerCase();
var parserKey = Object.keys(this._mappedParsers).find(function (key) { return colorString.startsWith(key); });
if (parserKey == null) {
return null;
}
return this._mappedParsers[parserKey](colorString);
};
RgbColorParser.prototype._parseHex = function (hexString) {
var shortHexRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hexString = hexString.replace(shortHexRegex, function (r, g, b) { return r + r + g + g + b + b; });
var standardHexRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
var match = standardHexRegex.exec(hexString);
if (match == null || match.length !== 4) {
return null;
}
return new RgbColor(parseInt(match[1], 16), parseInt(match[2], 16), parseInt(match[3], 16), 255);
};
RgbColorParser.prototype._parseRgb = function (rgbString) {
var rgbRegex = /^\s*rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)\s*;{0,1}\s*$/;
var match = rgbRegex.exec(rgbString);
if (match == null || match.length !== 4) {
return null;
}
return new RgbColor(parseInt(match[1]), parseInt(match[2]), parseInt(match[3]), 255);
};
RgbColorParser.prototype._parseRgba = function (rgbaString) {
var rgbaRegex = /^\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*$/;
var match = rgbaRegex.exec(rgbaString);
if (match == null || match.length !== 6) {
return null;
}
var alpha = convertRelativeToByte(parseFloat(match[4]));
return new RgbColor(parseInt(match[1]), parseInt(match[2]), parseInt(match[3]), alpha);
};
return RgbColorParser;
}());
export { RgbColorParser };
//# sourceMappingURL=RgbColorParser.js.map