diff options
Diffstat (limited to 'scripts/gui-common/color.js')
-rw-r--r-- | scripts/gui-common/color.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/scripts/gui-common/color.js b/scripts/gui-common/color.js index 90eac14..0b25f68 100644 --- a/scripts/gui-common/color.js +++ b/scripts/gui-common/color.js @@ -52,8 +52,61 @@ class Color { return new Color(r / 255, g / 255, b / 255, a); } + + static d(h) + { + let d = Math.floor((h - (h % PI_THIRDS)) / PI_THIRDS); + return (DIVS.length - 1 + d) % (DIVS.length - 1); + } + + static from_hsv(h, s, v) + { + h = (((h % TWO_PI)) + TWO_PI) % TWO_PI; + + let d = Color.d(h); + + h = (h % PI_THIRDS) / PI_THIRDS; + + let out = DIVS[d].interpolate(DIVS[d + 1], h); + out = WHITE.interpolate(out, s); + + return BLACK.interpolate(out, v); + } + + static from_hsl(h, s, l) + { + h = (((h % TWO_PI)) + TWO_PI) % TWO_PI; + + let d = Color.d(h); + + h = (h % PI_THIRDS) / PI_THIRDS; + + let out = DIVS[d].interpolate(DIVS[d + 1], h); + let L = BLACK.interpolate(WHITE, l); + + if (l < 0.5) + out = BLACK.interpolate(out, l / 0.5); + else + out = out.interpolate(WHITE, (l - 0.5) / 0.5); + + return L.interpolate(out, s); + } } +const RED = new Color(1, 0, 0, 1); +const YELLOW = new Color(1, 1, 0, 1); +const GREEN = new Color(0, 1, 0, 1); +const CYAN = new Color(0, 1, 1, 1); +const BLUE = new Color(0, 0, 1, 1); +const MAGENTA = new Color(1, 0, 1, 1); +const WHITE = new Color(1, 1, 1, 1); +const BLACK = new Color(0, 0, 0, 1); + +const PI_THIRDS = Math.PI / 3; +const TWO_PI = Math.PI * 2; + +const DIVS = [RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA, RED]; + /** * Interpolate between two colors * @param {Color} a |