From 4327eaf72c38f630f521d520374639b4fb3ba598 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Tue, 14 Jun 2016 02:18:37 +0300 Subject: [PATCH] Add helper functions to pack colors into integers. This removes the need to create an instance of the Color3 or Color4 classes just to obtain a packed color integer. --- source/Base/Shared.cpp | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/source/Base/Shared.cpp b/source/Base/Shared.cpp index 2a2f75b1..4cf1a7ef 100644 --- a/source/Base/Shared.cpp +++ b/source/Base/Shared.cpp @@ -813,6 +813,38 @@ Color3 GetColor(CSStr name) } } +// ------------------------------------------------------------------------------------------------ +static SQInteger SqPackRGB(SQInteger r, SQInteger g, SQInteger b) +{ + return static_cast< Int32 >(SQMOD_PACK_RGB( + ConvTo< Uint8 >::From(r), + ConvTo< Uint8 >::From(g), + ConvTo< Uint8 >::From(b) + )); +} + +// ------------------------------------------------------------------------------------------------ +static SQInteger SqPackRGBA(SQInteger r, SQInteger g, SQInteger b, SQInteger a) +{ + return static_cast< Int32 >(SQMOD_PACK_RGBA( + ConvTo< Uint8 >::From(r), + ConvTo< Uint8 >::From(g), + ConvTo< Uint8 >::From(b), + ConvTo< Uint8 >::From(a) + )); +} + +// ------------------------------------------------------------------------------------------------ +static SQInteger SqPackARGB(SQInteger r, SQInteger g, SQInteger b, SQInteger a) +{ + return static_cast< Int32 >(SQMOD_PACK_ARGB( + ConvTo< Uint8 >::From(a), + ConvTo< Uint8 >::From(r), + ConvTo< Uint8 >::From(g), + ConvTo< Uint8 >::From(b) + )); +} + // ================================================================================================ void Register_Base(HSQUIRRELVM vm) { @@ -830,7 +862,9 @@ void Register_Base(HSQUIRRELVM vm) .Func(_SC("ClampMaxF"), &ClampMax< SQFloat >) .Func(_SC("NextPow2"), &NextPow2) .Func(_SC("SToB"), &SToB) - .Func(_SC("GetColor"), &GetColor); + .Func(_SC("PackRGB"), &SqPackRGB) + .Func(_SC("PackRGBA"), &SqPackRGBA) + .Func(_SC("PackARGB"), &SqPackARGB); } } // Namespace:: SqMod