Codeworks on vacahy

This commit is contained in:
2024-12-10 13:26:33 -06:00
parent bb157ff855
commit c2692a27ae
2 changed files with 64 additions and 29 deletions

View File

@@ -147,29 +147,29 @@ public:
static Color4 Lerp(const Color4& lhs, const Color4& rhs, float t); static Color4 Lerp(const Color4& lhs, const Color4& rhs, float t);
/// Alternative to Lerp which does not normalize color channels before and after interpolation. /// 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. /// Alternative to Lerp which does not normalize color channels before and after interpolation.
static Color4 Lerp2(const Color4& lhs, const Color4& rhs, float t); 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; [[nodiscard]] std::string ToHex() const;
std::string ToHexA() const; [[nodiscard]] std::string ToHexA() const;
u8 RedChannel() const; [[nodiscard]] u8 RedChannel() const;
u8 GreenChannel() const; [[nodiscard]] u8 GreenChannel() const;
u8 BlueChannel() const; [[nodiscard]] u8 BlueChannel() const;
u8 AlphaChannel() const; [[nodiscard]] u8 AlphaChannel() const;
u8 R() const; [[nodiscard]] u8 R() const;
u8 G() const; [[nodiscard]] u8 G() const;
u8 B() const; [[nodiscard]] u8 B() const;
u8 A() const; [[nodiscard]] u8 A() const;
float RedChannelNormalized() const; [[nodiscard]] float RedChannelNormalized() const;
float GreenChannelNormalized() const; [[nodiscard]] float GreenChannelNormalized() const;
float BlueChannelNormalized() const; [[nodiscard]] float BlueChannelNormalized() const;
float AlphaChannelNormalized() const; [[nodiscard]] float AlphaChannelNormalized() const;
[[nodiscard]] float RN() const; [[nodiscard]] float RN() const;
[[nodiscard]] float GN() const; [[nodiscard]] float GN() const;
@@ -179,14 +179,12 @@ public:
u8* ptr(); u8* ptr();
[[nodiscard]] const u8* ptr() const; [[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 { LCH ToLCH() const {

View File

@@ -120,13 +120,6 @@ float Color4::BN() const { return BlueChannelNormalized(); }
float Color4::AN() const { return AlphaChannelNormalized(); } 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) { Color4 Color4::FromNormalized(float red, float green, float blue, float alpha) {
return { return {
static_cast<u8>(red * 255), static_cast<u8>(red * 255),
@@ -234,3 +227,47 @@ HSVA Color4::ToHSVA() const {
auto hsv = ToHSV(); auto hsv = ToHSV();
return {hsv.h, hsv.s, hsv.v, AN()}; 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()};
}