viridis
Version:
Color gradients for data visualization
102 lines (101 loc) • 3.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Color = void 0;
var SMath = require("smath");
/**
* Structure for storing colors based on RGBa values.
*/
var Color = /** @class */ (function () {
/**
* Define a new color from RGBa values.
* @param red Red channel intensity [0, 255]
* @param green Green channel intensity [0, 255]
* @param blue Blue channel intensity [0, 255]
* @param alpha Alpha channel transparency [0, 100]
* @example
* ```js
* const red = new Color(255, 0, 0);
* ```
*/
function Color(red, green, blue, alpha) {
if (alpha === void 0) { alpha = 100; }
this.red = red;
this.green = green;
this.blue = blue;
this.alpha = alpha;
this.red = SMath.clamp(red, 0, 255) | 0;
this.green = SMath.clamp(green, 0, 255) | 0;
this.blue = SMath.clamp(blue, 0, 255) | 0;
this.alpha = SMath.clamp(alpha, 0, 100) | 0;
}
/**
* Return the most contrasting color for
* text on a background of this color.
* @returns Black or white
* @example
* ```js
* const contrast = red.getContrastingColor();
* // Returns the color white (255, 255, 255)
* ```
*/
Color.prototype.getContrastingColor = function () {
if (this.red + this.green * 1.5 + this.blue * 0.5 > 255 * 1.5) {
return new Color(0, 0, 0);
}
else {
return new Color(255, 255, 255);
}
};
/**
* Return a string representation of this color.
* @returns A valid CSS color code
* @example
* ```js
* const css = red.toString(); // rgba(255,0,0,100%)
* ```
*/
Color.prototype.toString = function (type) {
if (type === void 0) { type = 'rgba'; }
switch (type) {
case 'rgb': {
return 'rgb(' + this.red + ',' + this.green + ',' + this.blue + ')';
}
case 'rgba': {
return 'rgba(' + this.red + ',' + this.green + ',' + this.blue + ',' + this.alpha + '%)';
}
case 'hex': {
return '#' + SMath.toHex(this.red, 2) + SMath.toHex(this.green, 2) + SMath.toHex(this.blue, 2);
}
case 'hex-transparency': {
var alpha255 = SMath.clamp(SMath.round2(SMath.translate(this.alpha, 0, 100, 0, 255), 1), 0, 255);
return this.toString('hex') + SMath.toHex(alpha255, 2);
}
default: {
throw new Error('Invalid color type: ' + type);
}
}
};
/**
* Create a new color given a hexadecimal string.
* Will throw an error if the string is invalid.
* - Expects 2 bits [0-F] for red, green, blue channels (6 chars total)
* - String can optionally start with the character '#'
* - Alpha channel can be included for an additional 2 bits (8 chars total)
* @param hex Hexadecimal string
* @returns A new color defined by the hexadecimal string
* @example
* ```js
* const red = Color.from('#ff0000');
* ```
*/
Color.from = function (hex) {
var _a;
var regex = /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})?$/.exec(hex);
if (regex === null) {
throw new Error('Invalid hexadecimal string: ' + hex);
}
return new Color(parseInt(regex[1], 16), parseInt(regex[2], 16), parseInt(regex[3], 16), SMath.translate(parseInt((_a = regex[4]) !== null && _a !== void 0 ? _a : 'FF', 16), 0, 255, 0, 100));
};
return Color;
}());
exports.Color = Color;