@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
66 lines • 2.95 kB
JavaScript
import * as MathUtils from "./Math";
import { ColorSpace } from "@aurigma/design-atoms-model/Colors";
import { ArgumentException, ArgumentNullException } from "@aurigma/design-atoms-model";
export class ColorUtils {
static getUniqueColors(colors) {
if (colors == null)
return colors;
colors = colors.filter(x => x != null);
const uniqueColors = [];
for (let color of colors) {
if (uniqueColors.findIndex(x => x.equals(color)) == -1)
uniqueColors.push(color);
}
return uniqueColors;
}
static toUriComponent(color) {
if (color == null) {
throw new ArgumentNullException("color");
}
const 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);
}
static _rgbToUriComponent(rgbColor) {
return rgbColor.toString();
}
static _cmykToUriComponent(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)})`);
}
static _grayscaleToUriComponent(grayscaleColor) {
if (grayscaleColor.alpha === 255) {
return `rgb(${grayscaleColor.l},${grayscaleColor.l},${grayscaleColor.l})`;
}
else {
const alpha = ColorUtils._prepareColorComponent(grayscaleColor.alpha);
return `rgba(${grayscaleColor.l},${grayscaleColor.l},${grayscaleColor.l},${alpha})`;
}
}
static _labToUriComponent(labColor) {
return `lab(${labColor.l},${labColor.a},${labColor.b},${ColorUtils._convertByteToPercent(labColor.alpha)}`;
}
static _spotToUriComponent(spotColor) {
const ink = spotColor.ink;
return `spot('${ink.name}',${ColorUtils.toUriComponent(ink.alternativeColor)},${ink.solidity},${ColorUtils._convertByteToPercent(spotColor.tint)})`;
}
static _prepareColorComponent(component) {
return MathUtils.mapToRange(component, 0, 255, 0, 1).toFixed(7);
}
static _convertByteToPercent(value) {
return `${(value / 255 * 100).toFixed(0)}%`;
}
}
ColorUtils._mappedConverters = {
[ColorSpace.Rgb]: ColorUtils._rgbToUriComponent,
[ColorSpace.Cmyk]: ColorUtils._cmykToUriComponent,
[ColorSpace.Grayscale]: ColorUtils._grayscaleToUriComponent,
[ColorSpace.Lab]: ColorUtils._labToUriComponent,
[ColorSpace.Spot]: ColorUtils._spotToUriComponent
};
//# sourceMappingURL=ColorUtils.js.map