-
-
Save JakobOvrum/a5bdfabce12c48e437ec to your computer and use it in GitHub Desktop.
Scripts for converting between PaintedBiomes configuration files and GIMP palette files (.gpl)
This file contains 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
-- arguments | |
local palettePath = select(1, ...) | |
local configPath = select(2, ...) | |
if not (palettePath and configPath) then | |
io.stderr:write("Usage: gpl_to_paintedbiomes.lua <path to GIMP palette file (.gpl)> <path to PaintedBiomes config file>") | |
return | |
end | |
-- load palette | |
local palette = {} | |
local lines = io.lines(palettePath) | |
-- skip 4 first lines | |
for i = 1, 4 do | |
lines() | |
end | |
local errorOccurred = false | |
-- read palette | |
for line in lines do | |
if not line:match("^%s*$") then | |
local r, g, b, name = line:match("^%s*(%d+)%s*(%d+)%s*(%d+)%s*(.-)%s*$") | |
r, g, b = tonumber(r), tonumber(g), tonumber(b) | |
if not (r == 0 and g == 0 and b == 0) and | |
not (r == 255 and g == 255 and b == 255) then | |
local htmlColor = ("%02X%02X%02X"):format(r, g, b) | |
local existing = palette[htmlColor] | |
if existing then | |
io.stderr:write(("color %s [#%s] is already in the palette as %s\n"):format(name, htmlColor, existing)) | |
errorOccurred = true | |
else | |
palette[htmlColor] = name | |
end | |
end | |
end | |
end | |
if errorOccurred then | |
return | |
end | |
-- key palette table by name now that it's verified to only have unique colors | |
local paletteByName = {} | |
for htmlColor, name in pairs(palette) do | |
paletteByName[name] = htmlColor | |
end | |
-- Read config and replace colors mappings with colors from palette | |
local colorMap = {} | |
local configLines = {} | |
for line in io.lines(configPath) do | |
local name, color = line:match("^%s*S:([^=]+)=(.+)$") | |
if name then | |
local strippedName = name:match('"([^"]+)"') or name | |
local newColor = paletteByName[strippedName] | |
if newColor then | |
table.insert(configLines, (" S:%s=%s"):format(name, newColor)) | |
color = newColor | |
paletteByName[strippedName] = "deleted" | |
else | |
table.insert(configLines, line) | |
end | |
local existing = colorMap[color] | |
if existing and color ~= "000000" then | |
io.stderr:write(("Bad color for %s: color %s is already occupied by biome %s\n"):format(name, color, existing)) | |
errorOccurred = true | |
else | |
colorMap[color] = name | |
end | |
else | |
table.insert(configLines, line) | |
end | |
end | |
-- Look for palette colors that didn't have a corresponding biome entry | |
for name, color in pairs(paletteByName) do | |
if color ~= "deleted" then | |
io.stderr:write(('Biome with palette color "%s" was not detected in config'):format(name)) | |
errorOccurred = true | |
end | |
end | |
if not errorOccurred then | |
print(table.concat(configLines, "\n")) | |
end |
This file contains 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
GIMP Palette | |
Name: Minecraft Biomes | |
Columns: 16 | |
# | |
250 222 85 Beach | |
48 116 68 Birch Forest | |
31 95 50 Birch Forest Hills | |
71 135 90 Birch Forest Hills M | |
88 156 108 Birch Forest M | |
250 240 192 Cold Beach | |
49 85 74 Cold Taiga | |
36 63 54 Cold Taiga Hills | |
89 125 114 Cold Taiga M | |
0 0 48 Deep Ocean | |
250 148 24 Desert | |
255 188 64 Desert M | |
210 95 18 DesertHills | |
96 96 96 Extreme Hills | |
114 120 154 Extreme Hills Edge | |
136 136 136 Extreme Hills M | |
80 112 80 Extreme Hills+ | |
120 152 120 Extreme Hills+ M | |
45 142 73 Flower Forest | |
5 102 33 Forest | |
34 85 28 ForestHills | |
144 144 160 FrozenOcean | |
160 160 255 FrozenRiver | |
255 0 0 Hell | |
160 160 160 Ice Mountains | |
255 255 255 Ice Plains | |
180 220 220 Ice Plains Spikes | |
83 123 9 Jungle | |
123 163 49 Jungle M | |
98 139 23 JungleEdge | |
138 179 63 JungleEdge M | |
44 66 5 JungleHills | |
109 119 102 Mega Spruce Taiga | |
89 102 81 Mega Taiga | |
69 79 62 Mega Taiga Hills | |
217 69 21 Mesa | |
255 109 61 Mesa (Bryce) | |
202 140 101 Mesa Plateau | |
176 151 101 Mesa Plateau F | |
216 191 141 Mesa Plateau F M | |
242 180 141 Mesa Plateau M | |
255 0 255 MushroomIsland | |
160 0 255 MushroomIslandShore | |
0 0 112 Ocean | |
141 179 96 Plains | |
0 0 255 River | |
64 81 26 Roofed Forest | |
104 121 66 Roofed Forest M | |
189 178 95 Savanna | |
229 218 135 Savanna M | |
167 157 100 Savanna Plateau | |
207 197 140 Savanna Plateau M | |
128 128 255 Sky | |
162 162 132 Stone Beach | |
181 219 136 Sunflower Plains | |
7 249 178 Swampland | |
47 255 218 Swampland M | |
11 102 89 Taiga | |
51 142 129 Taiga M | |
22 57 51 TaigaHills |
This file contains 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
local configPath = select(1, ...) | |
if configPath == nil then | |
io.stderr:write("Usage: paintedbiomes_to_gpl.lua <path to PaintedBiomes config file>\n") | |
return | |
end | |
local paletteLines = { | |
"GIMP Palette"; | |
"Name: Minecraft Biomes"; | |
"Columns: 16"; | |
"#"; | |
} | |
for line in io.lines(configPath) do | |
local name, color = line:match("^%s*S:([^=]+)=(.+)$") | |
if name then | |
name = name:match('"([^"]+)"') or name | |
local r, g, b = tonumber(color:sub(1, 2), 16), tonumber(color:sub(3, 4), 16), tonumber(color:sub(5, 6), 16) | |
table.insert(paletteLines, ("%3s %3s %3s %s"):format(r, g, b, name)) | |
end | |
end | |
print(table.concat(paletteLines, "\n")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment