Update representation of RGBA

This commit is contained in:
ShadelessFox 2025-02-22 17:50:21 +01:00
parent 0b6ba2faee
commit f7fa9774d6
No known key found for this signature in database
GPG Key ID: 1CF8D77ADDADADD9

View File

@ -14,8 +14,10 @@ LuaCanvas::LuaCanvas(
} }
union RGBA { union RGBA {
uint8_t rgba[4]; struct {
uint32_t raw; uint8_t r, g, b, a;
};
uint32_t rgba;
}; };
static RGBA* get_at(const ImageData& data, uint index) { static RGBA* get_at(const ImageData& data, uint index) {
@ -41,7 +43,7 @@ static int l_at(State* L) {
auto y = static_cast<uint>(tointeger(L, 3)); auto y = static_cast<uint>(tointeger(L, 3));
if (auto pixel = get_at(L, x, y)) { if (auto pixel = get_at(L, x, y)) {
return pushinteger(L, pixel->raw); return pushinteger(L, pixel->rgba);
} }
return 0; return 0;
@ -54,19 +56,19 @@ static int l_set(State* L) {
if (auto pixel = get_at(L, x, y)) { if (auto pixel = get_at(L, x, y)) {
switch (gettop(L)) { switch (gettop(L)) {
case 4: case 4:
pixel->raw = static_cast<uint>(tointeger(L, 4)); pixel->rgba = static_cast<uint>(tointeger(L, 4));
return 1; return 1;
case 6: case 6:
pixel->rgba[0] = static_cast<ubyte>(tointeger(L, 4)); pixel->r = static_cast<ubyte>(tointeger(L, 4));
pixel->rgba[1] = static_cast<ubyte>(tointeger(L, 5)); pixel->g = static_cast<ubyte>(tointeger(L, 5));
pixel->rgba[2] = static_cast<ubyte>(tointeger(L, 6)); pixel->b = static_cast<ubyte>(tointeger(L, 6));
pixel->rgba[3] = 255; pixel->a = 255;
return 1; return 1;
case 7: case 7:
pixel->rgba[0] = static_cast<ubyte>(tointeger(L, 4)); pixel->r = static_cast<ubyte>(tointeger(L, 4));
pixel->rgba[1] = static_cast<ubyte>(tointeger(L, 5)); pixel->g = static_cast<ubyte>(tointeger(L, 5));
pixel->rgba[2] = static_cast<ubyte>(tointeger(L, 6)); pixel->b = static_cast<ubyte>(tointeger(L, 6));
pixel->rgba[3] = static_cast<ubyte>(tointeger(L, 7)); pixel->a = static_cast<ubyte>(tointeger(L, 7));
return 1; return 1;
default: default:
return 0; return 0;
@ -97,7 +99,7 @@ static int l_meta_index(State* L) {
auto& data = texture->data(); auto& data = texture->data();
if (isnumber(L, 2)) { if (isnumber(L, 2)) {
if (auto pixel = get_at(data, static_cast<uint>(tointeger(L, 2)))) { if (auto pixel = get_at(data, static_cast<uint>(tointeger(L, 2)))) {
return pushinteger(L, pixel->raw); return pushinteger(L, pixel->rgba);
} }
} }
if (isstring(L, 2)) { if (isstring(L, 2)) {
@ -123,7 +125,7 @@ static int l_meta_newindex(State* L) {
auto& data = texture->data(); auto& data = texture->data();
if (isnumber(L, 2) && isnumber(L, 3)) { if (isnumber(L, 2) && isnumber(L, 3)) {
if (auto pixel = get_at(data, static_cast<uint>(tointeger(L, 2)))) { if (auto pixel = get_at(data, static_cast<uint>(tointeger(L, 2)))) {
pixel->raw = static_cast<uint>(tointeger(L, 3)); pixel->rgba = static_cast<uint>(tointeger(L, 3));
return 1; return 1;
} }
return 1; return 1;