UNPKG

scriptable-testlab

Version:

A lightweight, efficient tool designed to manage and update scripts for Scriptable.

134 lines 3.2 kB
import { AbsColor } from "scriptable-abstract"; const DEFAULT_STATE = { hex: "#000000", red: 0, green: 0, blue: 0, alpha: 1 }; class MockColor extends AbsColor { constructor(hex = "#000000", alpha = 1) { super(DEFAULT_STATE); this.hex = hex; if (alpha !== void 0) { this.alpha = alpha; } } get hex() { const r = Math.round(this.state.red).toString(16).padStart(2, "0"); const g = Math.round(this.state.green).toString(16).padStart(2, "0"); const b = Math.round(this.state.blue).toString(16).padStart(2, "0"); return `#${r}${g}${b}`.toUpperCase(); } set hex(value) { const normalizedHex = value.startsWith("#") ? value : `#${value}`; if (!/^#[0-9A-Fa-f]{6}$/i.test(normalizedHex)) { throw new Error("Invalid hex color format. Expected format: #RRGGBB"); } const r = parseInt(normalizedHex.slice(1, 3), 16); const g = parseInt(normalizedHex.slice(3, 5), 16); const b = parseInt(normalizedHex.slice(5, 7), 16); this.setState({ hex: normalizedHex.toUpperCase(), red: r, green: g, blue: b, alpha: this.alpha }); } get red() { return this.state.red; } set red(value) { if (value < 0 || value > 255) { throw new Error("Red value must be between 0 and 255"); } const hex = this.hex; this.setState({ red: value, hex }); } get green() { return this.state.green; } set green(value) { if (value < 0 || value > 255) { throw new Error("Green value must be between 0 and 255"); } const hex = this.hex; this.setState({ green: value, hex }); } get blue() { return this.state.blue; } set blue(value) { if (value < 0 || value > 255) { throw new Error("Blue value must be between 0 and 255"); } const hex = this.hex; this.setState({ blue: value, hex }); } get alpha() { return this.state.alpha; } set alpha(value) { if (value < 0 || value > 1) { throw new Error("Alpha value must be between 0 and 1"); } const hex = this.hex; this.setState({ alpha: value, hex }); } // Static factory methods static black() { return new MockColor("#000000"); } static darkGray() { return new MockColor("#555555"); } static lightGray() { return new MockColor("#AAAAAA"); } static white() { return new MockColor("#FFFFFF"); } static gray() { return new MockColor("#808080"); } static red() { return new MockColor("#FF0000"); } static green() { return new MockColor("#00FF00"); } static blue() { return new MockColor("#0000FF"); } static cyan() { return new MockColor("#00FFFF"); } static yellow() { return new MockColor("#FFFF00"); } static magenta() { return new MockColor("#FF00FF"); } static orange() { return new MockColor("#FFA500"); } static purple() { return new MockColor("#800080"); } static brown() { return new MockColor("#A52A2A"); } static clear() { const color = new MockColor("#000000"); color.alpha = 0; return color; } static dynamic(dark, _light) { return dark; } } export { MockColor }; //# sourceMappingURL=color.js.map