Message Boards Message Boards

GROUPS:

[✓] Export palette from Mathematica into .pal file

Posted 2 years ago
3255 Views
|
5 Replies
|
5 Total Likes
|

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

5 Replies

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

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...

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

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

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

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract