Modules/Math/public/Color.hpp
2024-10-11 14:44:56 +03:00

109 lines
1.7 KiB
C++

#pragma once
#include "Vec.hpp"
namespace tp {
class RGB;
class HSV;
class RGB {
public:
RGB();
RGB(flt4 pr, flt4 pg, flt4 pb);
RGB(flt4 val);
public:
void set(flt4 pr, flt4 pg, flt4 pb);
operator HSV() const;
public:
halnf r, g, b;
};
class HSV {
public:
HSV();
HSV(flt4 ph, flt4 ps, flt4 pv);
public:
void set(flt4 ph, flt4 ps, flt4 pv);
operator RGB() const;
public:
halnf h, s, v;
};
class RGBA {
public:
RGBA() :
r(0),
g(0),
b(0),
a(0) {}
RGBA(flt4 val) :
rgbs(val),
a(val) {}
RGBA(const RGB& RGBs, flt4 val) :
rgbs(RGBs),
a(val) {}
RGBA(flt4 pr, flt4 pg, flt4 pb, flt4 pa) :
rgbs(pr, pg, pb),
a(pa) {}
public:
RGBA& operator=(const HSV& in) {
rgbs = in;
a = 1;
return *this;
}
RGBA operator-(const RGBA& in) const {
const auto nr = tp::clamp(r - in.r, 0.f, 1.f);
const auto ng = tp::clamp(g - in.g, 0.f, 1.f);
const auto nb = tp::clamp(b - in.b, 0.f, 1.f);
const auto na = tp::clamp(a - in.a, 0.f, 1.f);
return { nr, ng, nb, na };
}
RGBA operator+(const RGBA& in) const {
const auto nr = tp::clamp(r + in.r, 0.f, 1.f);
const auto ng = tp::clamp(g + in.g, 0.f, 1.f);
const auto nb = tp::clamp(b + in.b, 0.f, 1.f);
const auto na = tp::clamp(a + in.a, 0.f, 1.f);
return { nr, ng, nb, na };
}
bool operator==(const RGBA& in) const { return r == in.r && g == in.g && b == in.b && a == in.a; }
static RGBA random() {
return RGBA{
(halnf) randomFloat(),
(halnf) randomFloat(),
(halnf) randomFloat(),
1.f,
};
}
public:
union {
RGB rgbs;
struct {
flt4 r;
flt4 g;
flt4 b;
};
};
flt4 a;
};
class HSVA {
HSV rgbs;
flt4 a = 1.f;
};
}