From 7ec5544948043b7b6d1cde0be9c64477c033aba5 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Thu, 3 Sep 2020 20:52:46 +0300 Subject: [PATCH] Implement hex conversion to color types. --- module/Base/Color3.cpp | 28 ++++++++++++++++++++++++++++ module/Base/Color3.hpp | 10 ++++++++++ module/Base/Color4.cpp | 28 ++++++++++++++++++++++++++++ module/Base/Color4.hpp | 20 +++++++++++++++----- 4 files changed, 81 insertions(+), 5 deletions(-) diff --git a/module/Base/Color3.cpp b/module/Base/Color3.cpp index fff00fed..48a0f5f4 100644 --- a/module/Base/Color3.cpp +++ b/module/Base/Color3.cpp @@ -632,6 +632,32 @@ void Color3::Inverse() b = static_cast< Value >(~b); } +// ------------------------------------------------------------------------------------------------ +LightObj Color3::ToHex() const +{ + SQChar buff[16]; // 7 should be enough + // Attempt to perform the format in the local buffer + if (std::snprintf(buff, 16, "%02x%02x%02x", r, g, b) <= 0) + { + STHROWF("Format failed (somehow)"); + } + // Return the resulted string + return LightObj{buff, -1}; +} + +// ------------------------------------------------------------------------------------------------ +LightObj Color3::ToHex4() const +{ + SQChar buff[16]; // 9 should be enough + // Attempt to perform the format in the local buffer + if (std::snprintf(buff, 16, "%02x%02x%02x%02x", r, g, b, 0) <= 0) + { + STHROWF("Format failed (somehow)"); + } + // Return the resulted string + return LightObj{buff, -1}; +} + // ------------------------------------------------------------------------------------------------ LightObj Color3::Format(const String & spec, StackStrF & fmt) const { @@ -733,6 +759,8 @@ void Register_Color3(HSQUIRRELVM vm) .Prop(_SC("RGB"), &Color3::GetRGB, &Color3::SetRGB) .Prop(_SC("RGBA"), &Color3::GetRGBA, &Color3::SetRGBA) .Prop(_SC("ARGB"), &Color3::GetARGB, &Color3::SetARGB) + .Prop(_SC("Hex"), &Color3::ToHex) + .Prop(_SC("Hex4"), &Color3::ToHex4) // Member Methods .Func(_SC("SetScalar"), &Color3::SetScalar) .Func(_SC("SetColor3"), &Color3::SetColor3) diff --git a/module/Base/Color3.hpp b/module/Base/Color3.hpp index 3030d29a..900fda22 100644 --- a/module/Base/Color3.hpp +++ b/module/Base/Color3.hpp @@ -498,6 +498,16 @@ struct Color3 */ void Inverse(); + /* -------------------------------------------------------------------------------------------- + * Generate a 3 component hex color from the values in this instance. + */ + LightObj ToHex() const; + + /* -------------------------------------------------------------------------------------------- + * Generate a 4 component hex color from the values in this instance. + */ + LightObj ToHex4() const; + /* -------------------------------------------------------------------------------------------- * Generate a formatted string with the values from this instance. */ diff --git a/module/Base/Color4.cpp b/module/Base/Color4.cpp index aae90da2..2defb4de 100644 --- a/module/Base/Color4.cpp +++ b/module/Base/Color4.cpp @@ -667,6 +667,32 @@ void Color4::Inverse() a = static_cast< Value >(~a); } +// ------------------------------------------------------------------------------------------------ +LightObj Color4::ToHex() const +{ + SQChar buff[16]; // 9 should be enough + // Attempt to perform the format in the local buffer + if (std::snprintf(buff, 16, "%02x%02x%02x%02x", r, g, b, a) <= 0) + { + STHROWF("Format failed (somehow)"); + } + // Return the resulted string + return LightObj{buff, -1}; +} + +// ------------------------------------------------------------------------------------------------ +LightObj Color4::ToHex3() const +{ + SQChar buff[16]; // 7 should be enough + // Attempt to perform the format in the local buffer + if (std::snprintf(buff, 16, "%02x%02x%02x", r, g, b) <= 0) + { + STHROWF("Format failed (somehow)"); + } + // Return the resulted string + return LightObj{buff, -1}; +} + // ------------------------------------------------------------------------------------------------ LightObj Color4::Format(const String & spec, StackStrF & fmt) const { @@ -772,6 +798,8 @@ void Register_Color4(HSQUIRRELVM vm) .Prop(_SC("RGB"), &Color4::GetRGB, &Color4::SetRGB) .Prop(_SC("RGBA"), &Color4::GetRGBA, &Color4::SetRGBA) .Prop(_SC("ARGB"), &Color4::GetARGB, &Color4::SetARGB) + .Prop(_SC("Hex"), &Color4::ToHex) + .Prop(_SC("Hex3"), &Color4::ToHex3) // Member Methods .Func(_SC("SetScalar"), &Color4::SetScalar) .Func(_SC("SetColor3"), &Color4::SetColor3) diff --git a/module/Base/Color4.hpp b/module/Base/Color4.hpp index dca55016..671331ef 100644 --- a/module/Base/Color4.hpp +++ b/module/Base/Color4.hpp @@ -498,6 +498,21 @@ struct Color4 */ void Inverse(); + /* -------------------------------------------------------------------------------------------- + * Generate a 4 component hex color from the values in this instance. + */ + LightObj ToHex() const; + + /* -------------------------------------------------------------------------------------------- + * Generate a 3 component hex color from the values in this instance. + */ + LightObj ToHex3() const; + + /* -------------------------------------------------------------------------------------------- + * Generate a formatted string with the values from this instance. + */ + LightObj Format(const String & spec, StackStrF & fmt) const; + /* -------------------------------------------------------------------------------------------- * Extract the values for components of the Color4 type from a string. */ @@ -507,11 +522,6 @@ struct Color4 * Extract the values for components of the Color4 type from a string. */ static const Color4 & GetEx(SQChar delim, StackStrF & str); - - /* -------------------------------------------------------------------------------------------- - * Generate a formatted string with the values from this instance. - */ - LightObj Format(const String & spec, StackStrF & fmt) const; }; } // Namespace:: SqMod