1
0
Fork 0
2018-soft-3d-renderer/Source/Color.hpp

117 lines
1.5 KiB
C++

#pragma once
#include <algorithm>
#include <cstdint>
class ColorU32
{
public:
ColorU32()
: b(0), g(0), r(0), a(0)
{}
ColorU32(uint8_t b, uint8_t g, uint8_t r, uint8_t a)
: b(b), g(g), r(r), a(a)
{}
union
{
struct
{
uint8_t b, g, r, a;
};
uint32_t u32;
};
};
class ColorF32
{
public:
ColorF32()
: b(0.0f), g(0.0f), r(0.0f), a(0.0f)
{}
ColorF32(float b, float g, float r, float a)
: b(b), g(g), r(r), a(a)
{}
void Scale()
{
float blue = std::max(b, 0.0f);
float green = std::max(g, 0.0f);
float red = std::max(r, 0.0f);
float alpha = std::max(a, 0.0f);
float max = std::max(std::max(std::max(blue, green), red), 1.0f);
ColorF32 scaled(blue, green, red, alpha);
scaled /= max;
b = scaled.b;
g = scaled.g;
r = scaled.r;
a = scaled.a;
}
float b, g, r, a;
ColorF32 operator+(ColorF32 const& rhs) const
{
return ColorF32(
b + rhs.b,
g + rhs.g,
r + rhs.r,
a + rhs.a);
}
ColorF32& operator+=(ColorF32 const& rhs)
{
b += rhs.b;
g += rhs.g;
r += rhs.r;
a += rhs.a;
return *this;
}
ColorF32 operator*(float f) const
{
return ColorF32(
b * f,
g * f,
r * f,
a * f);
}
ColorF32 operator*(ColorF32 const& rhs) const
{
return ColorF32(
b * rhs.b,
g * rhs.g,
r * rhs.r,
a * rhs.a);
}
ColorF32 operator/(float f) const
{
float fInv = 1.0f / f;
return ColorF32(
b * fInv,
g * fInv,
r * fInv,
a * fInv);
}
ColorF32& operator/=(float f)
{
b /= f;
g /= f;
r /= f;
a /= f;
return *this;
}
};