@runejs/common
Version:
Common logging, networking, compression, and other miscellaneous functionality for RuneJS.
88 lines • 3.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LAB = exports.LABValues = void 0;
const color_1 = require("./color");
const rgb_1 = require("./rgb");
const util_1 = require("../util");
class LABValues extends color_1.Color {
}
exports.LABValues = LABValues;
/**
* A color within the LAB color space.
*/
class LAB extends LABValues {
constructor(arg0, a, b, alpha = 255) {
super('hcl');
let lightness = arg0;
if (a === undefined && b === undefined) {
this.rgb = typeof arg0 === 'number' ? new rgb_1.RGB(arg0) : arg0;
const { l, a: a2, b: b2 } = LAB.fromRgb(this.rgb);
lightness = l;
a = a2;
b = b2;
alpha = this.rgb.alpha;
}
this.l = lightness;
this.a = a;
this.b = b;
this.alpha = alpha !== null && alpha !== void 0 ? alpha : 255;
}
/**
* Converts the given RGB(A) color into the LAB format.
* @param rgb The RGB(A) color to convert into LAB.
*/
static fromRgb(rgb) {
if (rgb.isPureBlack) {
return { l: 0, a: 0, b: 0 };
}
let { r, g, b } = rgb.decimalValues;
r = (r > 0.04045) ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92;
g = (g > 0.04045) ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92;
b = (b > 0.04045) ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92;
let x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047;
let y = (r * 0.2126 + g * 0.7152 + b * 0.0722) / 1.00000;
let z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883;
x = (x > 0.008856) ? Math.pow(x, 1 / 3) : (7.787 * x) + 16 / 116;
y = (y > 0.008856) ? Math.pow(y, 1 / 3) : (7.787 * y) + 16 / 116;
z = (z > 0.008856) ? Math.pow(z, 1 / 3) : (7.787 * z) + 16 / 116;
return {
l: (0, util_1.fixedFloor)((116 * y) - 16, 1),
a: (0, util_1.fixedFloor)(500 * (x - y), 1),
b: (0, util_1.fixedFloor)(200 * (y - z), 1)
};
}
/**
* Checks to see if the given color matches this color.
* @param other The new color to check against the current color.
*/
equals(other) {
return this.l === other.l && this.a === other.a && this.b === other.b;
}
/**
* Calculates the difference between two colors.
* @param other The new color to check against the current color.
*/
difference(other) {
const deltaL = this.l - other.l;
const deltaA = this.a - other.a;
const deltaB = this.b - other.b;
const c1 = Math.sqrt(this.a * this.a + this.b * this.b);
const c2 = Math.sqrt(other.a * other.a + other.b * other.b);
const deltaC = c1 - c2;
let deltaH = deltaA * deltaA + deltaB * deltaB - deltaC * deltaC;
deltaH = deltaH < 0 ? 0 : Math.sqrt(deltaH);
const sc = 1.0 + 0.045 * c1;
const sh = 1.0 + 0.015 * c1;
const deltaLKlsl = deltaL / (1.0);
const deltaCkcsc = deltaC / (sc);
const deltaHkhsh = deltaH / (sh);
const i = deltaLKlsl * deltaLKlsl + deltaCkcsc * deltaCkcsc + deltaHkhsh * deltaHkhsh;
return i < 0 ? 0 : Math.sqrt(i);
}
toString() {
return `LAB(A) ( ${(0, util_1.pad)(this.l, 3)}%, ${(0, util_1.pad)(this.a, 3)}, ` +
`${(0, util_1.pad)(this.b, 3)}, ${(0, util_1.pad)(this.alpha, 3)} )`;
}
}
exports.LAB = LAB;
//# sourceMappingURL=lab.js.map