How to set TImagePalette same as gStyle->SetPalette(1)?

Hi Rooters,

Is it possible to set a TImagePalette object to show the rainbow colors set by gStyle->SetPalette(1) for TH2? Please tell me how to do that.


One possible way is to open the palette editor (last choice in the pull down menu when you click on an TImage with the right mouse button) and for the GUI select the “Rainbow” palette.
But may be you would like to do that inside a macro ? let’s know…

Thanks for the suggestion. I know the “Rainbow” palette can be selected by using TPaletteEditor, but it is different from the rainbow palette that can be used for TH2? by doing gStyle->SetPalette(1). I would like to use the latter rainbow palette for image drawing, especially for AstroImage::Draw.


Here is a macro doing that. The image is in the ROOTSYS/tutorials/image

void imagepalette()
   // Define the rainbow palette (50 entries)
   TColor *col;
   Int_t red, green, blue;
   TImagePalette*pal = new TImagePalette(50);
   for (int i=0; i< 50;i++) {
      pal->fPoints[i] = (Double_t)i*0.02;
      pal->fColorRed[i]   = (UShort_t)red << 8;
      pal->fColorGreen[i] = (UShort_t)green << 8;
      pal->fColorBlue[i]  = (UShort_t)blue << 8;
      pal->fColorAlpha[i] = 255 << 8;
   TImage *img = TImage::Open("rose512.jpg");
   TImage *img2 = (TImage*)img->Clone("img2");
   TArrayD *arr = img2->GetArray(700, 700, pal);
   img2->SetImage(arr->GetArray(), 700, 700, pal);

Very good! Thanks for the answer.

By the way, why is the top of the right color bar getting black?


Just do:

      pal->fPoints[i] = (Double_t)i*(1/49.);

(replace 0.02 by 1/49)

Hi again,

Any sample code to make the same figure using CMYK?


TColor does not have the function to make the transformation between RGB and CMY. Look at:

I would just like to save an eps file with CMYK and found the TStyle::SetColorModelPS(Int_t c = 0) method. Isn’t there any workaround with it?


Yes . For PS you can use this one. See the help of this method.

Where is the help? I cannot find such info on the web.


Section “Color Model”

Thanks for the pointer. I tried to add the following three lines at the end of the example macro.


But, “imagepalette.eps” has different colors (I see only black, blue, and green). Do I have to do something else?


I am not really sure what you are looking for. . gStyle->SetColorModelPS(1); does what is explain on the help… the ps file is the same except that the internal color model is different. That helps on some printers which have black as a color.

A Journal Editorial Office requires us to draw the same color figure with CMYK. I think the story is exactly the same as in

If I do something like:

TF2* f = new TF2(…);

I can see the rainbow colors of “tmp.eps” with a ps viewer. It is perfect. But, I don’t get the same result with the imagepalette example. That is why I thought something else is necessary.

Thanks for your help,

CMYK is not implemented for TASImage and PS … some mods are needed… I can have a look… it looks tricky.

This problem is now fixed in the SVN trunk.

It compiles in my environment (FC3) and I could see proper colors with it. Thanks very much!

I think my question is along the same lines but I have absolutely no problem using the palette generated in the palette editor. What I am having trouble with is saving the image after implementing grey scale on my image. The file that I am able to save is a greyscale.pal.root.

/// \file
/// \ingroup Tutorials
/// \notebook
///  This program creates :
///    - a one dimensional histogram
///    - a two dimensional histogram
///    - a profile histogram
///    - a memory-resident ntuple
///  These objects are filled with some random numbers and saved on a file.
///  If get=1 the macro returns a pointer to the TFile of "cluster_random.root"
///          if this file exists, otherwise it is created.
///  The file "cluster_random.root" is created in $ROOTSYS/tutorials if the caller has
///  write access to this directory, otherwise the file is created in $PWD
/// \macro_image
/// \macro_output
/// \macro_code
/// \author Rene Brun

#include <TFile.h>
#include <TNtuple.h>
#include <TH2.h>
#include <TProfile.h>
#include <TCanvas.h>
#include <TFrame.h>
#include <TROOT.h>
#include <TSystem.h>
#include <TRandom3.h>
#include <TBenchmark.h>
#include <TInterpreter.h>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <TAttImage.h>
#include <TImage.h>
#include <TASImage.h>
#include <tiffio.h>

// #include "edflib.h"

using namespace std;

TFile *cluster_nano(Int_t getFile=0) {
    FILE *fileptr1, *fileptr2;
    char filename[40];
    char ch;
    int delete_line, temp = 1;
    int count = 0;
    string line;
    Double_t rho = 0;
    Double_t mu_y = 20.12;
    Double_t mu_x = 27.36;
    Double_t sigma_y = 1.408;
    Double_t sigma_x = 1.915;
    Double_t x, y;
    Double_t bin_l = 10;
    Double_t bin_h = 63;
    Double_t fact = 100;
    const Int_t nbins = bin_h - bin_l;
    const Int_t nfbins = nbins*fact;
    TImage *img = TImage::Create();

    // Create some histograms, a profile histogram and an ntuple
    TCanvas *canv = new TCanvas("c1", "c1",900,900);

    TCanvas *c1 = new TCanvas("c1", "c1",900,900);

    TPad *center_pad = new TPad("center_pad", "center_pad",0.0,0.0,0.6,0.6);

    TPad *right_pad = new TPad("right_pad", "right_pad",0.55,0.0,1.0,0.6);

    TPad *top_pad = new TPad("top_pad", "top_pad",0.0,0.55,0.6,1.0);

    TF2 *f2 = new TF2("f2", "(1.0/(2*TMath::Pi()*[0]*[1]*sqrt(1-TMath::Power([2],2))))*TMath::Exp(-(1.0/(2.0*(1-TMath::Power([2],2))))*((TMath::Power(x-[3],2))/(TMath::Power([0],2)) -2.0*[2]*(x-[3])*(y-[4])/([0]*[1]) +(TMath::Power(y-[4],2))/(TMath::Power([1],2))))",10, 62.5, 10, 62.5);
    f2->SetParameters(sigma_x, sigma_y, rho, mu_x, mu_y);

    TH2D *h2 = new TH2D("h2","",nbins,10,63,nbins,10,63);
    TH2D *h2f = new TH2D("h2f","",nfbins,10,63,nfbins,10,63);

//    hdl = edfopen_file_writeonly("test_generator.edf", EDFLIB_FILETYPE_EDFPLUS, chns);

    // Fill histograms randomly
    TRandom3 randomNum;
    TRandom r = *gRandom;
    Float_t px, py;

    ofstream myfile_s;
    ofstream myfile_b;
    ofstream fout;"signal.csv");"background.csv");

    for (Int_t i = 0; i < 1000; i++) {

//      randomNum.Uniform(px, py);
//      Float_t rnd = randomNum.Uniform(px,py);

//      Float_t m12 = abs(px);TF1
//      Float_t m34 = abs(py);
        Float_t m12 = r.Uniform(10, 62.5);
        Float_t m34 = r.Uniform(10, 62.5);

        if (m34 / m12 < 1) {

//               cout << m12 << " : " << m34 << ";"<< endl;
            myfile_b << m12 << "," << m34 << "\n";
//            myfile_b.close();


    for (Int_t i = 0; i < 1000000; i++) {
        f2->GetRandom2(x, y);

//         randomNum.Rannor(px, py);
//         Float_t rnd = randomNum.Gaus();

        Float_t m12 = abs(x);
        Float_t m34 = abs(y);
        Int_t n = 0;

        if (m34 / m12 < 1) {
//               cout << m12 << " : " << m34 << ";" << endl;
            myfile_s << m12 << "," << m34 << "\n";

//            myfile_s.close();


    for (Int_t ix = 0; ix <= nfbins; ix++) {
        for (Int_t iy = 0; iy <= nfbins; iy++) {
            Double_t bin = h2f->GetBin(ix, iy);
            Int_t iyy = iy/fact;
            Int_t ixx = ix/fact;
            Double_t par = h2->GetBinContent(ixx, iyy);
            Double_t edge_c = h2->GetXaxis()->GetBinLowEdge(ixx+1);
            Double_t edge_f = h2f->GetXaxis()->GetBinLowEdge(iy);
//            cout << bin << endl;
            if (edge_f < fact*edge_c) {
                h2f->SetBinContent(bin, par);
            } else

//  img->SetImage((const Double_t *)h2->GetArray(), h2->GetNbinsX() ,h2->GetNbinsY());

  // Drawing

    img->SetImage((const Double_t *)h2f->GetArray(), h2f->GetNbinsX() + 2,h2f->GetNbinsY() + 2, gHistImagePalette);

    return 0;

here is my code

I do not see where you are creating “greyscale.pal.root”…
I cannot run your script because of missing data