2024-07-30 01:25:14 +00:00
|
|
|
-- Functions for modifying a sprite
|
2024-07-30 03:22:57 +00:00
|
|
|
|
|
|
|
local BASE_COLOR = Color {
|
|
|
|
r = 0,
|
|
|
|
g = 0,
|
|
|
|
b = 0,
|
2024-07-30 22:57:17 +00:00
|
|
|
a = 0,
|
2024-07-30 03:22:57 +00:00
|
|
|
}
|
|
|
|
local IGNORE_COLOR = Color {
|
2024-07-30 22:57:17 +00:00
|
|
|
gray = 100,
|
|
|
|
alpha = 254,
|
2024-07-30 03:22:57 +00:00
|
|
|
}
|
|
|
|
local IGNORE_SUBCOLOR = Color {
|
2024-07-30 22:57:17 +00:00
|
|
|
gray = 150,
|
|
|
|
alpha = 254,
|
2024-07-30 03:22:57 +00:00
|
|
|
}
|
|
|
|
local MERGE_COLOR = Color {
|
|
|
|
r = 200,
|
|
|
|
g = 200,
|
2024-07-30 22:57:17 +00:00
|
|
|
b = 0,
|
|
|
|
a = 254,
|
2024-07-30 03:22:57 +00:00
|
|
|
}
|
|
|
|
local MERGE_SUBCOLOR = Color {
|
|
|
|
r = 200,
|
|
|
|
g = 200,
|
2024-07-30 22:57:17 +00:00
|
|
|
b = 128,
|
|
|
|
a = 254,
|
2024-07-30 03:22:57 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 01:25:14 +00:00
|
|
|
-- Deletes any layers with the 'ignored' property.
|
2024-07-30 22:57:17 +00:00
|
|
|
local function DeleteLayers(spr, layers)
|
|
|
|
for _, layer in ipairs(layers) do
|
2024-07-30 01:25:14 +00:00
|
|
|
if layer.properties(extKey).ignored then
|
|
|
|
spr:deleteLayer(layer)
|
|
|
|
elseif layer.isGroup then
|
2024-07-30 22:57:17 +00:00
|
|
|
DeleteLayers(spr, layer.layers)
|
2024-07-30 01:25:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Flattens any layers that have the 'exportedAsSprite' property.
|
|
|
|
-- Should be called after deleteLayers.
|
2024-07-30 22:57:17 +00:00
|
|
|
local function FlattenLayers(layers)
|
|
|
|
for _, layer in ipairs(layers) do
|
2024-07-30 01:25:14 +00:00
|
|
|
if not layer.isGroup then
|
|
|
|
goto continue
|
|
|
|
end
|
|
|
|
|
|
|
|
if layer.properties(extKey).exportedAsSprite then
|
|
|
|
app.range.layers = {layer}
|
|
|
|
app.command.FlattenLayers(false)
|
|
|
|
else
|
|
|
|
-- recurse
|
2024-07-30 22:57:17 +00:00
|
|
|
FlattenLayers(layer.layers)
|
2024-07-30 01:25:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
::continue::
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Makes all layers visible.
|
|
|
|
-- This should be called after deleteLayers and flattenLayers
|
2024-07-30 22:57:17 +00:00
|
|
|
local function RevealLayers(layers)
|
|
|
|
for _, layer in ipairs(layers) do
|
2024-07-30 01:25:14 +00:00
|
|
|
if layer.isGroup then
|
2024-07-30 22:57:17 +00:00
|
|
|
RevealLayers(layer.layers)
|
2024-07-30 01:25:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if not layer.isVisible then
|
|
|
|
layer.isVisible = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-30 22:57:17 +00:00
|
|
|
local function safeSetColor(layer, color)
|
|
|
|
pixelValue = layer.color.rgbaPixel
|
|
|
|
|
|
|
|
if (pixelValue ~= BASE_COLOR.rgbaPixel and
|
|
|
|
pixelValue ~= IGNORE_COLOR.rgbaPixel and
|
|
|
|
pixelValue ~= IGNORE_SUBCOLOR.rgbaPixel and
|
|
|
|
pixelValue ~= MERGE_COLOR.rgbaPixel and
|
|
|
|
pixelValue ~= MERGE_SUBCOLOR.rgbaPixel) then
|
|
|
|
print("DEBUG: not setting color")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
layer.color = color
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set the color of a layer and its sublayers based on the extension properties
|
|
|
|
local function SetColor(layer, subColor)
|
2024-07-30 03:22:57 +00:00
|
|
|
if (layer.properties(extKey).ignored) then
|
2024-07-30 22:57:17 +00:00
|
|
|
safeSetColor(layer, IGNORE_COLOR)
|
2024-07-30 03:22:57 +00:00
|
|
|
subColor = IGNORE_SUBCOLOR
|
|
|
|
elseif subColor == IGNORE_SUBCOLOR then
|
2024-07-30 22:57:17 +00:00
|
|
|
safeSetColor(layer, subColor)
|
2024-07-30 03:22:57 +00:00
|
|
|
elseif (layer.properties(extKey).exportedAsSprite) then
|
2024-07-30 22:57:17 +00:00
|
|
|
safeSetColor(layer, MERGE_COLOR)
|
2024-07-30 03:22:57 +00:00
|
|
|
subColor = MERGE_SUBCOLOR
|
|
|
|
elseif subColor == MERGE_SUBCOLOR then
|
2024-07-30 22:57:17 +00:00
|
|
|
safeSetColor(layer, subColor)
|
2024-07-30 03:22:57 +00:00
|
|
|
else
|
2024-07-30 22:57:17 +00:00
|
|
|
safeSetColor(layer, BASE_COLOR)
|
2024-07-30 03:22:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if (layer.isGroup) then
|
|
|
|
for i, sublayer in ipairs(layer.layers) do
|
2024-07-30 22:57:17 +00:00
|
|
|
SetColor(sublayer, subColor)
|
2024-07-30 03:22:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-30 22:57:17 +00:00
|
|
|
-- Find the root of the layer stack, then set colors appropriately
|
|
|
|
-- for all children
|
|
|
|
local function SetColorFromRoot(layer)
|
|
|
|
-- The standard Lua `if table["field"] == nil` throws an error in Aseprite.
|
|
|
|
-- So we just check for the parent Layer being equal to the sprite.
|
|
|
|
if layer.parent == layer.sprite then
|
|
|
|
SetColor(layer)
|
|
|
|
else
|
|
|
|
SetColorFromRoot(layer.parent)
|
|
|
|
end
|
|
|
|
end
|
2024-07-30 03:22:57 +00:00
|
|
|
|
2024-07-30 01:25:14 +00:00
|
|
|
local export = {
|
2024-07-30 22:57:17 +00:00
|
|
|
DeleteLayers = DeleteLayers,
|
|
|
|
FlattenLayers = FlattenLayers,
|
|
|
|
RevealLayers = RevealLayers,
|
|
|
|
SetColor = SetColor,
|
|
|
|
SetColorFromRoot = SetColorFromRoot,
|
2024-07-30 01:25:14 +00:00
|
|
|
}
|
|
|
|
return export
|