Here's a function that reads GIMP Color Palettes files

Organizing tasks to work on, New Features Ideas, Building LCS & LCB Libraries & Widgets, Redecorating and Modifying the IDE, Hacking / Editing Tools, Compiling the Engine from Source, etc.
Post Reply
User avatar
OpenXTalkPaul
Posts: 1871
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Here's a function that reads GIMP Color Palettes files

Post by OpenXTalkPaul »

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
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.
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/
User avatar
richmond62
Posts: 3264
Joined: Sun Sep 12, 2021 11:03 am
Location: Bulgaria
Contact:

Re: Here's a function that reads GIMP Color Palettes files

Post by richmond62 »

OK: here's the predictably goofy question:

What might you do with the information read from a GIMP colour palette?
https://richmondmathewson.owlstown.net/
xAction
Posts: 285
Joined: Thu Sep 16, 2021 1:40 pm
Contact:

Re: Here's a function that reads GIMP Color Palettes files

Post by xAction »

Step 1 in reading a gimp .xcf document file (full of layers and such), so that:
  1. import GIF to gimp as layers
  2. use Gimp tools to manipulate animation frame properties
  3. save .xcf
  4. load into OXT
  5. test
  6. return to open Gimp document to adjust properties
  7. repeat until you have the results you want.
User avatar
OpenXTalkPaul
Posts: 1871
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Re: Here's a function that reads GIMP Color Palettes files

Post by OpenXTalkPaul »

richmond62 wrote: Wed Mar 27, 2024 7:59 am OK: here's the predictably goofy question:

What might you do with the information read from a GIMP colour palette?
Well I've already manually imported a few of these palettes into OXT to use as color sets, such as the Mac II 16 color set available in my Color Swatch palette.
And these sets can be to applied to index color images (so GIF and TIFF), replacing an images index color palette with a different set of colors. For example you can nearly instantly convert a color image to a grayscale, duo-tone, tri-tone, sepiatone, etc. image simply by swapping out the color palette, as demonstrated in the GIF Parser demos I've recently been posting.
This script just automates converting GIMP format color lists to the comma separated "R,G,B" (as preferred for xTalk scripts) + optional color name lists. I'd like to be able to just dump them into color swatches folder and have the Color Swatch palette read them as needed.
GIMP's .gpl format is one of the formats available from this site: https://lospec.com
User avatar
OpenXTalkPaul
Posts: 1871
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Re: Here's a function that reads GIMP Color Palettes files

Post by OpenXTalkPaul »

Well ChatGPT got a few syntax things wrong as usual (so don't use that first post's script) but changing a few 'keywords' to valid syntax quickly did the trick.

Here's a working demo stack (I put this together in between sipping on boat drinks in the Caribbean last week :-) ) that shows all of a color palette's colors as an image (1 pixel per color and then scaled to larger size).
GIMP Color Palette read.oxtstack
(35.42 KiB) Downloaded 38 times
GIMPREAD.jpg
GIMPREAD.jpg (73.63 KiB) Viewed 1698 times
And here is a zip file containing a few GIMP color palette format files to test it with
GIMP_Color_Palettes.zip
(67.37 KiB) Downloaded 48 times
That's just a few, but I have a folder that currently some 724 (with a few duplicates) of these color palette files.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests