-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcindy.lua
235 lines (177 loc) · 7.21 KB
/
cindy.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
local cindy = {
_VERSION = 'cindy 0.1.2',
_LICENSE = 'WTFPL, http://www.wtfpl.net',
_URL = 'https://github.com/megagrump/cindy',
_DESCRIPTION = 'True Colors for LÖVE 11',
}
--[[-----------------------------------------------------------------------------------------------------------------
cindy adds functions to LÖVE 11.x that accept/return colors in the [0,255] range instead of the newly introduced
[0.0,1.0] range.
In love.graphics:
- clearBytes
- getColorBytes, setColorBytes
- getBackgroundColorBytes, setBackgroundColorBytes
In ImageData:
- getPixelBytes, setPixelBytes
- mapPixelBytes
In ParticleSystem:
- setColorsBytes, getColorsBytes
In SpriteBatch:
- getColorBytes, setColorBytes
In Shader:
- sendColorBytes
These functions behave the same as their built-in counterparts, except for the different value range.
Note that calling them has additional runtime costs.
To replace all original functions, call cindy.applyPatch() at the start of the program: require('cindy').applyPatch() -
this effectively restores the pre-11.0 behavior.
-------------------------------------------------------------------------------------------------------------------]]
local gfx, reg = love.graphics, debug.getregistry()
local ImageData, ParticleSystem, SpriteBatch, Shader = reg.ImageData, reg.ParticleSystem, reg.SpriteBatch, reg.Shader
local clear, getColor, setColor = gfx.clear, gfx.getColor, gfx.setColor
local getBackgroundColor, setBackgroundColor = gfx.getBackgroundColor, gfx.setBackgroundColor
local getPixel, setPixel, mapPixel = ImageData.getPixel, ImageData.setPixel, ImageData.mapPixel
local getParticleColors, setParticleColors = ParticleSystem.getColors, ParticleSystem.setColors
local getBatchColor, setBatchColor = SpriteBatch.getColor, SpriteBatch.setColor
local sendColor = Shader.sendColor
---------------------------------------------------------------------------------------------------------------------
local function round(v)
return math.floor(v + .5)
end
-- convert a single channel value from [0.0,1.0] to [0,255]
function cindy.channel2byte(c)
return round(c * 255)
end
-- convert a single channel value from [0,255] to [0.0,1.0]
function cindy.byte2channel(c)
return c / 255
end
-- convert RGBA values from [0.0,1.0] to [0,255]
function cindy.rgba2bytes(r, g, b, a)
return round(r * 255), round(g * 255), round(b * 255), a and round(a * 255)
end
-- convert RGBA values from [0,255] to [0.0,1.0]
function cindy.bytes2rgba(r, g, b, a)
return r / 255, g / 255, b / 255, a and a / 255
end
-- convert RGBA value table from [0.0,1.0] to [0,255]. places the result in dest, if given
function cindy.table2bytes(color, dest)
dest = dest or {}
dest[1], dest[2], dest[3], dest[4] = cindy.rgba2bytes(color[1], color[2], color[3], color[4])
return dest
end
-- convert RGBA value table from [0,255] to [0.0,1.0]. places the result in dest, if given
function cindy.bytes2table(color, dest)
dest = dest or {}
dest[1], dest[2], dest[3], dest[4] = cindy.bytes2rgba(color[1], color[2], color[3], color[4])
return dest
end
-- convert RGBA values or table from [0.0,1.0] to [0,255]. returns separate values
function cindy.color2bytes(r, g, b, a)
if type(r) == 'table' then
r, g, b, a = r[1], r[2], r[3], r[4]
end
return cindy.rgba2bytes(r, g, b, a)
end
-- convert RGBA values or table from [0,255] to [0.0,1.0]. returns separate values
function cindy.bytes2color(r, g, b, a)
if type(r) == 'table' then
r, g, b, a = r[1], r[2], r[3], r[4]
end
return cindy.bytes2rgba(r, g, b, a)
end
-- patch all LÖVE functions to accept colors in the [0,255] range
function cindy.applyPatch()
gfx.clear, gfx.getColor, gfx.setColor = gfx.clearBytes, gfx.getColorBytes, gfx.setColorBytes
gfx.getBackgroundColor, gfx.setBackgroundColor = gfx.getBackgroundColorBytes, gfx.setBackgroundColorBytes
ImageData.getPixel, ImageData.setPixel = ImageData.getPixelBytes, ImageData.setPixelBytes
ImageData.mapPixel = ImageData.mapPixelBytes
ParticleSystem.getColors, ParticleSystem.setColors = ParticleSystem.getColorsBytes, ParticleSystem.setColorsBytes
SpriteBatch.getColor, SpriteBatch.setColor = SpriteBatch.getColorBytes, SpriteBatch.setColorBytes
Shader.sendColor = Shader.sendColorBytes
return cindy
end
---------------------------------------------------------------------------------------------------------------------
local tempTables = { {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} }
function gfx.getColorBytes()
return cindy.rgba2bytes(getColor())
end
function gfx.setColorBytes(r, g, b, a)
return setColor(cindy.bytes2color(r, g, b, a))
end
function gfx.getBackgroundColorBytes()
return cindy.rgba2bytes(getBackgroundColor())
end
function gfx.setBackgroundColorBytes(r, g, b, a)
return setBackgroundColor(cindy.bytes2color(r, g, b, a))
end
function gfx.clearBytes(...)
local nargs = select('#', ...)
if nargs == 0 then return clear() end
local argtype = type(select(1, ...))
if argtype == 'boolean' then return clear(...) end
local converter = argtype == 'table' and cindy.bytes2table or cindy.byte2channel
local args, i = {...}, 1
repeat
args[i] = converter(args[i], tempTables[i])
i = i + 1
until type(args[i]) ~= argtype
return clear(unpack(args))
end
---------------------------------------------------------------------------------------------------------------------
function ImageData:getPixelBytes(x, y)
return cindy.rgba2bytes(getPixel(self, x, y))
end
function ImageData:setPixelBytes(x, y, r, g, b, a)
return setPixel(self, x, y, cindy.bytes2rgba(r, g, b, a))
end
function ImageData:mapPixelBytes(fn)
return mapPixel(self, function(x, y, r, g, b, a)
return cindy.bytes2rgba(fn(x, y, cindy.rgba2bytes(r, g, b, a)))
end)
end
---------------------------------------------------------------------------------------------------------------------
function ParticleSystem:setColorsBytes(...)
local args, nargs = {...}, select('#', ...)
if type(args[1]) == 'table' then
for i = 1, nargs do
args[i] = cindy.bytes2table(args[i], tempTables[i])
end
else
for i = 1, nargs do
args[i] = args[i] / 255
end
end
return setParticleColors(self, unpack(args))
end
function ParticleSystem:getColorsBytes()
local colors = { getParticleColors(self) }
local ncolors = #colors
for i = 1, ncolors do
local rgba = colors[i]
rgba[1], rgba[2], rgba[3], rgba[4] = cindy.rgba2bytes(rgba[1], rgba[2], rgba[3], rgba[4])
end
return unpack(colors)
end
---------------------------------------------------------------------------------------------------------------------
function SpriteBatch:getColorBytes()
local r, g, b, a = getBatchColor(self)
if r then
return cindy.rgba2bytes(r, g, b, a)
end
end
function SpriteBatch:setColorBytes(r, g, b, a)
if r then
return setBatchColor(self, cindy.bytes2color(r, g, b, a))
end
return setBatchColor(self)
end
---------------------------------------------------------------------------------------------------------------------
function Shader:sendColorBytes(name, ...)
local colors, ncolors = {...}, select('#', ...)
for i = 1, ncolors do
colors[i] = cindy.bytes2table(colors[i], tempTables[i])
end
return sendColor(self, name, unpack(colors))
end
---------------------------------------------------------------------------------------------------------------------
return cindy