Fix GameScriptColor's D3DCOLOR operator and __tostring, and add a converting constructor.

This commit is contained in:
hispidence 2021-08-29 17:05:06 +01:00
parent d946f4c118
commit e834d38522
2 changed files with 16 additions and 3 deletions

View file

@ -13,6 +13,7 @@ void GameScriptColor::Register(sol::state* state)
{
state->new_usertype<GameScriptColor>("Color",
sol::constructors<GameScriptColor(byte, byte, byte), GameScriptColor(byte, byte, byte, byte)>(),
sol::meta_function::to_string, &GameScriptColor::ToString,
/// (int) red component
//@mem r
@ -63,6 +64,17 @@ GameScriptColor::GameScriptColor(Vector3 const& col) : GameScriptColor{ col.x, c
GameScriptColor::GameScriptColor(Vector4 const& col) : GameScriptColor{ col.x, col.y, col.z, col.w } {}
GameScriptColor::GameScriptColor(D3DCOLOR col)
{
b = col & 0xFF;
col >>= 8;
g = col & 0xFF;
col >>= 8;
r = col & 0xFF;
col >>= 8;
a = col & 0xFF;
}
GameScriptColor::operator Vector3() const
{
return Vector3{ float(r), float(g), float(b) };
@ -73,16 +85,16 @@ GameScriptColor::operator Vector4() const
return Vector4{ float(r), float(g), float(b), float(a) };
}
// D3DCOLOR is 32 bits and is layed out as RGBA.
// D3DCOLOR is 32 bits and is layed out as ARGB.
GameScriptColor::operator D3DCOLOR() const
{
D3DCOLOR col = a;
col <<= 8;
col += b;
col += r;
col <<= 8;
col += g;
col <<= 8;
col += r;
col += b;
return col;
}

View file

@ -19,6 +19,7 @@ public:
GameScriptColor(byte r, byte g, byte b, byte a);
GameScriptColor(Vector3 const &);
GameScriptColor(Vector4 const &);
GameScriptColor(D3DCOLOR);
operator Vector3() const;
operator Vector4() const;