Lua's type conversion made me crazy. But when my code has made screenshot with its own colors, I suddenly noticed that the code is almost identical to the NuPogodi's code (after he made some corrections).
In resulting file the high and the low bits are still equal, so the tiny loss of contrast in highlights should be. But who cares?