@runejs/common
Version:
Common logging, networking, compression, and other miscellaneous functionality for RuneJS.
211 lines • 6.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RGB = exports.RGBValues = void 0;
const color_1 = require("./color");
const util_1 = require("../util");
class RGBValues extends color_1.Color {
}
exports.RGBValues = RGBValues;
/**
* A color within the RGB color space.
*/
class RGB extends RGBValues {
constructor(arg0, green, blue, alpha = 255) {
super('rgb');
let red = arg0;
if (green === undefined && blue === undefined) {
arg0 >>>= 0;
blue = arg0 & 0xFF;
green = (arg0 & 0xFF00) >>> 8;
red = (arg0 & 0xFF0000) >>> 16;
alpha = (arg0 & 0xFF000000) >>> 24;
}
this.r = red !== null && red !== void 0 ? red : 0;
this.g = green !== null && green !== void 0 ? green : 0;
this.b = blue !== null && blue !== void 0 ? blue : 0;
this.alpha = alpha !== null && alpha !== void 0 ? alpha : 255;
}
/**
* Checks to see if the given color matches this color.
* @param other The new color to check against the current color.
*/
equals(other) {
if (this.r !== other.r) {
return false;
}
if (this.g !== other.g) {
return false;
}
return this.b === other.b;
}
/**
* Calculates the difference between two colors.
* @param other The new color to check against the current color.
*/
difference(other) {
const { r: r1, g: g1, b: b1 } = this;
const { r: r2, g: g2, b: b2 } = other;
const drp2 = Math.pow(r1 - r2, 2), dgp2 = Math.pow(g1 - g2, 2), dbp2 = Math.pow(b1 - b2, 2), t = (r1 + r2) / 2;
return Math.sqrt(2 * drp2 + 4 * dgp2 + 3 * dbp2 + t * (drp2 - dbp2) / 256);
}
/**
* Adds the given color to the current one, creating a new color that is a mixture of the two.
* R + R, G + G, B + B. If any value exceeds 255, it will wrap back around to 0 and start over.
* @param other The color to add to the current color.
*/
add(other) {
this.r += other.r;
this.g += other.g;
this.b += other.b;
if (this.r > 255) {
this.r -= 255;
}
if (this.g > 255) {
this.g -= 255;
}
if (this.b > 255) {
this.b -= 255;
}
if (this.r < 0) {
this.r += 255;
}
if (this.g < 0) {
this.g += 255;
}
if (this.b < 0) {
this.b += 255;
}
}
/**
* Calculates the hue value of this RGB(A) color.
*/
calculateHue() {
const { decimalValues: { r, g, b }, max, min } = this;
let h = 0;
if (max !== min) { // achromatic otherwise
const delta = max - min;
switch (max) {
case r:
h = (g - b) / delta + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / delta + 2;
break;
case b:
h = (r - g) / delta + 4;
break;
}
h /= 6;
}
return (0, util_1.fixedFloor)(h * 360, 0);
}
/**
* Calculates the saturation value of this RGB(A) color.
*/
calculateSaturation() {
const { max, min } = this;
if (max === min) {
return 0; // achromatic
}
const delta = max - min;
const s = max === 0 ? 0 : delta / max;
return (0, util_1.fixedFloor)(s * 100, 0);
}
toString() {
return `RGB(A) ( ${(0, util_1.pad)(this.r, 3)}, ${(0, util_1.pad)(this.g, 3)}, ${(0, util_1.pad)(this.b, 3)}, ` +
`${(0, util_1.pad)(this.alpha, 3)})`;
}
/**
* The minimum value found between R, G, and B.
*/
get min() {
return Math.min(Math.min(this.r, this.g), this.b);
}
/**
* The maximum value found between R, G, and B.
*/
get max() {
return Math.max(Math.max(this.r, this.g), this.b);
}
/**
* The RGBA integer representation of this color.
* Differing from `get argb()`, this variation places the alpha value at the end of the int instead of the front.<br>
* `int[red << 24, green << 16, blue << 8, alpha]`
*/
get rgba() {
return (this.r << 24) + (this.g << 16) + (this.b << 8) + this.alpha;
}
/**
* The ARGB integer representation of this color.
* Differing from `get rgba()`, this variation places the alpha value at the front of the int instead of the end.<br>
* `int[alpha << 24, red << 16, green << 8, blue]`
*/
get argb() {
return (this.alpha << 24) + (this.r << 16) + (this.g << 8) + (this.b);
}
/**
* The decimal representations of R, G, and B within this color.<br>
* `R:G:B / 255`
*/
get decimalValues() {
return this.values({
r: this.r / 255,
g: this.g / 255,
b: this.b / 255
});
}
/**
* The percentage representations of R, G, and B within this color.<br>
* `R:G:B / 255 * 100`
*/
get percentValues() {
const r = Math.floor(this.r / 255 * 100);
const g = Math.floor(this.g / 255 * 100);
const b = Math.floor(this.b / 255 * 100);
return this.values({
r, g, b
});
}
/**
* Average of the R+G+B values within this color.<br>
* `(this.r + this.g + this.b) / 3`
*/
get value() {
return Math.round((this.r + this.g + this.b) / 3);
}
/**
* Average of the R+G+B percentages within this color.<br>
* `(percentR + percentG + percentB) / 3`
*/
get intensity() {
const { r, g, b } = this.percentValues;
return Math.round((r + g + b) / 3);
}
/**
* The total value of R+G+B within this color.
*/
get total() {
return this.r + this.g + this.b;
}
/**
* The color's luminosity.
*/
get luminance() {
return ((.2126 * this.r) + (.7152 * this.g) + (.0722 * this.b)) / 255;
}
/**
* The color's grayscale rating.
*/
get grayscale() {
return Math.abs(Math.max(this.r, this.g) - this.b);
}
/**
* Whether or not the color is pure non-transparent black, hex `#000000`.
*/
get isPureBlack() {
// RS stores black as RGB 0,0,1 so transparent can be 0,0,0
return this.r === 0 && this.g === 0 && this.b <= 1;
}
}
exports.RGB = RGB;
//# sourceMappingURL=rgb.js.map