@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.
147 lines • 5.77 kB
JavaScript
import { Color } from "./Color";
import { Utils } from "./Utils";
import { CmykComponentValidator } from "../Validators/CmykComponentValidator";
import { Clamp } from "../Math";
export class CmykColor extends Color {
constructor(value) {
super(value);
this.c = 255;
this.m = 255;
this.y = 255;
this.k = 255;
this.a = 255;
if (value == null)
return;
const incorrectCmykColorMessage = `Incorrect CmykColor format: ${JSON.stringify(value)}`;
if (typeof value == "string") {
if (this._tryInitFromPercentCmyk(value) || this._tryInitFromRelativeCmyk(value) || this._tryInitFromDeviceCmyk(value))
this._previewFromServer = Utils.isValidRgbColor(this.preview);
else
console.warn(incorrectCmykColorMessage);
}
else {
try {
this._init(value);
}
catch (e) {
console.warn(incorrectCmykColorMessage);
}
}
}
get type() { return "CmykColor"; }
get alpha() { return this.a; }
_tryInitFromDeviceCmyk(value) {
const 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*$/g;
const match = deviceCmykRegex.exec(value);
if (match != null && match.length === 10) {
const cmyk = {
c: +match[1],
m: +match[2],
y: +match[3],
k: +match[4],
preview: match[9]
};
if (match[6] != undefined)
cmyk.a = this._convertRelativeToByte(match[6]);
this._init(cmyk);
return true;
}
return false;
}
_tryInitFromPercentCmyk(value) {
const 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*$/g;
const match = percentCmykRegex.exec(value);
if (match != null && match.length === 7) {
new CmykComponentValidator(value, match, 100).validate();
const cmyk = {
c: Utils.convertPercentToByte(match[1]),
m: Utils.convertPercentToByte(match[2]),
y: Utils.convertPercentToByte(match[3]),
k: Utils.convertPercentToByte(match[4]),
preview: match[6]
};
if (match[5] != undefined)
cmyk.a = Utils.convertPercentToByte(match[5]);
this._init(cmyk);
return true;
}
return false;
}
_tryInitFromRelativeCmyk(value) {
const 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*$/g;
const match = relativeCmykRegex.exec(value);
if (match != null && match.length === 7) {
new CmykComponentValidator(value, match, 1).validate();
const cmyk = {
c: this._convertRelativeToByte(match[1]),
m: this._convertRelativeToByte(match[2]),
y: this._convertRelativeToByte(match[3]),
k: this._convertRelativeToByte(match[4]),
preview: match[6]
};
if (match[5] != undefined)
cmyk.a = this._convertAlpha(match[5]);
this._init(cmyk);
return true;
}
return false;
}
_convertRelativeToByte(value) {
return Clamp(0, parseInt((parseFloat(value) * 255).toFixed(0)), 255);
}
_convertAlpha(value) {
return Clamp(parseInt((parseFloat(value) * 255).toFixed(0)), 0, 255);
}
clone() {
return new CmykColor(this);
}
getData() {
const data = super.getData();
data.c = this.c;
data.m = this.m;
data.y = this.y;
data.k = this.k;
data.a = this.a;
return data;
}
equals(color, ignorePreview = false) {
if (!(color instanceof CmykColor))
return false;
const baseEquals = super.equals(color, ignorePreview);
return baseEquals &&
color instanceof CmykColor &&
this.c === color.c &&
this.m === color.m &&
this.y === color.y &&
this.k === color.k &&
this.a === color.a;
}
toString() {
let result = `cmyk(${this._toRelative(this.c)},${this._toRelative(this.m)},${this._toRelative(this.y)},${this._toRelative(this.k)},${this._toRelative(this.a)}`;
if (this.preview != null)
result += `,${this.preview}`;
result += ")";
return result;
}
_toRelative(component) {
return (component / 255).toFixed(7);
}
_getPreview() {
return this.preview;
}
_init(colorObject) {
super._init(colorObject);
colorObject.a = colorObject.a != null ? colorObject.a : 255;
this._validateNumber(colorObject.c);
this._validateNumber(colorObject.m);
this._validateNumber(colorObject.y);
this._validateNumber(colorObject.k);
this._validateNumber(colorObject.a);
this.c = colorObject.c;
this.m = colorObject.m;
this.y = colorObject.y;
this.k = colorObject.k;
this.a = colorObject.a;
}
}
//# sourceMappingURL=CmykColor.js.map