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