Message Boards Message Boards

[✓] Export palette from Mathematica into .pal file

GROUPS:

Hi.

I would like to export a colour palette from Mathematica to Origin8.5. The Origin manual says that this requires a file in the format .pal (Microsoft palette format). I could not figure out so far how to create such file using Mathematica. I saw that someone did this with Matlab (see here: http://de.mathworks.com/matlabcentral/fileexchange/43114-cmap2pal-convert-matlab-colormap-to-binary-pal-format?requestedDomain=www.mathworks.com )

which actually works nicely. Anyone experience with such a thing? Any help would be greatly appreciated.

Thanks, Markus

POSTED BY: Markus Schmidt
Answer
13 days ago

May I ask why do you need that if you can plot most of things in Mathematica directly?

POSTED BY: Sam Carrettie
Answer
13 days ago

I followed this description: http://worms2d.info/Palette_file

SetDirectory[NotebookDirectory[]];
ClearAll[ExportPal]
ExportPal[fnout_String,colors_List]:=Module[{colorout,length,str},colorout=Select[colors,ColorQ];
    If[Length[colorout]>0,
        colorout=List@@@(ColorConvert[#,"RGB"]&/@colorout);
        colorout=Take[#,3]&/@colorout; (* remove alpha channel if present *)
        colorout=Append[0]/@Round[255 colorout];
        length=4+4+4+4+2+2+4Length[colors];
        str=OpenWrite[fnout,BinaryFormat->True];
        BinaryWrite[str,"RIFF"];
        BinaryWrite[str,length,"Integer32"];
        BinaryWrite[str,"PAL "];
        BinaryWrite[str,"data"];
        BinaryWrite[str,4Length[colors]+4,"Integer32"];
        BinaryWrite[str,16^^0300,"Integer16"];
        BinaryWrite[str,Length[colors],"Integer16"];
        BinaryWrite[str,Join@@colorout,"Byte"];
        Close[str]
    ,
        Print["plz check z colors, something wrong! kthxbye!"];
        Abort[]
    ]
]
ExportPal["out.pal",{RGBColor[0.5,0.1,0.8,0.1],Blue,Green,LABColor[0.33,-0.48,-0.54]}]

Unfortunately I don't have any software to test the pal file... so let me know if it works...

POSTED BY: Sander Huisman
Answer
13 days ago

Dear Sander. It works. This is very nice, thank you for the help. This tool will be very helpful for me in the future. Best, Markus

POSTED BY: Markus Schmidt
Answer
12 days ago

You're welcome! It is not often that my software works at first try ;)

POSTED BY: Sander Huisman
Answer
12 days ago

I made a small update to the code, in case you give an rgb color with alpha-channel.

POSTED BY: Sander Huisman
Answer
12 days ago

Group Abstract Group Abstract