@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
96 lines • 4.41 kB
JavaScript
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var _a;
import * as MathUtils from "./Math";
import { ColorSpace } from "@aurigma/design-atoms-model/Colors";
import { ArgumentException, ArgumentNullException } from "@aurigma/design-atoms-model";
var ColorUtils = /** @class */ (function () {
function ColorUtils() {
}
ColorUtils.getUniqueColors = function (colors) {
var e_1, _a;
if (colors == null)
return colors;
colors = colors.filter(function (x) { return x != null; });
var uniqueColors = [];
var _loop_1 = function (color) {
if (uniqueColors.findIndex(function (x) { return x.equals(color); }) == -1)
uniqueColors.push(color);
};
try {
for (var colors_1 = __values(colors), colors_1_1 = colors_1.next(); !colors_1_1.done; colors_1_1 = colors_1.next()) {
var color = colors_1_1.value;
_loop_1(color);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (colors_1_1 && !colors_1_1.done && (_a = colors_1.return)) _a.call(colors_1);
}
finally { if (e_1) throw e_1.error; }
}
return uniqueColors;
};
ColorUtils.toUriComponent = function (color) {
if (color == null) {
throw new ArgumentNullException("color");
}
var convert = ColorUtils._mappedConverters[color.colorSpace];
if (convert == null) {
throw new ArgumentException("Could not convert recieved color, colorSpace " + color.colorSpace + " is not supproted");
}
return convert(color);
};
ColorUtils._rgbToUriComponent = function (rgbColor) {
return rgbColor.toString();
};
ColorUtils._cmykToUriComponent = function (cmykColor) {
return ("cmyk(" + ColorUtils._prepareColorComponent(cmykColor.c) + ",")
.concat(ColorUtils._prepareColorComponent(cmykColor.m) + ",")
.concat(ColorUtils._prepareColorComponent(cmykColor.y) + ",")
.concat(ColorUtils._prepareColorComponent(cmykColor.k) + ",")
.concat(ColorUtils._prepareColorComponent(cmykColor.alpha) + ")");
};
ColorUtils._grayscaleToUriComponent = function (grayscaleColor) {
if (grayscaleColor.alpha === 255) {
return "rgb(" + grayscaleColor.l + "," + grayscaleColor.l + "," + grayscaleColor.l + ")";
}
else {
var alpha = ColorUtils._prepareColorComponent(grayscaleColor.alpha);
return "rgba(" + grayscaleColor.l + "," + grayscaleColor.l + "," + grayscaleColor.l + "," + alpha + ")";
}
};
ColorUtils._labToUriComponent = function (labColor) {
return "lab(" + labColor.l + "," + labColor.a + "," + labColor.b + "," + ColorUtils._convertByteToPercent(labColor.alpha);
};
ColorUtils._spotToUriComponent = function (spotColor) {
var ink = spotColor.ink;
return "spot('" + ink.name + "'," + ColorUtils.toUriComponent(ink.alternativeColor) + "," + ink.solidity + "," + ColorUtils._convertByteToPercent(spotColor.tint) + ")";
};
ColorUtils._prepareColorComponent = function (component) {
return MathUtils.mapToRange(component, 0, 255, 0, 1).toFixed(7);
};
ColorUtils._convertByteToPercent = function (value) {
return (value / 255 * 100).toFixed(0) + "%";
};
ColorUtils._mappedConverters = (_a = {},
_a[ColorSpace.Rgb] = ColorUtils._rgbToUriComponent,
_a[ColorSpace.Cmyk] = ColorUtils._cmykToUriComponent,
_a[ColorSpace.Grayscale] = ColorUtils._grayscaleToUriComponent,
_a[ColorSpace.Lab] = ColorUtils._labToUriComponent,
_a[ColorSpace.Spot] = ColorUtils._spotToUriComponent,
_a);
return ColorUtils;
}());
export { ColorUtils };
//# sourceMappingURL=ColorUtils.js.map