@runejs/common
Version:
Common logging, networking, compression, and other miscellaneous functionality for RuneJS.
63 lines • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HSL = exports.HSLValues = void 0;
const color_1 = require("./color");
const rgb_1 = require("./rgb");
const util_1 = require("../util");
class HSLValues extends color_1.Color {
}
exports.HSLValues = HSLValues;
/**
* A color within the HSL color space.
*/
class HSL extends HSLValues {
constructor(arg0, saturation, lightness, alpha = 255) {
super('hsl');
let hue = arg0;
if (saturation === undefined && lightness === undefined) {
this.rgb = typeof arg0 === 'number' ? new rgb_1.RGB(arg0) : arg0;
const { h, s, l } = HSL.fromRgb(this.rgb);
hue = h;
saturation = s;
lightness = l;
alpha = this.rgb.alpha;
}
this.h = hue;
this.s = saturation;
this.l = lightness;
this.alpha = alpha !== null && alpha !== void 0 ? alpha : 255;
}
/**
* Converts the given RGB(A) color into the Hue-Saturation-Lightness (HSL) format.
* @param rgb The RGB(A) color to convert into HSL.
*/
static fromRgb(rgb) {
if (rgb.isPureBlack) {
return { h: color_1.constants.black_hue, s: color_1.constants.black_saturation, l: 0 };
}
let { max, min } = rgb;
max /= 255;
min /= 255;
const h = rgb.calculateHue();
const s = rgb.calculateSaturation();
const l = (0, util_1.fixedFloor)(((max + min) / 2) * 100, 0);
if (h === 0 && s === 0 && l > 0) {
// gray/white
// h = 60; ???
}
return color_1.Color.values({ h, s, l });
}
/**
* 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.h === other.h && this.s === other.s && this.l === other.l;
}
toString() {
return `HSL(A) ( ${(0, util_1.pad)(this.h, 3)}, ${(0, util_1.pad)(this.s, 3)}%, ` +
`${(0, util_1.pad)(this.l, 3)}%, ${(0, util_1.pad)(this.alpha, 3)} )`;
}
}
exports.HSL = HSL;
//# sourceMappingURL=hsl.js.map