diff options
author | Alec Murphy <alec@noreply.localhost> | 2021-06-26 18:01:51 +0000 |
---|---|---|
committer | Alec Murphy <alec@noreply.localhost> | 2021-06-26 18:01:51 +0000 |
commit | 87aabe3b1adf37e95e715312eab4f531cba7ece9 (patch) | |
tree | d3ded354caeed41d0ceb8c75b0a8e97c2739ef0f | |
parent | 1e9a240b803c5ebbd19783f927aed8b37ee90e72 (diff) | |
parent | af3d2155d68ff64cf7fea1ff3155c27310478c88 (diff) |
Merge pull request 'Libraries/Graphics2D: Added RGB to HSV and HSV to RGB conversion' (#9) from GasInfinity/erythros:master into master
Reviewed-on: https://git.checksum.fail/alec/erythros/pulls/9
-rw-r--r-- | Boot/Libraries/Graphics2D.HC | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/Boot/Libraries/Graphics2D.HC b/Boot/Libraries/Graphics2D.HC index 000332a..446ff26 100644 --- a/Boot/Libraries/Graphics2D.HC +++ b/Boot/Libraries/Graphics2D.HC @@ -21,6 +21,87 @@ U32 Color(I64 r, I64 g, I64 b, I64 a = 255) { return c; } +U32 ColorHSVToRGB(I64 hue, I64 saturation, I64 value) { + if(!(0 <= hue < 360)) + hue = 0; + if(!(0 <= saturation <= 100)) + saturation = 100; + if(!(0 <= value <= 100)) + value = 100; + + F64 s = saturation / 100.0; + F64 v = value / 100.0; + + F64 c = v * s; + F64 x = c * (1 - Abs(((hue / 60.0) % 2) - 1)); + F64 m = v - c; + + F64 r, g, b; + switch(hue) { + case 0...59: + r = c; + g = x; + b = 0; + break; + case 60...119: + r = x; + g = c; + b = 0; + break; + case 120...179: + r = 0; + g = c; + b = x; + break; + case 180...239: + r = 0; + g = x; + b = c; + break; + case 240...299: + r = x; + g = 0; + b = c; + break; + case 300...359: + r = c; + g = 0; + b = x; + break; + } + + return Color(ToI64((r + m) * 255), ToI64((g + m) * 255), ToI64((b + m) * 255)); +} + +U0 ColorRGBToHSV(U32 color, I64 *hue, I64 *saturation, I64 *value) { + if(!hue || !saturation || !value) + return; + + F64 r = color.u8[2] / 255.0; + F64 g = color.u8[1] / 255.0; + F64 b = color.u8[0] / 255.0; + + F64 cMax = Max(r, Max(g, b)); + F64 cMin = Min(r, Min(g, b)); + + F64 delta = cMax - cMin; + + if(delta == 0) + *hue = 0; + else if(cMax == r) + *hue = 60 * (((g - b) / delta) % 6); + else if(cMax == g) + *hue = 60 * (((b - r) / delta) + 2); + else + *hue = 60 * (((r - g) / delta) + 4); + + if(cMax != 0) + *saturation = (delta / cMax) * 100; + else + *saturation = 0; + *value = cMax * 100; +} + /* * ISO Latin-1 Font * |