Last active
January 14, 2022 00:06
-
-
Save EngineerSmith/f2f81326e1f91b0beb9f4135f9a42048 to your computer and use it in GitHub Desktop.
Hex value to string lua using ffi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ffi = require("ffi") | |
ffi.cdef[[ | |
typedef struct { | |
union { | |
uint64_t rgba; | |
struct { | |
uint8_t a; | |
uint8_t b; | |
uint8_t g; | |
uint8_t r; | |
}; | |
}; | |
} leColor; | |
typedef struct { | |
union { | |
uint64_t rgba; | |
struct { | |
uint8_t r; | |
uint8_t g; | |
uint8_t b; | |
uint8_t a; | |
}; | |
}; | |
} beColor; | |
]] | |
local ffiColorToString = function(foo) | |
return ("0x%02x%02x%02x%02x"):format(foo.r,foo.g,foo.b,foo.a) | |
end | |
local color = 0x000000FF | |
local struct | |
if ffi.abi("le") then | |
struct = ffi.new("leColor") | |
else | |
struct = ffi.new("beColor") | |
end | |
struct.rgba = color | |
local print = love.graphics.print | |
love.draw = function() | |
print(ffiColorToString(struct)) -- prints 0x000000FF | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment