From 4f0f5dc8ada854195e6b36a012187f62b9709ec9 Mon Sep 17 00:00:00 2001 From: annabunches Date: Tue, 30 Jul 2024 18:57:17 -0400 Subject: [PATCH] First attempt at re-calculating all colors in a group hierarchy when one color changes, and also protecting user-defined colors. --- abase-commands.lua | 10 +++--- abase-sprite.lua | 83 +++++++++++++++++++++++++++++++--------------- readme.md | 3 +- 3 files changed, 63 insertions(+), 33 deletions(-) diff --git a/abase-commands.lua b/abase-commands.lua index b324fe3..b89745e 100644 --- a/abase-commands.lua +++ b/abase-commands.lua @@ -8,9 +8,9 @@ local function ExportSpritesheetAdvanced() local spr = Sprite(app.sprite) - sprt.deleteLayers(spr, spr.layers) - sprt.flattenLayers(spr.layers) - sprt.revealLayers(spr.layers) + sprt.DeleteLayers(spr, spr.layers) + sprt.FlattenLayers(spr.layers) + sprt.RevealLayers(spr.layers) app.command.ExportSpriteSheet { splitLayers = true @@ -26,7 +26,7 @@ local function ToggleIgnore() else layer.properties(extKey).ignored = true end - sprt.setColor(layer) + sprt.SetColorFromRoot(layer) end local function ToggleExportAsSprite() @@ -36,7 +36,7 @@ local function ToggleExportAsSprite() else layer.properties(extKey).exportedAsSprite = true end - sprt.setColor(layer) + sprt.SetColorFromRoot(layer) end local export = { diff --git a/abase-sprite.lua b/abase-sprite.lua index 34dc0b8..acc84fe 100644 --- a/abase-sprite.lua +++ b/abase-sprite.lua @@ -4,40 +4,44 @@ local BASE_COLOR = Color { r = 0, g = 0, b = 0, - a = 0 + a = 0, } local IGNORE_COLOR = Color { - gray = 100 + gray = 100, + alpha = 254, } local IGNORE_SUBCOLOR = Color { - gray = 150 + gray = 150, + alpha = 254, } local MERGE_COLOR = Color { r = 200, g = 200, - b = 0 + b = 0, + a = 254, } local MERGE_SUBCOLOR = Color { r = 200, g = 200, - b = 128 + b = 128, + a = 254, } -- Deletes any layers with the 'ignored' property. -local function deleteLayers(spr, layers) - for i, layer in ipairs(layers) do +local function DeleteLayers(spr, layers) + for _, layer in ipairs(layers) do if layer.properties(extKey).ignored then spr:deleteLayer(layer) elseif layer.isGroup then - deleteLayers(spr, layer.layers) + DeleteLayers(spr, layer.layers) end end end -- Flattens any layers that have the 'exportedAsSprite' property. -- Should be called after deleteLayers. -local function flattenLayers(layers) - for i, layer in ipairs(layers) do +local function FlattenLayers(layers) + for _, layer in ipairs(layers) do if not layer.isGroup then goto continue end @@ -47,7 +51,7 @@ local function flattenLayers(layers) app.command.FlattenLayers(false) else -- recurse - flattenLayers(layer.layers) + FlattenLayers(layer.layers) end ::continue:: @@ -56,10 +60,10 @@ end -- Makes all layers visible. -- This should be called after deleteLayers and flattenLayers -local function revealLayers(layers) - for i, layer in ipairs(layers) do +local function RevealLayers(layers) + for _, layer in ipairs(layers) do if layer.isGroup then - revealLayers(layer.layers) + RevealLayers(layer.layers) end if not layer.isVisible then @@ -68,35 +72,60 @@ local function revealLayers(layers) end end --- set the color of a layer and its sublayers -local function setColor(layer, subColor) +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) if (layer.properties(extKey).ignored) then - layer.color = IGNORE_COLOR + safeSetColor(layer, IGNORE_COLOR) subColor = IGNORE_SUBCOLOR elseif subColor == IGNORE_SUBCOLOR then - layer.color = subColor + safeSetColor(layer, subColor) elseif (layer.properties(extKey).exportedAsSprite) then - layer.color = MERGE_COLOR + safeSetColor(layer, MERGE_COLOR) subColor = MERGE_SUBCOLOR elseif subColor == MERGE_SUBCOLOR then - layer.color = subColor + safeSetColor(layer, subColor) else - layer.color = BASE_COLOR + safeSetColor(layer, BASE_COLOR) end if (layer.isGroup) then for i, sublayer in ipairs(layer.layers) do - setColor(sublayer, subColor) + SetColor(sublayer, subColor) end end end - +-- 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 local export = { - deleteLayers = deleteLayers, - flattenLayers = flattenLayers, - revealLayers = revealLayers, - setColor = setColor, + DeleteLayers = DeleteLayers, + FlattenLayers = FlattenLayers, + RevealLayers = RevealLayers, + SetColor = SetColor, + SetColorFromRoot = SetColorFromRoot, } return export \ No newline at end of file diff --git a/readme.md b/readme.md index fd792e4..eaad8d5 100644 --- a/readme.md +++ b/readme.md @@ -19,8 +19,9 @@ To install, go to Aseprite's Settings -> Extensions -> Add Extension, and select ## Additional Notes -* Toggling the advanced export settings on a layer will modify the layer colors. If you are using layer colors for other purposes, this extension will not work well for you. (This may be configurable in a future update) * Ignored layers always take precedence over merging; if a sublayer in a group is ignored, it will not be merged into the final sprite. +* Toggling the advanced export settings on a layer will modify the layer colors. The extension will attempt to detect and preserve user-colored layers. If you happen to use one of the exact colors we have chosen, this will fail. We have chosen odd alpha values to reduce the likelihood of a false negative, but if you are using layer colors extensively, this extension may not work well for you. +* To force a layer's color to be controlled by the extension, simply reset the layer's color to all 0 values. (red, green, blue, and alpha should all be 0) You may need to toggle the export settings of a parent layer or create a new layer before the changes take effect. ## Copyright Notice