Files
mcolor/include/Color4.hpp
2024-12-10 13:26:33 -06:00

194 lines
4.7 KiB
C++

#pragma once
/// Represents a 4-channel color value, with Red, Green, Blue, and Alpha components.
#include <cstdint>
#include <string>
#include <cmath>
#include <Color3.hpp>
#include <stdexcept>
using u8 = uint8_t;
/// A structure representing color in Red, Green, Blue format, 8 bits-per-color, or 24-bit, or True-color.
struct RGB {
u8 r;
u8 g;
u8 b;
};
struct RGBA {
u8 r;
u8 g;
u8 b;
u8 a;
};
struct RGBf {
float r;
float g;
float b;
};
struct RGBAf {
float r;
float g;
float b;
float a;
};
/// A structure representing color in Hue, Chroma, Value format.
struct HSV {
float h;
float s;
float v;
};
struct HSVA {
float h;
float s;
float v;
float a;
};
struct HSL {
float h;
float s;
float l;
};
struct HSLA {
float h;
float s;
float l;
float a;
};
/// A structure representing a Lum, Chroma, Lightness value.
struct LCH {
float l;
float h;
float c;
};
struct LCHA {
float l;
float h;
float c;
float a;
};
struct CMYK {
float c;
float m;
float y;
float k;
};
/// A type representing a color with alpha.
/// Our default format is RGBA with 8 bits-per-channel.
/// Conversions to other color formats are provided.
class Color4 {
public:
u8 r;
u8 g;
u8 b;
u8 a;
public:
/// The default constructor does not initialize any members.
Color4() = default;
/// Constructs a new Color4 from a Color3 and an optional alpha value.
explicit Color4(const Color3& color3, u8 alpha = 255);
/// Constructs a new Color4 from red, green, blue channel values, and an optional alpha value.
Color4(u8 red, u8 green, u8 blue, u8 alpha = 255);
/// Constructs a new Color4 from an RGB structure and an optional alpha value.
explicit Color4(const RGB& rgb, u8 alpha = 255);
/// Constructs a new Color4 from an RGBA structure.
explicit Color4(const RGBA& rgba);
/// Constructs a new Color4 from a floating-point RGB structure and an optional alpha value.
/// @note: Normalizes the color values from ranges [0, 1] to [0, 255].
explicit Color4(const RGBf& rgb, float alpha = 1.0f);
/// Constructs a new Color4 from a floating-point RGBA structure.
/// /// @note: Normalizes the color values from ranges [0, 1] to [0, 255].
explicit Color4(const RGBAf& rgba);
/// Constructs a new Color4 from an HSV structure.
explicit Color4(const HSV& hsv, float alpha = 1.f);
/// Constructs a new Color4 from an HSVA structure.
explicit Color4(const HSVA& hsva);
explicit Color4(const HSL& hsl, float alpha = 1.f);
explicit Color4(const LCH& lch, float alpha = 1.f);
explicit Color4(const LCHA& lcha);
static Color4 FromColor3(const Color3& color3, u8 alpha = 255);
static Color4 FromHex(const std::string& hexCode, u8 alpha = 255);
static Color4 FromHexA(const std::string& hexACode);
static Color4 FromHSV(float hue, float saturation, float value, float alpha = 1.f) {
// TODO: implement
}
static Color4 FromHSL(float hue, float saturation, float lightness, float alpha = 1.f);
static Color4 FromNormalized(float red, float green, float blue, float alpha = 1.f);
static Color4 FromLCH(float l, float c, float h, float alpha = 1.f);
public:
[[nodiscard]] Color4 Lerp(const Color4& rhs, float t) const;
static Color4 Lerp(const Color4& lhs, const Color4& rhs, float t);
/// Alternative to Lerp which does not normalize color channels before and after interpolation.
[[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);
[[nodiscard]] Color4 LerpByHSVA(const Color4& rhs, float t) const;
[[nodiscard]] std::string ToHex() const;
[[nodiscard]] std::string ToHexA() const;
[[nodiscard]] u8 RedChannel() const;
[[nodiscard]] u8 GreenChannel() const;
[[nodiscard]] u8 BlueChannel() const;
[[nodiscard]] u8 AlphaChannel() const;
[[nodiscard]] u8 R() const;
[[nodiscard]] u8 G() const;
[[nodiscard]] u8 B() const;
[[nodiscard]] u8 A() 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;
[[nodiscard]] float BN() const;
[[nodiscard]] float AN() const;
u8* ptr();
[[nodiscard]] const u8* ptr() const;
[[nodiscard]] HSV ToHSV() const;
[[nodiscard]] HSVA ToHSVA() const;
HSL ToHSL() const;
HSLA ToHSLA() const;
LCH ToLCH() const {
}
};