Here's a function that reads GIMP Color Palettes files
Posted: Wed Mar 27, 2024 12:27 am
Compliments of ChatGPT after I corrected it a couple of times:
Code: Select all
function readGIMPColorPaletteFile pFilePath
local tPaletteName, tColumns, tColors
put empty into tPaletteName
put empty into tColumns
put empty into tColors
-- Read the file contents
open file pFilePath for binary read -- added 'Binary' just to be safe, any names should be Unicode
read from file pFilePath until eof
put it into tFileContent
close file pFilePath
-- Process each line
repeat with each line tLine in tFileContent
if tLine starts with "GIMP Palette" then
-- Header line, skip
next repeat
else if tLine starts with "Name: " then
-- Extract palette name
put word 2 to -1 of tLine into tPaletteName
else if tLine starts with "Columns: " then
-- Extract number of columns
put word 2 of tLine into tColumns
else if tLine is not empty and char 1 of tLine is not "#" then
-- Parse color description line
put word 1 of tLine into tRed
put word 2 of tLine into tGreen
put word 3 of tLine into tBlue
put tRed & "," & tGreen & "," & tBlue into tColorRGB
if the number of words of tLine > 3 then
-- Extract color name
put word 4 to -1 of tLine into tColorName
put tColorRGB & tab & tColorName & return after tColors -- use tab to delimit color from any color name
else
put tColorRGB & return after tColors
end if
end if
end repeat
-- Return the extracted data
return tPaletteName & return & tColumns & return & tColors
end readGIMPColorPaletteFile
I haven't actually tested this yet but it's not much to read through and it looks correct, based on GIMP's spec for this format here: https://developer.gimp.org/core/standards/gpl/This version reads the file content directly into tFileContent using read from file without splitting it into an array. Then, it iterates through each line of tFileContent to extract the palette name, number of columns, and color information as before. The result is returned as a concatenated string.