1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Allow choosing between uppercase or lowercase hex conversion.

Lowercase by default.
This commit is contained in:
Sandu Liviu Catalin 2020-09-03 21:05:38 +03:00
parent 7ec5544948
commit b5c8121b7d
4 changed files with 52 additions and 4 deletions

View File

@ -22,6 +22,7 @@ const Color3 Color3::MAX = Color3(std::numeric_limits< Color3::Value >::max());
// ------------------------------------------------------------------------------------------------
SQChar Color3::Delim = ',';
bool Color3::UpperCaseHex = false;
// ------------------------------------------------------------------------------------------------
Color3::Color3() noexcept
@ -637,7 +638,7 @@ 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)
if (std::snprintf(buff, 16, UpperCaseHex ? "%02X%02X%02X" : "%02x%02x%02x", r, g, b) <= 0)
{
STHROWF("Format failed (somehow)");
}
@ -650,7 +651,7 @@ 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)
if (std::snprintf(buff, 16, UpperCaseHex ? "%02X%02X%02X%02X" : "%02x%02x%02x%02x", r, g, b, 0) <= 0)
{
STHROWF("Format failed (somehow)");
}
@ -722,6 +723,22 @@ const Color3 & Color3::GetEx(SQChar delim, StackStrF & str)
return col;
}
/* ------------------------------------------------------------------------------------------------
* Retrieve the hex case preference of the Color3 type.
*/
static bool SqGetColor3UpperCaseHex()
{
return Color3::UpperCaseHex;
}
/* ------------------------------------------------------------------------------------------------
* Modify the hex case preference of the Color3 type.
*/
static void SqSetColor3UpperCaseHex(bool t)
{
Color3::UpperCaseHex = t;
}
// ================================================================================================
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
@ -780,6 +797,8 @@ void Register_Color3(HSQUIRRELVM vm)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< Color3 >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Color3 >)
.StaticFunc(_SC("GetUpperCaseHex"), &SqGetColor3UpperCaseHex)
.StaticFunc(_SC("SetUpperCaseHex"), &SqSetColor3UpperCaseHex)
.StaticFmtFunc(_SC("FromStr"), &Color3::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Color3::GetEx)
// Operator Exposure

View File

@ -28,6 +28,11 @@ struct Color3
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* Whenever converting colors to hex, useupper case characters.
*/
static bool UpperCaseHex;
/* --------------------------------------------------------------------------------------------
* The red, green and blue components of this type.
*/

View File

@ -22,6 +22,7 @@ const Color4 Color4::MAX = Color4(std::numeric_limits< Color4::Value >::max());
// ------------------------------------------------------------------------------------------------
SQChar Color4::Delim = ',';
bool Color4::UpperCaseHex = false;
// ------------------------------------------------------------------------------------------------
Color4::Color4() noexcept
@ -672,7 +673,7 @@ 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)
if (std::snprintf(buff, 16, UpperCaseHex ? "%02X%02X%02X%02X" : "%02x%02x%02x%02x", r, g, b, a) <= 0)
{
STHROWF("Format failed (somehow)");
}
@ -685,7 +686,7 @@ 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)
if (std::snprintf(buff, 16, UpperCaseHex ? "%02X%02X%02X" : "%02x%02x%02x", r, g, b) <= 0)
{
STHROWF("Format failed (somehow)");
}
@ -759,6 +760,22 @@ const Color4 & Color4::GetEx(SQChar delim, StackStrF & str)
return col;
}
/* ------------------------------------------------------------------------------------------------
* Retrieve the hex case preference of the Color4 type.
*/
static bool SqGetColor4UpperCaseHex()
{
return Color4::UpperCaseHex;
}
/* ------------------------------------------------------------------------------------------------
* Modify the hex case preference of the Color4 type.
*/
static void SqSetColor4UpperCaseHex(bool t)
{
Color4::UpperCaseHex = t;
}
// ================================================================================================
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
@ -819,6 +836,8 @@ void Register_Color4(HSQUIRRELVM vm)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< Color4 >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Color4 >)
.StaticFunc(_SC("GetUpperCaseHex"), &SqGetColor4UpperCaseHex)
.StaticFunc(_SC("SetUpperCaseHex"), &SqSetColor4UpperCaseHex)
.StaticFmtFunc(_SC("FromStr"), &Color4::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Color4::GetEx)
// Operator Exposure

View File

@ -28,6 +28,11 @@ struct Color4
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* Whenever converting colors to hex, useupper case characters.
*/
static bool UpperCaseHex;
/* --------------------------------------------------------------------------------------------
* The red, green and blue components of this type.
*/