From 1e1e150411a1c94ce8b34f7906ae8cd0e3d169c2 Mon Sep 17 00:00:00 2001 From: Noel Berry Date: Sat, 30 Jan 2021 19:48:14 -0800 Subject: [PATCH] cleaning up Color struct a little --- include/blah/math/color.h | 16 +++++++++++++++- src/math/color.cpp | 22 +++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/include/blah/math/color.h b/include/blah/math/color.h index 0b99316..b6bee8f 100644 --- a/include/blah/math/color.h +++ b/include/blah/math/color.h @@ -17,6 +17,7 @@ namespace Blah 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, uint8_t a); + Color(const Vec4& vec4); // Parses a Hex string in the format of "#00000000" or "0x00000000" or "00000000" Color(const char* hexCstr); @@ -28,12 +29,20 @@ namespace Blah // The buffer must be at least 8 bytes long 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 // The buffer must be at least 6 bytes long 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; + // Converts the Color to a Vec4 Vec4 to_vec4() const; // 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 static Color from_rgb(uint32_t value); + // Lerps between two colors static Color lerp(Color a, Color b, float amount); // Mutliplties the Color @@ -49,7 +59,11 @@ namespace Blah // assignment from int 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 white; static const Color black; diff --git a/src/math/color.cpp b/src/math/color.cpp index 0344ace..3faae74 100644 --- a/src/math/color.cpp +++ b/src/math/color.cpp @@ -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) : 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) { int length = 0; @@ -83,6 +86,13 @@ void Color::to_hex_rgba(char* buffer) const 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 { buffer[0] = hex[(r & 0xF0) >> 4]; @@ -93,6 +103,13 @@ void Color::to_hex_rgb(char* buffer) const 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) { return @@ -136,7 +153,7 @@ Color Color::operator*(float multiply) const (int)(a * multiply)); } -Color& Color::operator= (const int rgb) +Color& Color::operator=(const int rgb) { r = (uint8_t)((rgb & 0xFF0000) >> 16); g = (uint8_t)((rgb & 0x00FF00) >> 8); @@ -145,6 +162,9 @@ Color& Color::operator= (const int rgb) 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::white = Color(255, 255, 255, 255); const Color Color::black = Color(0, 0, 0, 255);