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

Implement hex conversion to color types.

This commit is contained in:
Sandu Liviu Catalin 2020-09-03 20:52:46 +03:00
parent 455c401c2c
commit 7ec5544948
4 changed files with 81 additions and 5 deletions

View File

@ -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)

View File

@ -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.
*/

View File

@ -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)

View File

@ -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