Added ptr() that can be passed to glColorNubv

This commit is contained in:
2024-08-05 00:16:30 -04:00
parent a74d1ac737
commit 3004690fe0
5 changed files with 67 additions and 12 deletions

View File

@@ -8,19 +8,20 @@ using u8 = uint8_t;
/// Represents a 3-channel color value, with Red, Green and Blue components.
class Color3 {
public:
u8 r;
u8 g;
u8 b;
u8 r; // Red value 0-255
u8 g; // Green value 0-255
u8 b; // Blue value 0-255
public:
/// Explicitly constructs a Color3 from the given Red, Green, and Blue values.
Color3(u8 R, u8 G, u8 B);
/// Returns a Color3 parsed from the given hexadecimal string.
static Color3 FromHex(const std::string& hexCode);
static Color3 FromHSL(u8 hue, u8 saturation, u8 lightness);
Color3() = default;
public:
/// Returns a Color3 that is somewhere in-between this and the given Color3, determined by the alpha [0-1].
Color3 Lerp(const Color3& rhs, float alpha) const;
Color3 Lerp(const Color3& rhs, float t) const;
/// Returns the red channel [0-255].
u8 RedChannel () const;
@@ -35,4 +36,7 @@ public:
/// Returns the blue channel normalized from [0-255] to [0-1].
float BlueChannelNormalized() const;
u8* ptr();
[[nodiscard]] const u8* ptr() const;
};

View File

@@ -7,10 +7,8 @@
#include <Color3.hpp>
using u8 = uint8_t;
class Color4 {
public:
u8 r;
@@ -23,13 +21,23 @@ public:
Color4(u8 red, u8 green, u8 blue, u8 alpha = 255);
static Color4 FromColor3(const Color3& color3, u8 alpha = 255);
static Color4 FromHex(const std::string& hexCode, u8 alpha = 255);
static Color4 FromHSL(u8 hue, u8 saturation, u8 lightness, u8 alpha = 255);
static Color4 FromNormalized(float red, float green, float blue, float alpha);
public:
[[nodiscard]] Color4 Lerp(const Color4& rhs, float t) const;
static Color4 Lerp(const Color4& lhs, const Color4& rhs, float t);
u8 RedChannel() const;
u8 GreenChannel() const;
u8 BlueChannel() const;
u8 AlphaChannel() const;
u8 R() const { return AlphaChannel(); }
u8 G() const { return GreenChannel(); }
u8 B() const { return BlueChannel(); }
u8 A() const { return AlphaChannel(); }
float RedChannelNormalized() const;
float GreenChannelNormalized() const;
float BlueChannelNormalized() const;
@@ -38,8 +46,8 @@ public:
[[nodiscard]] float RN() const { return RedChannelNormalized(); }
[[nodiscard]] float GN() const { return GreenChannelNormalized(); }
[[nodiscard]] float BN() const { return BlueChannelNormalized(); }
[[nodiscard]] float AN() const { return AlphaChannelNormalized(); }
int* ptr() { return reinterpret_cast<int *>(&r);}
[[nodiscard]] const int* ptr() const { return reinterpret_cast<const int *>(&r);}
u8* ptr();
[[nodiscard]] const u8* ptr() const;
};

View File

@@ -2,6 +2,7 @@
#include <vector>
#include <functional>
#include <mcolor.h>
#include <Colors.hpp>
int main()
{

View File

@@ -18,4 +18,8 @@ Color3 Color3::FromHex(const std::string &hexCode) {
u8 r, g, b;
std::sscanf(hexCode.c_str(), "#%02x%02x%02x", &r, &g, &b);
return {r, g, b};
}
}
u8 *Color3::ptr() { return &r;}
const u8 *Color3::ptr() const { return &r;}

View File

@@ -25,4 +25,42 @@ Color4 Color4::FromHex(const std::string &hexCode, u8 alpha) {
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<u8>(red * 255),
static_cast<u8>(green * 255),
static_cast<u8>(blue * 255),
static_cast<u8>(alpha * 255)
};
}
Color4 Color4::Lerp(const Color4 &rhs, float t) const {
/// Performs the interpolation in normalized color space (0-1) and converts it back.
/// Trust me.
float rn = RN();
float rhs_rn = rhs.RN();
float bn = BN();
float rhs_bn = rhs.BN();
float gn = GN();
float rhs_gn = rhs.GN();
float an = AN();
float rhs_an = rhs.AN();
return FromNormalized(
rn + (rhs_rn - rn) * t,
gn + (rhs_gn - gn) * t,
bn + (rhs_bn - bn) * t,
an + (rhs_an - an) * t
);
}
Color4 Color4::Lerp(const Color4 &lhs, const Color4 &rhs, float t) {
return lhs.Lerp(rhs, t);
}
u8 *Color4::ptr() { return (&r);}
const u8 *Color4::ptr() const { return (&r);}