cleaning up Color struct a little

This commit is contained in:
Noel Berry 2021-01-30 19:48:14 -08:00
parent add76cd968
commit 1e1e150411
2 changed files with 36 additions and 2 deletions

View File

@ -17,6 +17,7 @@ namespace Blah
Color(int rgb, float alpha); Color(int rgb, float alpha);
Color(uint8_t r, uint8_t g, uint8_t b); Color(uint8_t r, uint8_t g, uint8_t b);
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a); Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
Color(const Vec4& vec4);
// Parses a Hex string in the format of "#00000000" or "0x00000000" or "00000000" // Parses a Hex string in the format of "#00000000" or "0x00000000" or "00000000"
Color(const char* hexCstr); Color(const char* hexCstr);
@ -28,12 +29,20 @@ namespace Blah
// The buffer must be at least 8 bytes long // The buffer must be at least 8 bytes long
void to_hex_rgba(char* buffer) const; void to_hex_rgba(char* buffer) const;
// Returns an RGBA hex string of the color
String to_hex_rgba() const;
// Sets a Hex string to the given buffer, in the format of RRGGBB // Sets a Hex string to the given buffer, in the format of RRGGBB
// The buffer must be at least 6 bytes long // The buffer must be at least 6 bytes long
void to_hex_rgb(char* buffer) const; void to_hex_rgb(char* buffer) const;
// Returns an RGB hex string of the color
String to_hex_rgb() const;
// Convers the Color to a uint32_t
uint32_t to_rgba() const; uint32_t to_rgba() const;
// Converts the Color to a Vec4
Vec4 to_vec4() const; Vec4 to_vec4() const;
// Returns a RGBA Color representation of the integer value // Returns a RGBA Color representation of the integer value
@ -42,6 +51,7 @@ namespace Blah
// Returns a RGB Color representation of the integer value, with Alpha = 255 // Returns a RGB Color representation of the integer value, with Alpha = 255
static Color from_rgb(uint32_t value); static Color from_rgb(uint32_t value);
// Lerps between two colors
static Color lerp(Color a, Color b, float amount); static Color lerp(Color a, Color b, float amount);
// Mutliplties the Color // Mutliplties the Color
@ -50,6 +60,10 @@ namespace Blah
// assignment from int // assignment from int
Color& operator= (const int rgb); Color& operator= (const int rgb);
// comparisons
bool operator ==(const Color& rhs) const;
bool operator !=(const Color& rhs) const;
static const Color transparent; static const Color transparent;
static const Color white; static const Color white;
static const Color black; static const Color black;

View File

@ -27,6 +27,9 @@ Color::Color(uint8_t r, uint8_t g, uint8_t b)
Color::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) Color::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
: r(r), g(g), b(b), a(a) {} : r(r), g(g), b(b), a(a) {}
Color::Color(const Vec4& vec4)
: r((int)(vec4.x * 255)), g((int)(vec4.y * 255)), b((int)(vec4.z * 255)), a((int)(vec4.w * 255)) {}
Color::Color(const char* value) : r(0), g(0), b(0), a(255) Color::Color(const char* value) : r(0), g(0), b(0), a(255)
{ {
int length = 0; int length = 0;
@ -83,6 +86,13 @@ void Color::to_hex_rgba(char* buffer) const
buffer[7] = hex[(a & 0x0F) >> 0]; buffer[7] = hex[(a & 0x0F) >> 0];
} }
String Color::to_hex_rgba() const
{
String str = "00000000";
to_hex_rgba(str.cstr());
return str;
}
void Color::to_hex_rgb(char* buffer) const void Color::to_hex_rgb(char* buffer) const
{ {
buffer[0] = hex[(r & 0xF0) >> 4]; buffer[0] = hex[(r & 0xF0) >> 4];
@ -93,6 +103,13 @@ void Color::to_hex_rgb(char* buffer) const
buffer[5] = hex[(b & 0x0F) >> 0]; buffer[5] = hex[(b & 0x0F) >> 0];
} }
String Color::to_hex_rgb() const
{
String str = "000000";
to_hex_rgb(str.cstr());
return str;
}
Color Color::from_rgba(uint32_t value) Color Color::from_rgba(uint32_t value)
{ {
return return
@ -136,7 +153,7 @@ Color Color::operator*(float multiply) const
(int)(a * multiply)); (int)(a * multiply));
} }
Color& Color::operator= (const int rgb) Color& Color::operator=(const int rgb)
{ {
r = (uint8_t)((rgb & 0xFF0000) >> 16); r = (uint8_t)((rgb & 0xFF0000) >> 16);
g = (uint8_t)((rgb & 0x00FF00) >> 8); g = (uint8_t)((rgb & 0x00FF00) >> 8);
@ -145,6 +162,9 @@ Color& Color::operator= (const int rgb)
return *this; return *this;
} }
bool Color::operator ==(const Color& rhs) const { return r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a; }
bool Color::operator !=(const Color& rhs) const { return r != rhs.r || g != rhs.g || b != rhs.b || a != rhs.a; }
const Color Color::transparent = Color(0, 0, 0, 0); const Color Color::transparent = Color(0, 0, 0, 0);
const Color Color::white = Color(255, 255, 255, 255); const Color Color::white = Color(255, 255, 255, 255);
const Color Color::black = Color(0, 0, 0, 255); const Color Color::black = Color(0, 0, 0, 255);