@felixgeelhaar/govee-api-client
Version:
Enterprise-grade TypeScript client library for the Govee Developer REST API
55 lines • 1.75 kB
JavaScript
export class ColorRgb {
constructor(r, g, b) {
this.validateComponent(r, 'red');
this.validateComponent(g, 'green');
this.validateComponent(b, 'blue');
this._r = Math.round(r);
this._g = Math.round(g);
this._b = Math.round(b);
}
validateComponent(value, component) {
if (!Number.isFinite(value)) {
throw new Error(`RGB ${component} component must be a finite number`);
}
const rounded = Math.round(value);
if (rounded < 0 || rounded > 255) {
throw new Error(`RGB ${component} component must be between 0 and 255, got ${value}`);
}
}
get r() {
return this._r;
}
get g() {
return this._g;
}
get b() {
return this._b;
}
equals(other) {
return this._r === other._r && this._g === other._g && this._b === other._b;
}
toString() {
return `rgb(${this._r}, ${this._g}, ${this._b})`;
}
toHex() {
const toHex = (n) => n.toString(16).padStart(2, '0');
return `#${toHex(this._r)}${toHex(this._g)}${toHex(this._b)}`;
}
toObject() {
return { r: this._r, g: this._g, b: this._b };
}
static fromHex(hex) {
const cleanHex = hex.replace('#', '');
if (!/^[0-9A-Fa-f]{6}$/.test(cleanHex)) {
throw new Error(`Invalid hex color format: ${hex}`);
}
const r = parseInt(cleanHex.substring(0, 2), 16);
const g = parseInt(cleanHex.substring(2, 4), 16);
const b = parseInt(cleanHex.substring(4, 6), 16);
return new ColorRgb(r, g, b);
}
static fromObject(obj) {
return new ColorRgb(obj.r, obj.g, obj.b);
}
}
//# sourceMappingURL=ColorRgb.js.map