@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.
168 lines • 5.43 kB
JavaScript
import { Color } from "./Color";
export class RgbColor extends Color {
constructor(value) {
super(value);
this._r = 0;
this._g = 0;
this._b = 0;
this._a = 0;
if (value == null)
return;
const incorrectRgbColorMessage = `Incorrect RgbColor format: ${JSON.stringify(value)}`;
if (typeof value == "string") {
let colorString = value;
if (value.indexOf("#") !== 0 && value.indexOf("rgb") !== 0) {
colorString = this._parseNamedColor(value);
if (colorString == null)
console.warn(`Unknown rgb color name: ${value}`);
}
if (this._tryInitFromRgba(colorString))
return;
if (this._tryInitFromRgb(colorString))
return;
if (!this._tryInitFromHex(colorString))
console.warn(incorrectRgbColorMessage);
}
else {
try {
this._init(value);
}
catch (e) {
console.warn(incorrectRgbColorMessage);
}
}
}
_parseNamedColor(value) {
if (window.getComputedStyle) {
const div = document.createElement("div");
div.style.color = value;
document.body.appendChild(div);
const result = window.getComputedStyle(div).color;
document.body.removeChild(div);
return result;
}
else {
console.warn(`Unable to parse rgb color string ${value}`);
return null;
}
}
_tryInitFromRgba(value) {
const match = RgbColor.matchRgba(value);
if (match != null && match.length === 6) {
let a = parseInt((parseFloat(match[4]) * 255).toFixed(0));
a = Math.max(a, 0);
a = Math.min(a, 255);
this._init({
r: +match[1],
g: +match[2],
b: +match[3],
a: a
});
return true;
}
return false;
}
_tryInitFromRgb(value) {
const match = RgbColor.matchRgb(value);
if (match != null && match.length === 4) {
this._init({
r: +match[1],
g: +match[2],
b: +match[3],
a: 255
});
return true;
}
return false;
}
_tryInitFromHex(value) {
const shortHex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
value = value.replace(shortHex, (m, r, g, b) => r + r + g + g + b + b);
const standardHex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
const match = standardHex.exec(value);
if (match != null && match.length === 4) {
this._init({
r: parseInt(match[1], 16),
g: parseInt(match[2], 16),
b: parseInt(match[3], 16),
a: 255
});
return true;
}
return false;
}
get type() { return "RgbColor"; }
get r() { return this._r; }
;
set r(value) { this._r = value; this._updatePreview(); }
;
get g() { return this._g; }
;
set g(value) { this._g = value; this._updatePreview(); }
;
get b() { return this._b; }
;
set b(value) { this._b = value; this._updatePreview(); }
;
get a() { return this._a; }
;
set a(value) { this._a = value; this._updatePreview(); }
;
get alpha() { return this.a; }
static matchRgba(string) {
RgbColor._rgba.lastIndex = 0;
return RgbColor._rgba.exec(string);
}
static matchRgb(string) {
RgbColor._rgb.lastIndex = 0;
return RgbColor._rgb.exec(string);
}
getData() {
const data = super.getData();
data.r = this.r;
data.g = this.g;
data.b = this.b;
data.a = this.a;
return data;
}
clone() {
return new RgbColor(this);
}
toString() {
return this._getPreview();
}
equals(other) {
if (other instanceof RgbColor) {
return super.equals(other);
}
return false;
}
_getPreview() {
if (this.a === 255) {
return `rgb(${this.r},${this.g},${this.b})`;
}
else {
const a = (this.a / 255).toFixed(7);
return `rgba(${this.r},${this.g},${this.b},${a})`;
}
}
_init(colorObject) {
super._init(colorObject);
colorObject.a = colorObject.a != null ? colorObject.a : 255;
this._validateNumber(colorObject.r);
this._validateNumber(colorObject.g);
this._validateNumber(colorObject.b);
this._validateNumber(colorObject.a);
this.r = colorObject.r;
this.g = colorObject.g;
this.b = colorObject.b;
this.a = colorObject.a;
this._updatePreview();
}
_updatePreview() {
this.preview = this._getPreview();
}
}
RgbColor._rgba = /^\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*$/g;
RgbColor._rgb = /^\s*rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)\s*;{0,1}\s*$/g;
//# sourceMappingURL=RgbColor.js.map