Re: [ROOT] Different color palettes within one canvas

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Thu May 10 2001 - 13:59:42 MEST


Hi Rene et al, 

On Thu, 10 May 2001 09:12:17 +0000
Rene Brun <Rene.Brun@cern.ch> wrote
concerning ": Re: [ROOT] Different color palettes within one canvas":
> Hi Christian,
> 
> I do not agree with your request of SetLineColor(r,g,b);
> The TAttLine object must be as small as possible (currently 3 shorts).
> Your request would imply to add 3 floats (or 3 doubles).
> I do not see the interest to define the rgb on a per object basis.
> You define the color once, then you reference the color.

Well, TAttLine needn't contain the three float, does it.  I could
imagine having: 

  TAttLine::SetLineColor(float r, float g, float b) {
     // Get list of all defined colours. 
     TObjArray *colours = (TObjArray*)gROOT->GetListOfColors();

     // Dummy. 
     TColor* colour = 0; 

     // Look for colour by name 
     if ((colour = (TColor*)colours->FindObject(Form("#%02x%02x%02x", 
                                                     255 * r, 255 * g,
                                                     255 * b))) {
       // We found the colour by name, so we use that right away
       fLineColor = colour->GetNumber();
       return;
     }
     
     // Iterator 
     TIter next(colours); 

     // Loop over all defined colours. 
     while((colour = (TColor*)next())) {
       if (colour->GetRed() != r) 
         continue;
       if (colour->GetGreen() != g) 
         continue;
       if (colour->GetBlue() != b) 
         continue;
      
       // We found a matching colour in the colour table 
       fLineColor = colour->GetNumber();
       return;
     }

     // We didn't find a matching colour in the colour table, so we
     // add it.  Note name is of the form "#rrggbb" where rr, etc are
     // hexadecimal numbers. 
     colour = new TColor(colours->GetEntries(), r, g, b, 
                         Form("#%02x%02x%02x", 255 * r, 255 * g, 255 * b));
     fLineColor = colour->GetNumber(); 
  }

Perhaps a static method of TColor, say 

  static int TColor::GetColor(float, float, float) { 
    // Get list of all defined colours. 
    TObjArray *colours = (TObjArray*)gROOT->GetListOfColors();

    // Dummy. 
    TColor* colour = 0; 

    // Look for colour by name 
    if ((colour = (TColor*)colours->FindObject(Form("#%02x%02x%02x", 
                                                     255 * r, 255 * g,
                                                     255 * b)))
      // We found the colour by name, so we use that right away
      return colour->GetNumber();
     
    // Iterator 
    TIter next(colours); 

    // Loop over all defined colours. 
    while((colour = (TColor*)next())) {
      if (colour->GetRed() != r) 
        continue;
      if (colour->GetGreen() != g) 
        continue;
      if (colour->GetBlue() != b) 
        continue;
     
      // We found a matching colour in the colour table 
      return colour->GetNumber();
    }

    // We didn't find a matching colour in the colour table, so we
    // add it.  Note name is of the form "#rrggbb" where rr, etc are
    // hexadecimal numbers. 
    colour = new TColor(colours->GetEntries(), r, g, b, 
                        Form("#%02x%02x%02x", 255 * r, 255 * g, 255 * b));
    return colour->GetNumber(); 
  }

could provide this service for all T...Attr classes that need this. 

Doing it like this, you don't need to have more members in the classes
TAtt... or TColor, and you can get hold of such colours again, by
requesting the colour by name, like 

   TColor* colour = 
     (TColor*)gROOT->GetListOfColors->FindObject("#0f5086"); 

Perhaps, TColor objects should, if the user didn't give a name, have
names of the form "#rrggbb", where rr is hexadecimal number.  I think
that's better than the present "COLOR_...", since the format "#rrggbb"
is widely used by a great deal of programs and formats (like HTML,
LaTeX, and so on), and also "COLOR_..." doesn't really tell you
anything about the actual colour, while "#rrggbb" does. 

I hope you find this suggestion useful. 

Yours, 

Christian  -----------------------------------------------------------
Holm Christensen                             Phone:  (+45) 35 35 96 91 
  Sankt Hansgade 23, 1. th.                  Office: (+45) 353  25 305 
  DK-2200 Copenhagen N                       Web:    www.nbi.dk/~cholm    
  Denmark                                    Email:       cholm@nbi.dk



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:44 MET