From 5ba620c066c793da86f2f6f84337788d1b91a071 Mon Sep 17 00:00:00 2001 From: Noel Berry Date: Tue, 25 May 2021 21:31:18 -0700 Subject: [PATCH] cleaning up color struct --- include/blah/images/image.h | 4 ++-- include/blah/numerics/color.h | 16 +++++++++++----- src/numerics/color.cpp | 18 ++++-------------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/include/blah/images/image.h b/include/blah/images/image.h index f89fb5e..9c951f0 100644 --- a/include/blah/images/image.h +++ b/include/blah/images/image.h @@ -34,8 +34,8 @@ namespace Blah Image& operator=(Image&& src) noexcept; ~Image(); - // disposes the existing image and recreates it from a stream - void from_stream(Stream& stream); + // creates the image from a stream, and returns true if successful + bool from_stream(Stream& stream); // disposes the image and resets its values to defaults void dispose(); diff --git a/include/blah/numerics/color.h b/include/blah/numerics/color.h index d1aff76..9c00c31 100644 --- a/include/blah/numerics/color.h +++ b/include/blah/numerics/color.h @@ -73,15 +73,15 @@ namespace Blah while (len < 8 && *(hex_string + len) != '\0') len++; - if (len >= 8) - a = (BLAH_HEX_VALUE(hex_string[6]) << 4) + BLAH_HEX_VALUE(hex_string[7]); - if (len >= 6) { r = (BLAH_HEX_VALUE(hex_string[0]) << 4) + BLAH_HEX_VALUE(hex_string[1]); g = (BLAH_HEX_VALUE(hex_string[2]) << 4) + BLAH_HEX_VALUE(hex_string[3]); b = (BLAH_HEX_VALUE(hex_string[4]) << 4) + BLAH_HEX_VALUE(hex_string[5]); } + + if (len >= 8) + a = (BLAH_HEX_VALUE(hex_string[6]) << 4) + BLAH_HEX_VALUE(hex_string[7]); } // Premultiplies the Color @@ -99,10 +99,16 @@ namespace Blah String to_hex_rgb() const; // Converts the Color to a Vec3 (RGB) - Vec3 to_vec3() const; + constexpr Vec3 to_vec3() const + { + return Vec3(r / 255.0f, g / 255.0f, b / 255.0f); + } // Converts the Color to a Vec4 (RGBA) - Vec4 to_vec4() const; + constexpr Vec4 to_vec4() const + { + return Vec4(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); + } // Convers the Color to a u32 constexpr u32 to_rgba() const diff --git a/src/numerics/color.cpp b/src/numerics/color.cpp index 757a440..52d46f8 100644 --- a/src/numerics/color.cpp +++ b/src/numerics/color.cpp @@ -1,23 +1,11 @@ #include -#include -#include using namespace Blah; -char const hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; - -Vec3 Color::to_vec3() const -{ - return Vec3(r / 255.0f, g / 255.0f, b / 255.0f); -} - -Vec4 Color::to_vec4() const -{ - return Vec4(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); -} - String Color::to_hex_rgba() const { + static const char* hex = "0123456789ABCDEF"; + String str = "00000000"; str[0] = hex[(r & 0xF0) >> 4]; str[1] = hex[(r & 0x0F) >> 0]; @@ -32,6 +20,8 @@ String Color::to_hex_rgba() const String Color::to_hex_rgb() const { + static const char* hex = "0123456789ABCDEF"; + String str = "000000"; str[0] = hex[(r & 0xF0) >> 4]; str[1] = hex[(r & 0x0F) >> 0];