-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpalettize.lua
More file actions
582 lines (555 loc) · 16.5 KB
/
palettize.lua
File metadata and controls
582 lines (555 loc) · 16.5 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
--
-- Apply a palette to an image.
--
-- Usage (first time):
-- 1) Open Aseprite
-- 2) Go to File > Scripts > Open scripts folder
-- 3) Copy this file to the scripts folder
-- 4) In Aseprite, go to File > Scripts > Rescan scripts folder
-- 5) Run the script: File > Scripts > palettize
--
-- Usage:
-- 1) File > Scripts > palettize
--
local sprite = app.activeSprite
if sprite == nil then
return app.alert("ERROR: There is no active sprite")
end
if sprite.colorMode == ColorMode.Tilemap then
return app.alert("ERROR: Tilemap color mode not supported")
end
if sprite.colorMode ~= ColorMode.RGB then
app.command.ChangePixelFormat{ format="rgb" }
end
local image = sprite.cels[1].image
if image == nil then
return app.alert("ERROR: The active sprite has no image")
end
local sourceColors = { Color{ r=33, g=30, b=32 }, Color{ r=85, g=85, b=104 }, Color{ r=160, g=160, b=139 }, Color{ r=233, g=239, b=236 }}
local previewScale = math.floor((200 / image.width) + 0.5) -- Preview as close to 200 pix as possible
if previewScale < 1 then
previewScale = 1
elseif previewScale > 8 then
previewScale = 8
end
local adjustGlobal = {h=0, s=0, l=0}
local adjustR = {h=0, s=0, l=0}
local adjustY = {h=0, s=0, l=0}
local adjustG = {h=0, s=0, l=0}
local adjustC = {h=0, s=0, l=0}
local adjustB = {h=0, s=0, l=0}
local adjustP = {h=0, s=0, l=0}
local adjustType = "global"
local palette = Palette(#sourceColors)
for i=1, #sourceColors, 1 do
palette:setColor(i-1, sourceColors[i])
end
local paletteExclusions = {}
local prevImageA = Image(image.width, image.height, ColorMode.RGB)
local prevImageB = Image(image.width, image.height, ColorMode.RGB)
local showPalette = true
local swatchPerRow = 32
getActiveAdjust = function(type)
if type == nil then
type = adjustType
end
if type == "global" then
return adjustGlobal
elseif type == "red" then
return adjustR
elseif type == "yellow" then
return adjustY
elseif type == "green" then
return adjustG
elseif type == "cyan" then
return adjustC
elseif type == "blue" then
return adjustB
elseif type == "purple" then
return adjustP
end
return nil
end
-- Load a palette from a png file
loadPalette = function(data)
if data.src then
palette = Palette{ fromFile=data.src }
end
if palette ~= nil then
sourceColors = {}
for i=0, #palette - 1, 1 do
local color = palette:getColor(i)
table.insert(sourceColors, Color{ r=color.red, g=color.green, b=color.blue })
end
end
end
-- Linear interpolation between first and second
function lerp(first, second, by)
return first * (1 - by) + second * by
end
-- Shift a color by hue, saturation and lightness
function colorShift(color, type)
local f = 1
local v = getActiveAdjust(type)
if type ~= "global" then
local h = 0
if type == "red" then
h = 0
elseif type == "yellow" then
h = 60
elseif type == "green" then
h = 120
elseif type == "cyan" then
h = 180
elseif type == "blue" then
h = 240
elseif type == "purple" then
h = 300
end
local d = 360 - math.abs(color.hslHue - h)
if d < 240 then
f = 0
else
f = (d - 240) / 120
end
end
local hueShift = v.h * f / 8
local satShift = v.s * f / 100
local lightShift = v.l * f / 200
local newColor = Color(color)
newColor.hslHue = (newColor.hslHue + hueShift) % 360
if (satShift > 0) then
newColor.saturation = lerp(newColor.saturation, 1, satShift)
elseif (satShift < 0) then
newColor.saturation = lerp(newColor.saturation, 0, -satShift)
end
if (lightShift > 0) then
newColor.lightness = lerp(newColor.lightness, 1, lightShift)
elseif (lightShift < 0) then
newColor.lightness = lerp(newColor.lightness, 0, -lightShift)
end
return newColor
end
isExclusion = function(index, color)
for j=1, #paletteExclusions, 1 do
if paletteExclusions[j].index == index then
local exclColor = paletteExclusions[j].color
local exclDistance = math.abs(color.red - exclColor.red) + math.abs(color.green - exclColor.green) + math.abs(color.blue - exclColor.blue)
if (exclDistance / 765) < paletteExclusions[j].tolerance then
return true
end
end
end
return false
end
matchPaletteId = function(color)
if (color.alpha == 0) then
return Color{ r=0, g=0, b=0, a=0 }
end
local bestMatch = 0
local bestDistance = 999999
for i=1, #sourceColors, 1 do
local palColor = sourceColors[i]
local distance = math.abs(color.red - palColor.red) + math.abs(color.green - palColor.green) + math.abs(color.blue - palColor.blue)
if distance < bestDistance then
if not isExclusion(i, color) then
bestMatch = i
bestDistance = distance
end
end
end
return bestMatch
end
-- Find the closest color in the palette
matchPalette = function(color)
return sourceColors[matchPaletteId(color)]
end
-- Draw the exclusions as pairs of swatches
drawExclusions = function(context)
local w = #paletteExclusions * 24 + 4
local h = 16
context.antialias = true
context.strokeWidth = 1
context:beginPath()
context.color = app.theme.color.background
context:roundedRect(Rectangle(0.5,0.5, w-1, h-1), 2)
context:stroke()
context:beginPath()
context.color = app.theme.color.editor_sprite_border
context:roundedRect(Rectangle(1.5,1.5, w-3,h-3), 2)
context:stroke()
local x = 2
for i=1, #paletteExclusions, 1 do
context.color = paletteExclusions[i].color
context:fillRect(x, 2, 24, 12)
context.color = sourceColors[paletteExclusions[i].index]
context:beginPath()
context:moveTo(x, 14)
context:lineTo(x + 24, 14)
context:lineTo(x + 24, 2)
context:fill()
context.color = Color{ r=0, g=0, b=0 }
context.strokeWidth = 3
context:beginPath()
context:moveTo(x + 9.5, 4.5)
context:lineTo(x + 15.5, 10.5)
context:moveTo(x + 15.5, 4.5)
context:lineTo(x + 9.5, 10.5)
context:stroke()
context.color = Color{ r=255, g=0, b=0 }
context.strokeWidth = 1
context:beginPath()
context:moveTo(x + 9.5, 4.5)
context:lineTo(x + 15.5, 10.5)
context:moveTo(x + 15.5, 4.5)
context:lineTo(x + 9.5, 10.5)
context:stroke()
x = x + 24
end
end
-- Draw the preview images
-- The first has HSV applied
-- The second has HSV applied and is matched to the palette
drawPreviewImages = function(context)
local w = image.width
local h = image.height
for y=0, h-1, 1 do
for x=0, w-1, 1 do
local color = Color(image:getPixel(x, y))
color = colorShift(color, "global")
color = colorShift(color, "red")
color = colorShift(color, "yellow")
color = colorShift(color, "green")
color = colorShift(color, "cyan")
color = colorShift(color, "blue")
color = colorShift(color, "purple")
local palColor = matchPalette(color)
prevImageA:drawPixel(x, y, color)
prevImageB:drawPixel(x, y, palColor)
end
end
local sw = w * previewScale
local sh = h * previewScale
if context == nil then
return
end
context:drawImage(prevImageA, 0, 0, w, h, 0, 0, sw, sh)
context:drawImage(prevImageB, 0, 0, w, h, sw, 0, sw, sh)
end
-- Add an exclusion when user clicks on the preview canvas
addExclusionFromPreview = function(context, mx, my)
mx = math.floor(mx / previewScale);
my = math.floor(my / previewScale);
if (mx < image.width) then
return false
end
local exclColor = Color(prevImageA:getPixel(mx - image.width, my))
local match = matchPaletteId(exclColor)
-- If an exact match exists then exit
for i=1, #paletteExclusions, 1 do
if paletteExclusions[i].index == match and paletteExclusions[i].color == exclColor then
return false
end
end
table.insert(paletteExclusions, {
index = match,
color = exclColor,
tolerance = 0.1
})
return true
end
-- Remove an exclusion when user clicks on the exclusions canvas
removeExclusion = function(context, mx, my)
mx = mx - 2
my = my - 2
if (mx < 0 or mx >= #paletteExclusions * 24) then
return false
end
if (my < 0 or my >= 12) then
return false
end
local index = math.floor(mx / 24) + 1
table.remove(paletteExclusions, index)
return true
end
-- Apply the palette to the image and convert to indexed mode
applyPalette = function()
sprite.cels[1].image = prevImageB
sprite:setPalette(palette)
app.command.ChangePixelFormat{ format="indexed", dithering="none" }
app.refresh()
end
-- (re)draw the dialog
showDialog = function()
local dlg = Dialog{ title = "Palettize" }
dlg:file{ id="src", label="Source Palette",
label="Pallete",
title="Browse for palette file (png)",
open=true,
filetypes=".png",
onchange=function(ev)
loadPalette(dlg.data)
showPalette = true
showDialog()
dlg:close()
end
}
if showPalette then
local paletteRows = math.ceil(#sourceColors / swatchPerRow)
for i=0, paletteRows-1, 1 do
local row = {}
for j=1, swatchPerRow, 1 do
local index = i * swatchPerRow + j
if index <= #sourceColors then
table.insert(row, sourceColors[index])
end
end
dlg:shades{ id="palette"..i,
label="",
mode="pick",
colors=row,
hexpand=false,
onclick=function(ev)
for i=1, #sourceColors, 1 do
if sourceColors[i] == ev.color then
table.remove(sourceColors, i)
for j=1, #paletteExclusions, 1 do
if paletteExclusions[j].index == i then
table.remove(paletteExclusions, j)
break
end
end
break
end
end
showDialog()
dlg:close()
end
}
end
if swatchPerRow ~= 16 then
dlg:button{ id="narrowPalette",
text="Narrow (16)",
hexpand=false,
onclick=function(ev)
swatchPerRow = 16
showDialog()
dlg:close()
end
}
end
if swatchPerRow ~= 32 then
dlg:button{ id="mediumPalette",
text="Medium (32)",
hexpand=false,
onclick=function(ev)
swatchPerRow = 32
showDialog()
dlg:close()
end
}
end
if swatchPerRow ~= 64 then
dlg:button{ id="widePalette",
text="Wide (64)",
hexpand=false,
onclick=function(ev)
swatchPerRow = 64
showDialog()
dlg:close()
end
}
end
dlg:button{ id="hidePalette",
text="Hide",
hexpand=false,
onclick=function(ev)
showPalette = false
showDialog()
dlg:close()
end
}
else
dlg:button{ id="showPalette",
text="Show palette",
hexpand=false,
onclick=function(ev)
showPalette = true
showDialog()
dlg:close()
end
}
end
if #paletteExclusions > 0 then
dlg:canvas{ id="exclusions",
label = "Exclusions",
width = #paletteExclusions * 24 + 4,
height = 16,
onpaint=function(ev)
drawExclusions(ev.context)
end,
onmouseup=function(ev)
if removeExclusion(ev.context, ev.x, ev.y) then
showDialog()
dlg:close()
end
end
}
end
dlg:radio{ id="adjustTypeAll",
label="Adjust",
text="Global",
hexpand=false,
selected=adjustType == "global",
onclick=function(ev)
adjustType = "global"
showDialog()
dlg:close()
end
}
dlg:radio{ id="adjustTypeR",
text="Reds",
hexpand=false,
selected=adjustType == "red",
onclick=function(ev)
adjustType = "red"
showDialog()
dlg:close()
end
}
dlg:radio{ id="adjustTypeY",
text="Yellows",
hexpand=false,
selected=adjustType == "yellow",
onclick=function(ev)
adjustType = "yellow"
showDialog()
dlg:close()
end
}
dlg:radio{ id="adjustTypeG",
text="Greens",
hexpand=false,
selected=adjustType == "green",
onclick=function(ev)
adjustType = "green"
showDialog()
dlg:close()
end
}
dlg:radio{ id="adjustTypeC",
text="Cyans",
hexpand=false,
selected=adjustType == "cyan",
onclick=function(ev)
adjustType = "cyan"
showDialog()
dlg:close()
end
}
dlg:radio{ id="adjustTypeB",
text="Blues",
hexpand=false,
selected=adjustType == "blue",
onclick=function(ev)
adjustType = "blue"
showDialog()
dlg:close()
end
}
dlg:radio{ id="adjustTypeP",
text="Purples",
hexpand=false,
selected=adjustType == "purple",
onclick=function(ev)
adjustType = "purple"
showDialog()
dlg:close()
end
}
dlg:radio{ id="adjustTypeReset",
text="Reset all",
hexpand=false,
selected=adjustType == "reset",
onclick=function(ev)
adjustType = "global"
adjustGlobal = {h=0, s=0, l=0}
adjustR = {h=0, s=0, l=0}
adjustG = {h=0, s=0, l=0}
adjustB = {h=0, s=0, l=0}
showDialog()
dlg:close()
end
}
local adjust = getActiveAdjust()
dlg:slider{ id="hue",
label="Hue",
min=-100,
max=100,
value=adjust.h,
onrelease=function(ev)
local adjust = getActiveAdjust()
adjust.h = dlg.data.hue
showDialog()
dlg:close()
end
}
dlg:slider{ id="sat",
label="Saturation",
min=-100,
max=100,
value=adjust.s,
onrelease=function(ev)
local adjust = getActiveAdjust()
adjust.s = dlg.data.sat
showDialog()
dlg:close()
end
}
dlg:slider{ id="light",
label="Lightness",
min=-100,
max=100,
value=adjust.l,
onrelease=function(ev)
local adjust = getActiveAdjust()
adjust.l = dlg.data.light
showDialog()
dlg:close()
end
}
dlg:canvas{ id="preview",
width=image.width * previewScale * 2,
height=image.height * previewScale,
onpaint=function(ev)
drawPreviewImages(ev.context)
end,
onmouseup=function(ev)
if addExclusionFromPreview(ev.context, ev.x, ev.y) then
showDialog()
dlg:close()
end
end
}
dlg:combobox{ id="previewScale",
label="Preview scale",
hexpand=false,
option=tostring(previewScale.."x"),
options={ "1x", "2x", "3x", "4x", "5x", "6x", "7x", "8x" },
onchange=function(ev)
previewScale = tonumber(dlg.data.previewScale:sub(1, -2))
showDialog()
dlg:close()
end
}
dlg:button{ id="apply",
text="Convert to indexed mode and apply palette",
onclick=function(ev)
applyPalette()
dlg:close()
end
}
dlg:show{wait = false}
end
-- Run the script
showDialog(app.fgColor)