UNPKG

@ngageoint/color-js

Version:
545 lines 14.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Color = void 0; const ColorConstants_1 = require("./ColorConstants"); const ColorUtils_1 = require("./ColorUtils"); /** * Color representation with support for hex, RBG, arithmetic RBG, HSL, and * integer colors * * @author osbornb */ class Color { constructor() { /** * Red arithmetic color value */ this.red = 0.0; /** * Green arithmetic color value */ this.green = 0.0; /** * Blue arithmetic color value */ this.blue = 0.0; /** * Opacity arithmetic value */ this.opacity = 1.0; } /** * Create a black color * * @return color */ static black() { return Color.color(ColorConstants_1.ColorConstants.BLACK); } /** * Create a blue color * * @return color */ static blue() { return Color.color(ColorConstants_1.ColorConstants.BLUE); } /** * Create a brown color * * @return color */ static brown() { return Color.color(ColorConstants_1.ColorConstants.BROWN); } /** * Create a cyan color * * @return color */ static cyan() { return Color.color(ColorConstants_1.ColorConstants.CYAN); } /** * Create a dark gray color * * @return color */ static darkGray() { return Color.color(ColorConstants_1.ColorConstants.DKGRAY); } /** * Create a gray color * * @return color */ static gray() { return Color.color(ColorConstants_1.ColorConstants.GRAY); } /** * Create a green color * * @return color */ static green() { return Color.color(ColorConstants_1.ColorConstants.GREEN); } /** * Create a light gray color * * @return color */ static lightGray() { return Color.color(ColorConstants_1.ColorConstants.LTGRAY); } /** * Create a magenta color * * @return color */ static magenta() { return Color.color(ColorConstants_1.ColorConstants.MAGENTA); } /** * Create an orange color * * @return color */ static orange() { return Color.color(ColorConstants_1.ColorConstants.ORANGE); } /** * Create a pink color * * @return color */ static pink() { return Color.color(ColorConstants_1.ColorConstants.PINK); } /** * Create a purple color * * @return color */ static purple() { return Color.color(ColorConstants_1.ColorConstants.PURPLE); } /** * Create a red color * * @return color */ static red() { return Color.color(ColorConstants_1.ColorConstants.RED); } /** * Create a violet color * * @return color */ static violet() { return Color.color(ColorConstants_1.ColorConstants.VIOLET); } /** * Create a white color * * @return color */ static white() { return Color.color(ColorConstants_1.ColorConstants.WHITE); } /** * Create a yellow color * * @return color */ static yellow() { return Color.color(ColorConstants_1.ColorConstants.YELLOW); } /** * Create the color in hex * * @param color * hex color in format #RRGGBB, RRGGBB, #RGB, RGB, #AARRGGBB, * AARRGGBB, #ARGB, or ARGB * @return color */ static color(color) { const newColor = new Color(); newColor.setColor(color); return newColor; } /** * Set the color as a single integer or Set the color in hex * * @param color * color integer or hex color in format #RRGGBB, RRGGBB, #RGB, RGB, #AARRGGBB, * AARRGGBB, #ARGB, or ARGB */ setColor(color) { if (typeof color === 'number') { this.setRed(ColorUtils_1.ColorUtils.getRed(color)); this.setGreen(ColorUtils_1.ColorUtils.getGreen(color)); this.setBlue(ColorUtils_1.ColorUtils.getBlue(color)); if (color > 16777215 || color < 0) { this.setAlpha(ColorUtils_1.ColorUtils.getAlpha(color)); } } else { this.setRed(ColorUtils_1.ColorUtils.getRed(color)); this.setGreen(ColorUtils_1.ColorUtils.getGreen(color)); this.setBlue(ColorUtils_1.ColorUtils.getBlue(color)); const alpha = ColorUtils_1.ColorUtils.getAlpha(color); if (alpha != null) { this.setAlpha(alpha); } } } /** * Set the color with HSLA (hue, saturation, lightness, alpha) values * * @param hue * hue value inclusively between 0.0 and 360.0 * @param saturation * saturation inclusively between 0.0 and 1.0 * @param lightness * lightness inclusively between 0.0 and 1.0 * @param alpha * alpha inclusively between 0.0 and 1.0 */ setColorByHSL(hue, saturation, lightness, alpha) { const arithmeticRGB = ColorUtils_1.ColorUtils.toArithmeticRGBFromHSL(hue, saturation, lightness); this.setRed(arithmeticRGB[0]); this.setGreen(arithmeticRGB[1]); this.setBlue(arithmeticRGB[2]); if (alpha) { this.setAlpha(alpha); } } /** * Set the red color as an integer or hex * * @param red * red integer color inclusively between 0 and 255 or red hex color in format RR or R */ setRed(red) { if (typeof red === 'number') { if (Number.isInteger(red) && red !== 0 && red !== 1) { red = ColorUtils_1.ColorUtils.toHex(red); } } if (typeof red === 'string') { red = ColorUtils_1.ColorUtils.toArithmeticRGB(red); } ColorUtils_1.ColorUtils.validateArithmeticRGB(red); this.red = red; } /** * Set the green color as an integer orr hex * * @param green * green integer color inclusively between 0 and 255 or in format GG or G */ setGreen(green) { if (typeof green === 'number') { if (Number.isInteger(green) && green !== 0 && green !== 1) { green = ColorUtils_1.ColorUtils.toHex(green); } } if (typeof green === 'string') { green = ColorUtils_1.ColorUtils.toArithmeticRGB(green); } ColorUtils_1.ColorUtils.validateArithmeticRGB(green); this.green = green; } /** * Set the blue color as an integer or hex * * @param blue * blue integer color inclusively between 0 and 255 or in format BB or B */ setBlue(blue) { if (typeof blue === 'number') { if (Number.isInteger(blue) && blue !== 0 && blue !== 1) { blue = ColorUtils_1.ColorUtils.toHex(blue); } } if (typeof blue === 'string') { blue = ColorUtils_1.ColorUtils.toArithmeticRGB(blue); } ColorUtils_1.ColorUtils.validateArithmeticRGB(blue); this.blue = blue; } /** * Set the opacity as an arithmetic float * * @param opacity * opacity float color inclusively between 0.0 and 1.0 */ setOpacity(opacity) { ColorUtils_1.ColorUtils.validateArithmeticRGB(opacity); this.opacity = opacity; } /** * Set the alpha color as an arithmetic float or hex * * @param alpha * alpha float color inclusively between 0.0 and 1.0 or hex color in format AA or A */ setAlpha(alpha) { if (typeof alpha === 'string') { alpha = ColorUtils_1.ColorUtils.toArithmeticRGB(alpha); } else if (Number.isInteger(alpha) && alpha !== 0 && alpha !== 1) { alpha = ColorUtils_1.ColorUtils.toArithmeticRGB(alpha); } this.setOpacity(alpha); } /** * Check if the color is opaque (opacity or alpha of 1.0, 255, or x00) * * @return true if opaque */ isOpaque() { return this.opacity === 1.0; } /** * Get the color as a hex string * * @return hex color in the format #RRGGBB */ getColorHex() { return ColorUtils_1.ColorUtils.toColor(this.getRedHex(), this.getGreenHex(), this.getBlueHex()); } /** * Get the color as a hex string with alpha * * @return hex color in the format #AARRGGBB */ getColorHexWithAlpha() { return ColorUtils_1.ColorUtils.toColorWithAlpha(this.getRedHex(), this.getGreenHex(), this.getBlueHex(), this.getAlphaHex()); } /** * Get the color as a hex string, shorthanded when possible * * @return hex color in the format #RGB or #RRGGBB */ getColorHexShorthand() { return ColorUtils_1.ColorUtils.toColorShorthand(this.getRedHex(), this.getGreenHex(), this.getBlueHex()); } /** * Get the color as a hex string with alpha, shorthanded when possible * * @return hex color in the format #ARGB or #AARRGGBB */ getColorHexShorthandWithAlpha() { return ColorUtils_1.ColorUtils.toColorShorthandWithAlpha(this.getRedHex(), this.getGreenHex(), this.getBlueHex(), this.getAlphaHex()); } /** * Get the color as an integer * * @return integer color */ getColor() { return ColorUtils_1.ColorUtils.toColor(this.getRed(), this.getGreen(), this.getBlue()); } /** * Get the color as an integer including the alpha * * @return integer color */ getColorWithAlpha() { return ColorUtils_1.ColorUtils.toColorWithAlpha(this.getRed(), this.getGreen(), this.getBlue(), this.getAlpha()); } /** * Get the red color in hex * * @return red hex color in format RR */ getRedHex() { return ColorUtils_1.ColorUtils.toHex(this.red); } /** * Get the green color in hex * * @return green hex color in format GG */ getGreenHex() { return ColorUtils_1.ColorUtils.toHex(this.green); } /** * Get the blue color in hex * * @return blue hex color in format BB */ getBlueHex() { return ColorUtils_1.ColorUtils.toHex(this.blue); } /** * Get the alpha color in hex * * @return alpha hex color in format AA */ getAlphaHex() { return ColorUtils_1.ColorUtils.toHex(this.opacity); } /** * Get the red color in hex, shorthand when possible * * @return red hex color in format R or RR */ getRedHexShorthand() { return ColorUtils_1.ColorUtils.shorthandHexSingle(this.getRedHex()); } /** * Get the green color in hex, shorthand when possible * * @return green hex color in format G or GG */ getGreenHexShorthand() { return ColorUtils_1.ColorUtils.shorthandHexSingle(this.getGreenHex()); } /** * Get the blue color in hex, shorthand when possible * * @return blue hex color in format B or BB */ getBlueHexShorthand() { return ColorUtils_1.ColorUtils.shorthandHexSingle(this.getBlueHex()); } /** * Get the alpha color in hex, shorthand when possible * * @return alpha hex color in format A or AA */ getAlphaHexShorthand() { return ColorUtils_1.ColorUtils.shorthandHexSingle(this.getAlphaHex()); } /** * Get the red color as an integer * * @return red integer color inclusively between 0 and 255 */ getRed() { return ColorUtils_1.ColorUtils.toRGB(this.red); } /** * Get the green color as an integer * * @return green integer color inclusively between 0 and 255 */ getGreen() { return ColorUtils_1.ColorUtils.toRGB(this.green); } /** * Get the blue color as an integer * * @return blue integer color inclusively between 0 and 255 */ getBlue() { return ColorUtils_1.ColorUtils.toRGB(this.blue); } /** * Get the alpha color as an integer * * @return alpha integer color inclusively between 0 and 255 */ getAlpha() { return ColorUtils_1.ColorUtils.toRGB(this.opacity); } /** * Get the red color as an arithmetic float * * @return red float color inclusively between 0.0 and 1.0 */ getRedArithmetic() { return this.red; } /** * Get the green color as an arithmetic float * * @return green float color inclusively between 0.0 and 1.0 */ getGreenArithmetic() { return this.green; } /** * Get the blue color as an arithmetic float * * @return blue float color inclusively between 0.0 and 1.0 */ getBlueArithmetic() { return this.blue; } /** * Get the opacity as an arithmetic float * * @return opacity float inclusively between 0.0 and 1.0 */ getOpacity() { return this.opacity; } /** * Get the alpha color as an arithmetic float * * @return alpha float color inclusively between 0.0 and 1.0 */ getAlphaArithmetic() { return this.getOpacity(); } /** * Get the HSL (hue, saturation, lightness) values * * @return HSL array where: 0 = hue, 1 = saturation, 2 = lightness */ getHSL() { return ColorUtils_1.ColorUtils.toHSL(this.red, this.green, this.blue); } /** * Get the HSL hue value * * @return hue value */ getHue() { return this.getHSL()[0]; } /** * Get the HSL saturation value * * @return saturation value */ getSaturation() { return this.getHSL()[1]; } /** * Get the HSL lightness value * * @return lightness value */ getLightness() { return this.getHSL()[2]; } /** * Copy the color * * @return color copy */ copy() { const color = new Color(); color.red = this.red; color.green = this.green; color.blue = this.blue; color.opacity = this.opacity; return color; } setRGB(red, green, blue, alpha) { this.setRed(red); this.setGreen(green); this.setBlue(blue); if (alpha) { this.setAlpha(alpha); } } } exports.Color = Color; //# sourceMappingURL=Color.js.map