Skip to content

Commit

Permalink
Color cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
afritz1 committed Jan 1, 2025
1 parent 3acd25e commit 00ef1b0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 57 deletions.
66 changes: 20 additions & 46 deletions OpenTESArena/src/Utilities/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,21 @@ const Color Color::White(255, 255, 255, 255);
const Color Color::Gray(127, 127, 127, 255);
const Color Color::Transparent(0, 0, 0, 0);

Color::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
this->r = r;
this->g = g;
this->b = b;
this->a = a;
}

Color::Color(uint8_t r, uint8_t g, uint8_t b)
: Color(r, g, b, 255) { }

Color::Color()
: Color(0, 0, 0) { }

Color Color::randomRGBA(Random &random)
{
uint8_t r = static_cast<uint8_t>(random.next(256));
uint8_t g = static_cast<uint8_t>(random.next(256));
uint8_t b = static_cast<uint8_t>(random.next(256));
uint8_t a = static_cast<uint8_t>(random.next(256));
return Color(r, g, b, a);
return Color(
static_cast<uint8_t>(random.next(256)),
static_cast<uint8_t>(random.next(256)),
static_cast<uint8_t>(random.next(256)),
static_cast<uint8_t>(random.next(256)));
}

Color Color::randomRGB(Random &random)
{
uint8_t r = static_cast<uint8_t>(random.next(256));
uint8_t g = static_cast<uint8_t>(random.next(256));
uint8_t b = static_cast<uint8_t>(random.next(256));
return Color(r, g, b);
return Color(
static_cast<uint8_t>(random.next(256)),
static_cast<uint8_t>(random.next(256)),
static_cast<uint8_t>(random.next(256)));
}

Color Color::fromARGB(uint32_t argb)
Expand Down Expand Up @@ -80,42 +66,37 @@ std::string Color::toString() const

uint32_t Color::toARGB() const
{
return static_cast<uint32_t>(
(this->r << 16) | (this->g << 8) | (this->b) | (this->a << 24));
return static_cast<uint32_t>((this->r << 16) | (this->g << 8) | (this->b) | (this->a << 24));
}

uint32_t Color::toRGBA() const
{
return static_cast<uint32_t>(
(this->r << 24) | (this->g << 16) | (this->b << 8) | (this->a));
return static_cast<uint32_t>((this->r << 24) | (this->g << 16) | (this->b << 8) | (this->a));
}

uint32_t Color::toRGB() const
{
return static_cast<uint32_t>(
(this->r << 16) | (this->g << 8) | (this->b));
return static_cast<uint32_t>((this->r << 16) | (this->g << 8) | (this->b));
}

Color Color::operator+(const Color &c) const
Color Color::operator+(const Color &other) const
{
return Color(this->r + c.r, this->g + c.g, this->b + c.b, this->a + c.a);
return Color(this->r + other.r, this->g + other.g, this->b + other.b, this->a + other.a);
}

Color Color::operator-(const Color &c) const
Color Color::operator-(const Color &other) const
{
return Color(this->r - c.r, this->g - c.g, this->b - c.b, this->a - c.a);
return Color(this->r - other.r, this->g - other.g, this->b - other.b, this->a - other.a);
}

bool Color::operator==(const Color &c) const
bool Color::operator==(const Color &other) const
{
return (this->r == c.r) && (this->g == c.g) && (this->b == c.b) &&
(this->a == c.a);
return (this->r == other.r) && (this->g == other.g) && (this->b == other.b) && (this->a == other.a);
}

bool Color::operator!=(const Color &c) const
bool Color::operator!=(const Color &other) const
{
return (this->r != c.r) || (this->g != c.g) || (this->b != c.b) ||
(this->a != c.a);
return (this->r != other.r) || (this->g != other.g) || (this->b != other.b) || (this->a != other.a);
}

Color Color::clamped(uint8_t low, uint8_t high) const
Expand All @@ -126,10 +107,3 @@ Color Color::clamped(uint8_t low, uint8_t high) const
(this->b > high) ? high : ((this->b < low) ? low : this->b),
(this->a > high) ? high : ((this->a < low) ? low : this->a));
}

Color Color::clamped() const
{
const uint8_t low = 0;
const uint8_t high = 255;
return this->clamped(low, high);
}
40 changes: 29 additions & 11 deletions OpenTESArena/src/Utilities/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,34 @@

class Random;

class Color
struct Color
{
public:
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
Color(uint8_t r, uint8_t g, uint8_t b);
Color();

uint8_t r, g, b, a;

constexpr Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
this->r = r;
this->g = g;
this->b = b;
this->a = a;
}

constexpr Color(uint8_t r, uint8_t g, uint8_t b)
{
this->r = r;
this->g = g;
this->b = b;
this->a = 255;
}

constexpr Color()
{
this->r = 0;
this->g = 0;
this->b = 0;
this->a = 255;
}

static const Color Red;
static const Color Green;
static const Color Blue;
Expand All @@ -32,17 +51,16 @@ class Color
static Color fromRGBA(uint32_t rgba);
static Color fromRGB(uint32_t rgb);

Color operator+(const Color &c) const;
Color operator-(const Color &c) const;
bool operator==(const Color &c) const;
bool operator!=(const Color &c) const;
Color operator+(const Color &other) const;
Color operator-(const Color &other) const;
bool operator==(const Color &other) const;
bool operator!=(const Color &other) const;

std::string toString() const;
uint32_t toARGB() const;
uint32_t toRGBA() const;
uint32_t toRGB() const;
Color clamped(uint8_t low, uint8_t high) const;
Color clamped() const;
};

#endif

0 comments on commit 00ef1b0

Please sign in to comment.