-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathokshift.lua
More file actions
431 lines (364 loc) · 10.6 KB
/
Copy pathokshift.lua
File metadata and controls
431 lines (364 loc) · 10.6 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
-- OKShift for Aseprite
-- OKLab L is perceptual lightness, so rotating hue in OKLCh keeps perceptual brightness.
-- way more swag than HSV/HSL/YIQ.
-- color math
local function srgb_to_linear(c)
if c<=0.04045 then return c/12.92 end
return ((c+0.055)/1.055)^2.4
end
local function linear_to_srgb(c)
if c<=0.0031308 then return c*12.92 end
return 1.055*(c^(1/2.4))-0.055
end
-- stands for cube root
local function cbrt(x)
-- lua's ^ doesn't like negative bases with fractional exponents, gives NaNs for some reason
if x<0 then return -((-x)^(1/3)) end
return x^(1/3)
end
local function rgb_to_oklab(r,g,b)
r = srgb_to_linear(r/255)
g = srgb_to_linear(g/255)
b = srgb_to_linear(b/255)
local l = 0.4122214708*r+0.5363325363*g+0.0514459929*b
local m = 0.2119034982*r+0.6806995451*g+0.1073969566*b
local s = 0.0883024619*r+0.2817188376*g+0.6299787005*b
local l_ = cbrt(l)
local m_ = cbrt(m)
local s_ = cbrt(s)
local L = 0.2104542553*l_+0.7936177850*m_-0.0040720468*s_
local a = 1.9779984951*l_-2.4285922050*m_+0.4505937099*s_
local bb = 0.0259040371*l_+0.7827717662*m_-0.8086757660*s_
return L,a,bb
end
local function oklab_to_rgb(L,a,b)
local l_ = L+0.3963377774*a+0.2158037573*b
local m_ = L-0.1055613458*a-0.0638541728*b
local s_ = L-0.0894841775*a-1.2914855480*b
local l = l_*l_*l_
local m = m_*m_*m_
local s = s_*s_*s_
local r = 4.0767416621*l-3.3077115913*m+0.2309699292*s
local g = -1.2684380046*l+2.6097574011*m-0.3413193965*s
local bl = -0.0041960863*l-0.7034186147*m+1.7076147010*s
r = linear_to_srgb(r)
g = linear_to_srgb(g)
bl = linear_to_srgb(bl)
-- gamut clip
r = math.max(0,math.min(1,r))
g = math.max(0,math.min(1,g))
bl = math.max(0,math.min(1,bl))
return math.floor(r*255+0.5),math.floor(g*255+0.5),math.floor(bl*255+0.5)
end
local function shift_color(r,g,b,hueDeg,chromaMult,lightShift)
local L,a,bb = rgb_to_oklab(r,g,b)
-- to OKLCh polar form
local C = math.sqrt(a*a+bb*bb)
local h = math.atan(bb,a)
h = h+math.rad(hueDeg)
C = C*chromaMult
L = L+lightShift
a = C*math.cos(h)
bb = C*math.sin(h)
return oklab_to_rgb(L,a,bb)
end
-- main
-- memorise packed rgba values
local function make_shifter(hue,chroma,light)
local pc = app.pixelColor
local cache = {}
return function(px)
local out = cache[px]
if out==nil then
local a = pc.rgbaA(px)
if a==0 then
out = px
else
local nr,ng,nb = shift_color(pc.rgbaR(px),pc.rgbaG(px),pc.rgbaB(px),hue,chroma,light)
out = pc.rgba(nr,ng,nb,a)
end
cache[px] = out
end
return out
end
end
-- indexed sprites... repoint every pixel at the closest color the palette already has
-- same as native hue/sat. closest is measured in OKLab, obviously :3
local function palette_candidates(palette,transparentIdx)
local cand = {}
for i=0,#palette-1 do
local c = palette:getColor(i)
-- an invisible entry is never a good place to land
if i~=transparentIdx and c.alpha>0 then
local L,a,b = rgb_to_oklab(c.red,c.green,c.blue)
cand[#cand+1] = {idx=i,L=L,a=a,b=b,alpha=c.alpha}
end
end
return cand
end
local function nearest_index(cand,r,g,b,alpha,fallback)
local L,a,bb = rgb_to_oklab(r,g,b)
local best,bestD = fallback,nil
for k=1,#cand do
local c = cand[k]
local dL,da,db = L-c.L,a-c.a,bb-c.b
-- keep a semi-transparent entry from stealing an opaque pixel
local dA = (alpha-c.alpha)/255
local d = dL*dL+da*da+db*db+dA*dA
if bestD==nil or d<bestD then
bestD,best = d,c.idx
end
end
return best
end
-- memorise index->index
local function make_index_shifter(palette,transparentIdx,hue,chroma,light)
local cand = palette_candidates(palette,transparentIdx)
local n = #palette
local cache = {}
return function(px)
local out = cache[px]
if out==nil then
out = px
-- px>=n means the image points outside its own palette, leave it alone
if px~=transparentIdx and px<n then
local c = palette:getColor(px)
if c.alpha>0 then
local nr,ng,nb = shift_color(c.red,c.green,c.blue,hue,chroma,light)
out = nearest_index(cand,nr,ng,nb,c.alpha,px)
end
end
cache[px] = out
end
return out
end
end
local function process_image(image,celPos,sel,shift)
local x0,y0,x1,y1 = 0,0,image.width,image.height
-- clip to the selection bounding box
if sel then
local b = sel.bounds
x0 = math.max(x0,b.x-celPos.x)
y0 = math.max(y0,b.y-celPos.y)
x1 = math.min(x1,b.x+b.width-celPos.x)
y1 = math.min(y1,b.y+b.height-celPos.y)
-- selection misses this cel entirely
if x1<=x0 or y1<=y0 then return end
end
for it in image:pixels(Rectangle(x0,y0,x1-x0,y1-y0)) do
if not sel or sel:contains(it.x+celPos.x,it.y+celPos.y) then
local px = it()
local out = shift(px)
-- skip transparent pipsis
if out~=px then it(out) end
end
end
end
local function is_identity(hue,chroma,light)
return hue==0 and chroma==1 and light==0
end
local function target_cels(allCels)
local cels = {}
if allCels and #app.range.cels>0 then
for _,c in ipairs(app.range.cels) do
table.insert(cels,c)
end
elseif app.activeCel then
table.insert(cels,app.activeCel)
end
-- tilemap cels hold tile indices, not colors!!!!!
local out = {}
for _,c in ipairs(cels) do
if not c.layer.isTilemap then
table.insert(out,c)
end
end
return out
end
local function active_selection(sprite,selOnly)
if selOnly and not sprite.selection.isEmpty then
return sprite.selection
end
return nil
end
-- which palette does a frame paint with? a sprite can carry several, each one owning frames from its own frame onwards.
-- NEVER ipairs() over sprite
local function palette_frames(pals)
if #pals<2 then return nil end
local ok,frames = pcall(function()
local f = {}
for p=1,#pals do
local fr = pals[p].frame
f[p] = fr and fr.frameNumber or 1
end
return f
end)
if ok then return frames end
return nil -- old aseprite without Palette.frame, just use the first one
end
-- the palette with the highest start frame at or before this one. ties go to the earlier palette, and it doesn't assume they came in frame order
local function palette_for_frame(frames,frameNumber)
local p,best = 1,nil
if frames then
for i=1,#frames do
if frames[i]<=frameNumber and (best==nil or frames[i]>best) then
p,best = i,frames[i]
end
end
end
return p
end
-- hands out the right shifter for a given cel
local function make_shifter_factory(sprite,hue,chroma,light)
if sprite.colorMode~=ColorMode.INDEXED then
local shift = make_shifter(hue,chroma,light)
return function() return shift end
end
local transparentIdx = sprite.transparentColor
local pals = sprite.palettes
local frames = palette_frames(pals)
local byPalette = {}
return function(cel)
local p = palette_for_frame(frames,cel.frameNumber)
local shift = byPalette[p]
if not shift then
shift = make_index_shifter(pals[p],transparentIdx,hue,chroma,light)
byPalette[p] = shift
end
return shift
end
end
-- applyyyy
local function apply_impl(sprite,hue,chroma,light,selOnly,allCels)
local sel = active_selection(sprite,selOnly)
local shifterFor = make_shifter_factory(sprite,hue,chroma,light)
for _,cel in ipairs(target_cels(allCels)) do
local img = cel.image:clone()
process_image(img,cel.position,sel,shifterFor(cel))
cel.image = img
end
end
-- ui
local function show_dialog(plugin)
local sprite = app.activeSprite
if not sprite then
app.alert("No active sprite!")
return
end
if sprite.colorMode==ColorMode.GRAY then
app.alert("Grayscale mode has no hue to shift :(")
return
end
-- restore last-used toggles (not sliders!!!!!)
local prefs = plugin.preferences
if prefs.selOnly==nil then prefs.selOnly = true end
if prefs.allCels==nil then prefs.allCels = false end
if prefs.livePreview==nil then prefs.livePreview = true end
local dlg
-- preview. raw byte writes, so the undo stack is skipüped
local snapCels = nil -- {{cel=cel,bytes=string},...}
local function clear_preview()
if snapCels then
for _,s in ipairs(snapCels) do
s.cel.image.bytes = s.bytes
end
snapCels = nil
end
end
local function do_preview()
-- always rewind the previous preview first; each preview is fresh from the original
clear_preview()
local d = dlg.data
local hue = d.hue
local chroma = d.chroma/100
local light = d.light/100
if is_identity(hue,chroma,light) then
app.refresh()
return
end
local sel = active_selection(sprite,d.selOnly)
local shifterFor = make_shifter_factory(sprite,hue,chroma,light)
snapCels = {}
for _,cel in ipairs(target_cels(d.allCels)) do
table.insert(snapCels,{cel=cel,bytes=cel.image.bytes})
process_image(cel.image,cel.position,sel,shifterFor(cel))
end
app.refresh()
end
-- throttle preview fires so it doesn't preview every frame and explodes your shit
local dirty = false
local timer
if Timer then
timer = Timer{interval=0.05,ontick=function()
if dirty then
dirty = false
do_preview()
else
timer:stop()
end
end}
end
local function on_change()
if not dlg.data.livePreview then
dirty = false
clear_preview()
app.refresh()
return
end
if not timer then
do_preview() -- no timer before v1.3
return
end
dirty = true
if not timer.isRunning then
timer:start()
end
end
-- dialog ui
dlg = Dialog{title="OKShift"}
dlg:slider{id="hue",label="Hue (°)",min=-180,max=180,value=0,onchange=on_change}
dlg:slider{id="chroma",label="Chroma (%)",min=0,max=200,value=100,onchange=on_change}
dlg:slider{id="light",label="Lightness ±",min=-50,max=50,value=0,onchange=on_change}
dlg:separator()
dlg:check{id="selOnly",label="Selection only",selected=prefs.selOnly,onclick=on_change}
dlg:check{id="allCels",label="All cels in timeline range",selected=prefs.allCels,onclick=on_change}
dlg:separator()
dlg:check{id="livePreview",label="Live preview",selected=prefs.livePreview,onclick=on_change}
dlg:separator()
dlg:button{id="ok",text="Apply",focus=true}
dlg:button{id="cancel",text="Cancel"}
dlg:show{wait=true}
if timer then timer:stop() end
-- the preview never did anything to the undo stack, so clearing it is always ok here
clear_preview()
if dlg.data.ok then
prefs.selOnly = dlg.data.selOnly
prefs.allCels = dlg.data.allCels
prefs.livePreview = dlg.data.livePreview
local hue = dlg.data.hue
local chroma = dlg.data.chroma/100
local light = dlg.data.light/100
if not is_identity(hue,chroma,light) then
app.transaction("OKShift",function()
apply_impl(sprite,hue,chroma,light,dlg.data.selOnly,dlg.data.allCels)
end)
end
end
app.refresh()
end
-- plugin entry
function init(plugin)
plugin:newCommand{
id="OKShift",
title="OKShift",
group="edit_fx",
onclick=function()
show_dialog(plugin)
end,
onenabled=function()
return app.activeSprite~=nil
end
}
end
function exit(plugin)
end