|
55 | 55 | if love._version_major ~= 11 then error("You have an outdated version of Love2d! Get 11.5 and retry.") end
|
56 | 56 |
|
57 | 57 | ----- COLOR MIGRATION helpers -----
|
58 |
| -function clamp(x, lower, upper) |
59 |
| - return math.min(math.max(x, lower), upper) |
60 |
| -end |
61 |
| - |
62 |
| -local function convertFromByte(x) |
63 |
| - return clamp(math.floor(x + 0.5) / 255, 0, 1) |
64 |
| -end |
65 |
| - |
66 |
| -local colorCache = {} |
67 |
| -for i = 0, 255 do colorCache[i] = convertFromByte(i) end |
68 |
| - |
69 |
| -local function convertFromCachedByte(x) |
70 |
| - x = clamp(x, 0, 255) |
71 |
| - local cached = colorCache[x] |
72 |
| - return (cached ~= nil and cached) or convertFromByte(x) -- fallback for decimals |
73 |
| -end |
74 |
| - |
75 |
| -local function convertFromCachedTable(table) |
76 |
| - for i, v in ipairs(table) do |
77 |
| - table[i] = v ~= nil and convertFromCachedByte(v) or nil |
78 |
| - end |
79 |
| - return table |
80 |
| -end |
| 58 | +FFIAVAILABLE = pcall(function () require("ffi") end) |
81 | 59 |
|
82 | 60 | local function convertText(text)
|
83 | 61 | if type(text) == "table" then
|
84 | 62 | for i, v in ipairs(text) do
|
85 | 63 | if type(v) == "table" then
|
86 |
| - text[i] = unpack(convertFromCachedTable(v)) |
| 64 | + text[i] = {love.math.colorFromBytes(unpack(v))} |
87 | 65 | end
|
88 | 66 | end
|
89 | 67 | end
|
|
93 | 71 | ----- COLOR MIGRATION for real -----
|
94 | 72 |
|
95 | 73 | local defaultSetColor = love.graphics.setColor
|
96 |
| -function love.graphics.setColor(r, g, b, a, ...) |
97 |
| - if type(r) == "table" then r, g, b, a = r[1], r[2], r[3], r[4] end |
98 |
| - return defaultSetColor(convertFromCachedTable({r, g, b, a}), ...) |
| 74 | +function love.graphics.setColor(r, g, b, a) |
| 75 | + if type(r) == "table" then r, g, b, a = unpack(r) end |
| 76 | + return defaultSetColor(love.math.colorFromBytes(r, g, b, a)) |
99 | 77 | end
|
100 | 78 |
|
101 | 79 | local defaultGetColor = love.graphics.getColor
|
102 |
| -function love.graphics.getColor(...) |
103 |
| - return love.math.colorToBytes(defaultGetColor(...)) |
| 80 | +function love.graphics.getColor() |
| 81 | + return love.math.colorToBytes(defaultGetColor()) |
104 | 82 | end
|
105 | 83 |
|
106 | 84 | local defaultSetBackgroundColor = love.graphics.setBackgroundColor
|
107 |
| -function love.graphics.setBackgroundColor(r, g, b, a, ...) |
108 |
| - if type(r) == "table" then r, g, b, a = r[1], r[2], r[3], r[4] end |
109 |
| - return defaultSetBackgroundColor(convertFromCachedTable({r, g, b, a}), ...) |
| 85 | +function love.graphics.setBackgroundColor(r, g, b, a) |
| 86 | + if type(r) == "table" then r, g, b, a = unpack(r) end |
| 87 | + return defaultSetBackgroundColor(love.math.colorFromBytes(r, g, b, a)) |
110 | 88 | end
|
111 | 89 |
|
112 | 90 | local defaultGetBackgroundColor = love.graphics.getBackgroundColor
|
113 |
| -function love.graphics.getBackgroundColor(...) |
114 |
| - return love.math.colorToBytes(defaultGetBackgroundColor(...)) |
| 91 | +function love.graphics.getBackgroundColor() |
| 92 | + return love.math.colorToBytes(defaultGetBackgroundColor()) |
115 | 93 | end
|
116 | 94 |
|
117 | 95 | local defaultClear = love.graphics.clear
|
118 | 96 | function love.graphics.clear(r, g, b, a, ...)
|
119 | 97 | if r ~= nil and g ~= nil and b ~= nil then
|
120 |
| - r, g, b, a = unpack(convertFromCachedTable({r, g, b, a})) |
| 98 | + r, g, b, a = love.math.colorFromBytes(r, g, b, a) |
121 | 99 | end
|
122 | 100 | return defaultClear(r, g, b, a, ...)
|
123 | 101 | end
|
@@ -163,19 +141,19 @@ local SpriteBatch = debug.getregistry().SpriteBatch
|
163 | 141 |
|
164 | 142 | local defaultSpriteBatchSetColor = SpriteBatch.setColor
|
165 | 143 | function SpriteBatch:setColor(...)
|
166 |
| - return defaultSpriteBatchSetColor(self, unpack(convertFromCachedTable({...}))) |
| 144 | + return defaultSpriteBatchSetColor(self, love.math.colorFromBytes(...)) |
167 | 145 | end
|
168 | 146 |
|
169 | 147 | local defaultSpriteBatchGetColor = SpriteBatch.getColor
|
170 | 148 | function SpriteBatch:getColor(...)
|
171 | 149 | return love.math.colorToBytes(defaultSpriteBatchGetColor(self, ...))
|
172 | 150 | end
|
173 | 151 |
|
174 |
| -local ImageData = debug.getregistry().ImageData |
| 152 | +local ImageData = debug.getregistry().ImageData -- TODO: use FFI by default? not sure if fetching the pointer constantly will actually be efficient |
175 | 153 |
|
176 | 154 | local defaultImageDataSetPixel = ImageData.setPixel
|
177 | 155 | function ImageData:setPixel(x, y, ...)
|
178 |
| - return defaultImageDataSetPixel(self, x, y, unpack(convertFromCachedTable({...}))) |
| 156 | + return defaultImageDataSetPixel(self, x, y, love.math.colorFromBytes(...)) |
179 | 157 | end
|
180 | 158 |
|
181 | 159 | local defaultImageDataGetPixel = ImageData.getPixel
|
|
185 | 163 |
|
186 | 164 | local defaultImageDataMapPixel = ImageData.mapPixel
|
187 | 165 | function ImageData:mapPixel(pixelFunction, ...)
|
188 |
| - return defaultImageDataMapPixel(self, function(x, y, r, g, b, a) return unpack(convertFromCachedTable({pixelFunction(x, y, love.math.colorToBytes(r, g, b, a))})) end, ...) |
| 166 | + return defaultImageDataMapPixel(self, function(x, y, r, g, b, a) |
| 167 | + local nr, ng, nb, na = pixelFunction(x, y, love.math.colorToBytes(r, g, b, a)) |
| 168 | + if nr == nil then return r, g, b, a end |
| 169 | + return love.math.colorFromBytes(nr, ng, nb, na) |
| 170 | + end, ...) |
189 | 171 | end
|
190 | 172 |
|
191 | 173 | -- TODO: ParticleSystem, linear/gamma functions, points, sendColor, newMesh, [gs]etVertex
|
|
0 commit comments