Last active
November 8, 2024 18:33
-
-
Save zherring/cff25bf22916a50473906277d525358a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- main.lua | |
| local function createColorIndices(sprite, cel) | |
| local colorToIndex = {} -- Maps color values to indices | |
| local nextIndex = 0 -- Next available index | |
| -- First pass: assign indices to unique colors | |
| local img = cel.image | |
| for pixel in img:pixels() do | |
| local colorValue = pixel() | |
| if app.pixelColor.rgbaA(colorValue) > 0 then | |
| if not colorToIndex[colorValue] then | |
| colorToIndex[colorValue] = nextIndex | |
| nextIndex = nextIndex + 1 | |
| end | |
| end | |
| end | |
| -- Second pass: create pixel data with indices | |
| local pixelData = {} | |
| for pixel in img:pixels() do | |
| local colorValue = pixel() | |
| if app.pixelColor.rgbaA(colorValue) > 0 then | |
| table.insert(pixelData, { | |
| x = pixel.x, | |
| y = pixel.y, | |
| color = colorToIndex[colorValue] | |
| }) | |
| end | |
| end | |
| return pixelData | |
| end | |
| -- Function to detect operating system | |
| local function getOS() | |
| if package.config:sub(1,1) == '\\' then | |
| return "Windows" | |
| else | |
| return "Mac" | |
| end | |
| end | |
| -- Function to copy data (with fallback) | |
| local function copyData(json_string) | |
| -- Try clipboard first | |
| local success = pcall(function() | |
| local command = package.config:sub(1,1) == '\\' and 'clip' or 'pbcopy' | |
| local clip = io.popen(command, 'w') | |
| clip:write(json_string) | |
| clip:close() | |
| end) | |
| -- If clipboard fails, save to file | |
| if not success then | |
| local scriptPath = debug.getinfo(1).source:match("@?(.*[/\\])") | |
| local filePath = scriptPath .. "output.json" | |
| local file = io.open(filePath, "w") | |
| if file then | |
| file:write(json_string) | |
| file:close() | |
| app.alert("JSON saved to: " .. filePath) | |
| else | |
| app.alert("Failed to save file!") | |
| end | |
| else | |
| app.alert("Copied to clipboard!") | |
| end | |
| end | |
| -- Store dialog globally so we can refresh it | |
| local dlg = nil | |
| -- Function to get unique colors and their indices | |
| local function getUniqueColors() | |
| local sprite = app.activeSprite | |
| if not sprite then return {} end | |
| local cel = sprite.cels[1] | |
| if not cel then return {} end | |
| local colorToIndex = {} | |
| local colorList = {} | |
| local nextIndex = 0 | |
| -- Get unique colors | |
| local img = cel.image | |
| for pixel in img:pixels() do | |
| local colorValue = pixel() | |
| if app.pixelColor.rgbaA(colorValue) > 0 then | |
| if not colorToIndex[colorValue] then | |
| colorToIndex[colorValue] = nextIndex | |
| table.insert(colorList, { | |
| color = colorValue, | |
| index = nextIndex | |
| }) | |
| nextIndex = nextIndex + 1 | |
| end | |
| end | |
| end | |
| return colorList | |
| end | |
| -- Function to create and show the dialog | |
| local function createDialog() | |
| if dlg then | |
| dlg:close() | |
| end | |
| dlg = Dialog{ | |
| title = "BasePaintPaint" | |
| } | |
| -- Add refresh button at the top | |
| dlg:button{ | |
| text = "↻ Refresh Colors", | |
| onclick = function() | |
| createDialog() | |
| end | |
| } | |
| dlg:newrow() | |
| dlg:separator() | |
| -- Get unique colors | |
| local colors = getUniqueColors() | |
| for _, colorInfo in ipairs(colors) do | |
| dlg:newrow() | |
| dlg:color{ | |
| color = Color{ | |
| r = app.pixelColor.rgbaR(colorInfo.color), | |
| g = app.pixelColor.rgbaG(colorInfo.color), | |
| b = app.pixelColor.rgbaB(colorInfo.color), | |
| a = app.pixelColor.rgbaA(colorInfo.color) | |
| } | |
| } | |
| dlg:label{ | |
| text = "Index: " .. colorInfo.index | |
| } | |
| end | |
| dlg:newrow() | |
| dlg:separator() | |
| -- Add copy button at the bottom | |
| dlg:button{ | |
| text = "Copy Pixels", | |
| onclick = function() | |
| local sprite = app.activeSprite | |
| if not sprite then | |
| app.alert("No sprite is active!") | |
| return | |
| end | |
| local cel = sprite.cels[1] | |
| local pixelData = createColorIndices(sprite, cel) | |
| -- Create JSON string | |
| local json_string = "[" | |
| for i, pixel in ipairs(pixelData) do | |
| json_string = json_string .. string.format( | |
| "{\"point\":{\"x\":%d,\"y\":%d},\"color\":%d}%s", | |
| pixel.x, pixel.y, pixel.color, | |
| i < #pixelData and "," or "" | |
| ) | |
| end | |
| json_string = json_string .. "]" | |
| -- Use the new copyData function | |
| copyData(json_string) | |
| end | |
| } | |
| dlg:show{ | |
| wait = false | |
| } | |
| end | |
| -- Initial dialog creation | |
| createDialog() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment