diff options
author | Kyle Gunger <kgunger12@gmail.com> | 2024-11-12 23:49:21 -0500 |
---|---|---|
committer | Kyle Gunger <kgunger12@gmail.com> | 2024-11-12 23:49:21 -0500 |
commit | 53c95ab94cab5163424646d4a798a7ea7fb13ec7 (patch) | |
tree | baa4551d161e1f074bc5579f4c9c6b061eddd084 /scripts/gui-common/color.js | |
parent | 4d93bd73f0a56974bd55db8f9e8ff3f318be195d (diff) |
Toggle, Checkbox, and Slider widgets
Diffstat (limited to 'scripts/gui-common/color.js')
-rw-r--r-- | scripts/gui-common/color.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/scripts/gui-common/color.js b/scripts/gui-common/color.js new file mode 100644 index 0000000..90eac14 --- /dev/null +++ b/scripts/gui-common/color.js @@ -0,0 +1,66 @@ +class Color +{ + /** @type {Array<number>} */ + channels = [] + + /** + * Construct a color + * @param {...number} nums + */ + constructor(...nums) + { + this.channels = nums; + } + + /** + * @param {Color} b + * @param {number} i + * @returns {Color} + */ + interpolate(b, i) + { + let out = new Color(); + for (let c = 0; c < this.channels.length && c < b.channels.length; c++) + { + out.channels.push(b.channels[c] * i + this.channels[c] * (1 - i)) + } + return out; + } + + /** Get the CSS string representing rgb + * @returns {string} + */ + rgb() + { + return `rgb(${Math.trunc(this.channels[0] * 255)}, ${Math.trunc(this.channels[1] * 255)}, ${Math.trunc(this.channels[2] * 255)})`; + } + + /** Get the CSS string representing rgba + * @returns {string} + */ + rgba() + { + return `rgba(${Math.trunc(this.channels[0] * 255)}, ${Math.trunc(this.channels[1] * 255)}, ${Math.trunc(this.channels[2] * 255)}, ${this.channels[3]})`; + } + + static from_rgb(r, g, b) + { + return new Color(r / 255, g / 255, b / 255); + } + + static from_rgba(r, g, b) + { + return new Color(r / 255, g / 255, b / 255, a); + } +} + +/** + * Interpolate between two colors + * @param {Color} a + * @param {Color} b + * @param {number} p + */ +function interpolate(a, b, p) +{ + +}
\ No newline at end of file |