diff --git a/include/Color4.hpp b/include/Color4.hpp index e1a0a0d..aadf6f9 100644 --- a/include/Color4.hpp +++ b/include/Color4.hpp @@ -147,29 +147,29 @@ public: static Color4 Lerp(const Color4& lhs, const Color4& rhs, float t); /// Alternative to Lerp which does not normalize color channels before and after interpolation. - Color4 Lerp2(const Color4& rhs, float t) const; + [[nodiscard]] Color4 Lerp2(const Color4& rhs, float t) const; /// Alternative to Lerp which does not normalize color channels before and after interpolation. static Color4 Lerp2(const Color4& lhs, const Color4& rhs, float t); - Color4 LerpByHSVA(const Color4& rhs, float t) const; + [[nodiscard]] Color4 LerpByHSVA(const Color4& rhs, float t) const; - std::string ToHex() const; - std::string ToHexA() const; + [[nodiscard]] std::string ToHex() const; + [[nodiscard]] std::string ToHexA() const; - u8 RedChannel() const; - u8 GreenChannel() const; - u8 BlueChannel() const; - u8 AlphaChannel() const; + [[nodiscard]] u8 RedChannel() const; + [[nodiscard]] u8 GreenChannel() const; + [[nodiscard]] u8 BlueChannel() const; + [[nodiscard]] u8 AlphaChannel() const; - u8 R() const; - u8 G() const; - u8 B() const; - u8 A() const; + [[nodiscard]] u8 R() const; + [[nodiscard]] u8 G() const; + [[nodiscard]] u8 B() const; + [[nodiscard]] u8 A() const; - float RedChannelNormalized() const; - float GreenChannelNormalized() const; - float BlueChannelNormalized() const; - float AlphaChannelNormalized() const; + [[nodiscard]] float RedChannelNormalized() const; + [[nodiscard]] float GreenChannelNormalized() const; + [[nodiscard]] float BlueChannelNormalized() const; + [[nodiscard]] float AlphaChannelNormalized() const; [[nodiscard]] float RN() const; [[nodiscard]] float GN() const; @@ -179,14 +179,12 @@ public: u8* ptr(); [[nodiscard]] const u8* ptr() const; - HSV ToHSV() const; + [[nodiscard]] HSV ToHSV() const; - HSVA ToHSVA() const; + [[nodiscard]] HSVA ToHSVA() const; - HSL ToHSL() const - { - - } + HSL ToHSL() const; + HSLA ToHSLA() const; LCH ToLCH() const { diff --git a/src/Color4.cpp b/src/Color4.cpp index 27c4e99..2bec550 100644 --- a/src/Color4.cpp +++ b/src/Color4.cpp @@ -120,13 +120,6 @@ float Color4::BN() const { return BlueChannelNormalized(); } float Color4::AN() const { return AlphaChannelNormalized(); } -Color4 Color4::FromHex(const std::string &hexCode, u8 alpha) { - // TODO: Support alpha in hex code. - u8 r, g, b; - std::sscanf(hexCode.c_str(), "#%02x%02x%02x", &r, &g, &b); - return {r, g, b, alpha}; -} - Color4 Color4::FromNormalized(float red, float green, float blue, float alpha) { return { static_cast(red * 255), @@ -234,3 +227,47 @@ HSVA Color4::ToHSVA() const { auto hsv = ToHSV(); return {hsv.h, hsv.s, hsv.v, AN()}; } + +HSL Color4::ToHSL() const +{ + float rn = RN(); + float gn = GN(); + float bn = BN(); + + float max = std::max(std::max(rn, gn), bn); + float min = std::min(std::min(rn, gn), bn); + + float delta = max - min; + + float L = (min + max) / 2.f; + float S = 0; + + if (max == min) + S = 0; + if (L > 0.5f) + S = (delta) * (2.f - max - min); + if (L <= 0.5f) + S = (max - min) / (max + min); + + float H = 0; + + if (rn == max) + H = (gn - bn) / (delta); + if (gn == max) + H = 2.f + (bn-rn) / (max-min); + if (bn == max) + H = 4.f + (rn-gn) / (max-min); + + H *= 60; + + if (H < 0.f) + H += 360.f; + + return {H,S,L}; +} + +HSLA Color4::ToHSLA() const +{ + HSL hsl = ToHSL(); + return {hsl.h, hsl.s, hsl.l, AN()}; +}