UNPKG

@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.9 kB
import { CmykColor } from "@aurigma/design-atoms-model"; import { convertPercentToByte, convertRelativeToByte } from "./Utils"; export class CmykColorParser { constructor() { this._mappedParsers = new Map([ [CmykColorParser._percentCmykRegex, this._parsePercentCmyk], [CmykColorParser._relativeCmykRegex, this._parseRelativeCmyk], [CmykColorParser._deviceCmykRegex, this._parseDeviceCmyk], ]); } parse(colorString) { const parserKey = Array.from(this._mappedParsers.keys()).find(key => key.test(colorString)); if (parserKey == null) { return null; } return this._mappedParsers.get(parserKey)(parserKey.exec(colorString)); } _parsePercentCmyk(percentMatch) { if (percentMatch.length !== 7) { return null; } let alpha = 255; if (percentMatch[5] != null) { alpha = convertPercentToByte(parseFloat(percentMatch[5])); } return new CmykColor(convertPercentToByte(parseFloat(percentMatch[1])), convertPercentToByte(parseFloat(percentMatch[2])), convertPercentToByte(parseFloat(percentMatch[3])), convertPercentToByte(parseFloat(percentMatch[4])), alpha); } _parseRelativeCmyk(relativeMatch) { if (relativeMatch.length !== 7) { return null; } let alpha = 255; if (relativeMatch[5] != null) { alpha = convertRelativeToByte(parseFloat(relativeMatch[5])); } return new CmykColor(convertRelativeToByte(parseFloat(relativeMatch[1])), convertRelativeToByte(parseFloat(relativeMatch[2])), convertRelativeToByte(parseFloat(relativeMatch[3])), convertRelativeToByte(parseFloat(relativeMatch[4])), alpha); } _parseDeviceCmyk(deviceMatch) { if (deviceMatch.length !== 10) { return null; } let alpha = 255; if (deviceMatch[6] != null) { alpha = convertRelativeToByte(parseFloat(deviceMatch[6])); } return new CmykColor(parseInt(deviceMatch[1]), parseInt(deviceMatch[2]), parseInt(deviceMatch[3]), parseInt(deviceMatch[4]), alpha); } } CmykColorParser._percentCmykRegex = /^\s*cmyk\(\s*(\d{1,3}(?:\.\d{1,})?)%\s*,\s*(\d{1,3}(?:\.\d{1,})?)%\s*,\s*(\d{1,3}(?:\.\d{1,})?)%\s*,\s*(\d{1,3}(?:\.\d{1,})?)%\s*(?:\,\s*(\d{1,3}(?:\.\d{1,})?)%)?\s*(?:\,\s*(.*)\s*)?\);{0,1}\s*$/; CmykColorParser._relativeCmykRegex = /^\s*cmyk\(\s*(\d{1,3}(?:\.\d{1,})?)\s*,\s*(\d{1,3}(?:\.\d{1,})?)\s*,\s*(\d{1,3}(?:\.\d{1,})?)\s*,\s*(\d{1,3}(?:\.\d{1,})?)\s*(?:\,\s*(\d{1,3}(?:\.\d{1,})?))?\s*(?:\,\s*(.*)\s*)?\);{0,1}\s*$/; CmykColorParser._deviceCmykRegex = /^\s*device-cmyk\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*(\,\s*(\d{1,}(\.\d{1,})?))?\s*(\,\s*(.*)\s*)?\)\s*;{0,1}\s*$/; //# sourceMappingURL=CmykColorParser.js.map