diff options
author | aidahosa1 <aisosa_idahosa@brown.edu> | 2024-06-04 12:48:41 -0400 |
---|---|---|
committer | aidahosa1 <aisosa_idahosa@brown.edu> | 2024-06-04 12:48:41 -0400 |
commit | 6bfa2fd696a306a984b29c6ea11fa66e93e0474c (patch) | |
tree | 924bbcdad4bbef3e13ea211d267bd6d8ea74964f /src/ClientUtils.ts | |
parent | a7f5bd1c2438f95252f5515d7226b491dfb5183b (diff) |
changes
Diffstat (limited to 'src/ClientUtils.ts')
-rw-r--r-- | src/ClientUtils.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/ClientUtils.ts b/src/ClientUtils.ts index d03ae1486..8e6cd78d0 100644 --- a/src/ClientUtils.ts +++ b/src/ClientUtils.ts @@ -238,6 +238,40 @@ export namespace ClientUtils { return 'rgba(' + col.r + ',' + col.g + ',' + col.b + (col.a !== undefined ? ',' + col.a : '') + ')'; } + export function hexToHsv(hex: string): [number, number, number] { + if (!hex) return [0, 0, 0]; // Default to black if hex is not defined + const r = parseInt(hex.slice(1, 3), 16) / 255; + const g = parseInt(hex.slice(3, 5), 16) / 255; + const b = parseInt(hex.slice(5, 7), 16) / 255; + const max = Math.max(r, g, b), + min = Math.min(r, g, b); + const d = max - min; + let h: number; + const s = max === 0 ? 0 : d / max; + const v = max; + + switch (max) { + case min: + h = 0; + break; + case r: + h = (g - b) / d + (g < b ? 6 : 0); + break; + case g: + h = (b - r) / d + 2; + break; + case b: + h = (r - g) / d + 4; + break; + default: + h = 0; + break; + } + h /= 6; + return [h, s, v]; + }; + + export function HSLtoRGB(h: number, s: number, l: number) { // Must be fractions of 1 // s /= 100; |