At initialization time, a table of colors is generated. An existing color can
be retrieve thanks to its index:
TColor *color = gROOT->GetColor(10);
color->SetRGB(0.1, 0.2, 0.3);
Int_t ci = 1756; // color index TColor *color = new TColor(ci, 0.1, 0.2, 0.3);
Two sets of colors are initialized;
Using this color set for your text, background or graphics will give your application a consistent appearance across different platforms and browsers.
Colors are grouped by hue, the aspect most important in human perception. Touching color chips have the same hue, but with different brightness and vividness.
Colors of slightly different hues clash. If you intend to display colors of the same hue together, you should pick them from the same group.
Each color chip is identified by a mnemonic (e.g. kYellow) and a number. The keywords, kRed, kBlue, kYellow, kPink, etc are defined in the header file Rtypes.h that is included in all ROOT other header files. It is better to use these keywords in user code instead of hardcoded color numbers, e.g.:
myObject.SetFillColor(kRed); myObject.SetFillColor(kYellow-10); myLine.SetLineColor(kMagenta+2);
{ TColorWheel *w = new TColorWheel(); w->Draw(); return w->GetCanvas(); }
Int_t dark = TColor::GetColorDark(color_index); Int_t bright = TColor::GetColorBright(color_index);
{ TColorWheel *w = new TColorWheel(); w->Draw(); w->GetCanvas()->SetGrayscale(); w->GetCanvas()->Modified(); w->GetCanvas()->Update(); return w->GetCanvas(); }
gStyle->SetPalette(...);
This function has two parameters: the number of colors in the palette and an array of containing the indices of colors in the pallette. The following small example demonstrates how to define and use the color palette:
To define more a complexe palette with a continous gradient of color, one should use the static function TColor::CreateGradientColorTable(). The following example demostrates how to proceed:
{ TCanvas *c2 = new TCanvas("c2","c2",0,0,600,400); TF2 *f2 = new TF2("f2","0.1+(1-(x-2)*(x-2))*(1-(y-2)*(y-2))",1,3,1,3); UInt_t Number = 3; Double_t Red[Number] = { 1.00, 0.00, 0.00}; Double_t Green[Number] = { 0.00, 1.00, 0.00}; Double_t Blue[Number] = { 1.00, 0.00, 1.00}; Double_t Length[Number] = { 0.00, 0.50, 1.00 }; Int_t nb=50; TColor::CreateGradientColorTable(Number,Length,Red,Green,Blue,nb); f2->SetContour(nb); f2->Draw("surf1z"); return c2; }
The function TColor::CreateGradientColorTable() performs automatically a call to gStyle->SetPalette(). So there is not need to add one.
After a call to TColor::CreateGradientColorTable() it is sometimes
useful to store the newly create palette for further use. In particular, it is
recomended to do if one wants to switch between several user define palettes.
To store a palette in an array it is enough to do:
Int_t MyPalette[100]; Double_t r[] = {0., 0.0, 1.0, 1.0, 1.0}; Double_t g[] = {0., 0.0, 0.0, 1.0, 1.0}; Double_t b[] = {0., 1.0, 0.0, 0.0, 1.0}; Double_t stop[] = {0., .25, .50, .75, 1.0}; Int_t FI = TColor::CreateGradientColorTable(5, stop, r, g, b, 100); for (int i=0;i<100;i++) MyPalette[i] = FI+i;
Later on to reuse the palette MyPalette it will be enough to do
gStyle->SetPalette(100, MyPalette);
As only one palette is active, one need to use TExec to be able to display plots using differents palettes on the same pad. The following macro illustrate this feature.
//Draw color plots using different color palettes. //Author:: Olivier Couet #include "TStyle.h" #include "TColor.h" #include "TF2.h" #include "TExec.h" #include "TCanvas.h" void Pal1() { static Int_t colors[50]; static Bool_t initialized = kFALSE; Double_t Red[3] = { 1.00, 0.00, 0.00}; Double_t Green[3] = { 0.00, 1.00, 0.00}; Double_t Blue[3] = { 1.00, 0.00, 1.00}; Double_t Length[3] = { 0.00, 0.50, 1.00 }; if(!initialized){ Int_t FI = TColor::CreateGradientColorTable(3,Length,Red,Green,Blue,50); for (int i=0; i<50; i++) colors[i] = FI+i; initialized = kTRUE; return; } gStyle->SetPalette(50,colors); } void Pal2() { static Int_t colors[50]; static Bool_t initialized = kFALSE; Double_t Red[3] = { 1.00, 0.50, 0.00}; Double_t Green[3] = { 0.50, 0.00, 1.00}; Double_t Blue[3] = { 1.00, 0.00, 0.50}; Double_t Length[3] = { 0.00, 0.50, 1.00 }; if(!initialized){ Int_t FI = TColor::CreateGradientColorTable(3,Length,Red,Green,Blue,50); for (int i=0; i<50; i++) colors[i] = FI+i; initialized = kTRUE; return; } gStyle->SetPalette(50,colors); } TCanvas* multipalette() { TCanvas *c3 = new TCanvas("c3","C3",0,0,600,400); c3->Divide(2,1); TF2 *f3 = new TF2("f3","0.1+(1-(x-2)*(x-2))*(1-(y-2)*(y-2))",1,3,1,3); c3->cd(1); f3->Draw("surf1"); TExec *ex1 = new TExec("ex1","Pal1();"); ex1->Draw(); f3->Draw("surf1 same"); c3->cd(2); f3->Draw("surf1"); TExec *ex2 = new TExec("ex2","Pal2();"); ex2->Draw(); f3->Draw("surf1 same"); return c3; }
TColor() | |
TColor(const TColor& color) | |
TColor(Int_t color, Float_t r, Float_t g, Float_t b, const char* name = "", Float_t a = 1) | |
virtual | ~TColor() |
void | TObject::AbstractMethod(const char* method) const |
virtual void | TObject::AppendPad(Option_t* option = "") |
const char* | AsHexString() const |
virtual void | TObject::Browse(TBrowser* b) |
static TClass* | Class() |
virtual const char* | TObject::ClassName() const |
virtual void | TNamed::Clear(Option_t* option = "") |
virtual TObject* | TNamed::Clone(const char* newname = "") const |
virtual Int_t | TNamed::Compare(const TObject* obj) const |
virtual void | Copy(TObject& color) const |
static void | CreateColorsCircle(Int_t offset, const char* name, UChar_t* rgb) |
static void | CreateColorsGray() |
static void | CreateColorsRectangle(Int_t offset, const char* name, UChar_t* rgb) |
static void | CreateColorWheel() |
static Int_t | CreateGradientColorTable(UInt_t Number, Double_t* Stops, Double_t* Red, Double_t* Green, Double_t* Blue, UInt_t NColors) |
virtual void | TObject::Delete(Option_t* option = "")MENU |
virtual Int_t | TObject::DistancetoPrimitive(Int_t px, Int_t py) |
virtual void | TObject::Draw(Option_t* option = "") |
virtual void | TObject::DrawClass() constMENU |
virtual TObject* | TObject::DrawClone(Option_t* option = "") constMENU |
virtual void | TObject::Dump() constMENU |
virtual void | TObject::Error(const char* method, const char* msgfmt) const |
virtual void | TObject::Execute(const char* method, const char* params, Int_t* error = 0) |
virtual void | TObject::Execute(TMethod* method, TObjArray* params, Int_t* error = 0) |
virtual void | TObject::ExecuteEvent(Int_t event, Int_t px, Int_t py) |
virtual void | TObject::Fatal(const char* method, const char* msgfmt) const |
virtual void | TNamed::FillBuffer(char*& buffer) |
virtual TObject* | TObject::FindObject(const char* name) const |
virtual TObject* | TObject::FindObject(const TObject* obj) const |
Float_t | GetAlpha() const |
Float_t | GetBlue() const |
static Int_t | GetColor(const char* hexcolor) |
static Int_t | GetColor(ULong_t pixel) |
static Int_t | GetColor(Float_t r, Float_t g, Float_t b) |
static Int_t | GetColor(Int_t r, Int_t g, Int_t b) |
static Int_t | GetColorBright(Int_t color) |
static Int_t | GetColorDark(Int_t color) |
static Int_t | GetColorPalette(Int_t i) |
virtual Option_t* | TObject::GetDrawOption() const |
static Long_t | TObject::GetDtorOnly() |
virtual Float_t | GetGrayscale() const |
Float_t | GetGreen() const |
virtual void | GetHLS(Float_t& h, Float_t& l, Float_t& s) const |
Float_t | GetHue() const |
virtual const char* | TObject::GetIconName() const |
Float_t | GetLight() const |
virtual const char* | TNamed::GetName() const |
Int_t | GetNumber() const |
static Int_t | GetNumberOfColors() |
virtual char* | TObject::GetObjectInfo(Int_t px, Int_t py) const |
static Bool_t | TObject::GetObjectStat() |
virtual Option_t* | TObject::GetOption() const |
ULong_t | GetPixel() const |
Float_t | GetRed() const |
virtual void | GetRGB(Float_t& r, Float_t& g, Float_t& b) const |
Float_t | GetSaturation() const |
virtual const char* | TNamed::GetTitle() const |
virtual UInt_t | TObject::GetUniqueID() const |
virtual Bool_t | TObject::HandleTimer(TTimer* timer) |
virtual ULong_t | TNamed::Hash() const |
static void | HLS2RGB(Float_t h, Float_t l, Float_t s, Float_t& r, Float_t& g, Float_t& b) |
static void | HLS2RGB(Int_t h, Int_t l, Int_t s, Int_t& r, Int_t& g, Int_t& b) |
static void | HLStoRGB(Float_t h, Float_t l, Float_t s, Float_t& r, Float_t& g, Float_t& b) |
static void | HSV2RGB(Float_t h, Float_t s, Float_t v, Float_t& r, Float_t& g, Float_t& b) |
virtual void | TObject::Info(const char* method, const char* msgfmt) const |
virtual Bool_t | TObject::InheritsFrom(const char* classname) const |
virtual Bool_t | TObject::InheritsFrom(const TClass* cl) const |
static void | InitializeColors() |
virtual void | TObject::Inspect() constMENU |
void | TObject::InvertBit(UInt_t f) |
virtual TClass* | IsA() const |
virtual Bool_t | TObject::IsEqual(const TObject* obj) const |
virtual Bool_t | TObject::IsFolder() const |
static Bool_t | IsGrayscale() |
Bool_t | TObject::IsOnHeap() const |
virtual Bool_t | TNamed::IsSortable() const |
Bool_t | TObject::IsZombie() const |
virtual void | ls(Option_t* option = "") const |
void | TObject::MayNotUse(const char* method) const |
virtual Bool_t | TObject::Notify() |
static ULong_t | Number2Pixel(Int_t ci) |
void | TObject::Obsolete(const char* method, const char* asOfVers, const char* removedFromVers) const |
static void | TObject::operator delete(void* ptr) |
static void | TObject::operator delete(void* ptr, void* vp) |
static void | TObject::operator delete[](void* ptr) |
static void | TObject::operator delete[](void* ptr, void* vp) |
void* | TObject::operator new(size_t sz) |
void* | TObject::operator new(size_t sz, void* vp) |
void* | TObject::operator new[](size_t sz) |
void* | TObject::operator new[](size_t sz, void* vp) |
TColor& | operator=(const TColor&) |
virtual void | TObject::Paint(Option_t* option = "") |
static void | Pixel2RGB(ULong_t pixel, Int_t& r, Int_t& g, Int_t& b) |
static void | Pixel2RGB(ULong_t pixel, Float_t& r, Float_t& g, Float_t& b) |
static const char* | PixelAsHexString(ULong_t pixel) |
virtual void | TObject::Pop() |
virtual void | Print(Option_t* option = "") const |
virtual Int_t | TObject::Read(const char* name) |
virtual void | TObject::RecursiveRemove(TObject* obj) |
void | TObject::ResetBit(UInt_t f) |
static void | RGB2HLS(Float_t r, Float_t g, Float_t b, Float_t& h, Float_t& l, Float_t& s) |
static void | RGB2HLS(Int_t r, Int_t g, Int_t b, Int_t& h, Int_t& l, Int_t& s) |
static void | RGB2HSV(Float_t r, Float_t g, Float_t b, Float_t& h, Float_t& s, Float_t& v) |
static ULong_t | RGB2Pixel(Int_t r, Int_t g, Int_t b) |
static ULong_t | RGB2Pixel(Float_t r, Float_t g, Float_t b) |
static void | RGBtoHLS(Float_t r, Float_t g, Float_t b, Float_t& h, Float_t& l, Float_t& s) |
virtual void | TObject::SaveAs(const char* filename = "", Option_t* option = "") constMENU |
static void | SaveColor(ostream& out, Int_t ci) |
virtual void | TObject::SavePrimitive(ostream& out, Option_t* option = "") |
void | TObject::SetBit(UInt_t f) |
void | TObject::SetBit(UInt_t f, Bool_t set) |
virtual void | TObject::SetDrawOption(Option_t* option = "")MENU |
static void | TObject::SetDtorOnly(void* obj) |
static void | SetGrayscale(Bool_t set = kTRUE) |
virtual void | TNamed::SetName(const char* name)MENU |
virtual void | TNamed::SetNameTitle(const char* name, const char* title) |
static void | TObject::SetObjectStat(Bool_t stat) |
static void | SetPalette(Int_t ncolors, Int_t* colors) |
virtual void | SetRGB(Float_t r, Float_t g, Float_t b) |
virtual void | TNamed::SetTitle(const char* title = "")MENU |
virtual void | TObject::SetUniqueID(UInt_t uid) |
virtual void | ShowMembers(TMemberInspector& insp) |
virtual Int_t | TNamed::Sizeof() const |
virtual void | Streamer(TBuffer& b) |
void | StreamerNVirtual(TBuffer& b) |
virtual void | TObject::SysError(const char* method, const char* msgfmt) const |
Bool_t | TObject::TestBit(UInt_t f) const |
Int_t | TObject::TestBits(UInt_t f) const |
virtual void | TObject::UseCurrentStyle() |
virtual void | TObject::Warning(const char* method, const char* msgfmt) const |
virtual Int_t | TObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0) |
virtual Int_t | TObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0) const |
virtual void | TObject::DoError(int level, const char* location, const char* fmt, va_list va) const |
void | TObject::MakeZombie() |
enum TObject::EStatusBits { | kCanDelete | |
kMustCleanup | ||
kObjInCanvas | ||
kIsReferenced | ||
kHasUUID | ||
kCannotPick | ||
kNoContextMenu | ||
kInvalidObject | ||
}; | ||
enum TObject::[unnamed] { | kIsOnHeap | |
kNotDeleted | ||
kZombie | ||
kBitMask | ||
kSingleKey | ||
kOverwrite | ||
kWriteDelete | ||
}; |
TString | TNamed::fName | object identifier |
TString | TNamed::fTitle | object title |
Float_t | fAlpha | Alpha (transparency) |
Float_t | fBlue | Fraction of Blue |
Float_t | fGreen | Fraction of Green |
Float_t | fHue | Hue |
Float_t | fLight | Light |
Int_t | fNumber | Color number identifier |
Float_t | fRed | Fraction of Red |
Float_t | fSaturation | Saturation |
static Bool_t | fgGrayscaleMode | if set, GetColor will return grayscale |
static Bool_t | fgInitDone | kTRUE once ROOT colors have been initialized |
static TArrayI | fgPalette | Color palette |
Normal color constructor. Initialize a color structure. Compute the RGB and HLS color components.
Initialize colors used by the TCanvas based graphics (via TColor objects). This method should be called before the ApplicationImp is created (which initializes the GUI colors).
Return color as hexidecimal string. This string can be directly passed to, for example, TGClient::GetColorByName(). String will be reused so copy immediately if needed.
Create the "circle" colors in the color wheel.
Create the "rectangular" colors in the color wheel.
Return pixel value corresponding to this color. This pixel value can be used in the GUI classes. This call does not work in batch mode since it needs to communicate with the graphics system.
Static method to compute RGB from HLS. The l and s are between [0,1] and h is between [0,360]. The returned r,g,b triplet is between [0,1].
Static method to compute RGB from HLS. The h,l,s are between [0,255]. The returned r,g,b triplet is between [0,255].
Static method to compute RGB from HSV:
Static method to compute HLS from RGB. The r,g,b triplet is between [0,1], hue is between [0,360], light and satur are [0,1].
Static method to compute HSV from RGB.
Static method to compute HLS from RGB. The r,g,b triplet is between [0,255], hue, light and satur are between [0,255].
Static method returning color number for color specified by hex color string of form: #rrggbb, where rr, gg and bb are in hex between [0,FF], e.g. "#c0c0c0".
Static method returning color number for color specified by r, g and b. The r,g,b should be in the range [0,1].
Static method returning color number for color specified by system dependent pixel value. Pixel values can be obtained, e.g., from the GUI color picker.
Static method returning color number for color specified by r, g and b. The r,g,b should be in the range [0,255]. If the specified color does not exist it will be created with as name "#rrggbb" with rr, gg and bb in hex between [0,FF].
Static function: Returns the bright color number corresponding to n If the TColor object does not exist, it is created. The convention is that the bright color nb = n+150
Static function: Returns the dark color number corresponding to n If the TColor object does not exist, it is created. The convention is that the dark color nd = n+100
Static method that given a color index number, returns the corresponding pixel value. This pixel value can be used in the GUI classes. This call does not work in batch mode since it needs to communicate with the graphics system.
Convert r,g,b to graphics system dependent pixel value. The r,g,b triplet must be [0,1].
Convert r,g,b to graphics system dependent pixel value. The r,g,b triplet must be [0,255].
Convert machine dependent pixel value (obtained via RGB2Pixel or via Number2Pixel() or via TColor::GetPixel()) to r,g,b triplet. The r,g,b triplet will be [0,1].
Convert machine dependent pixel value (obtained via RGB2Pixel or via Number2Pixel() or via TColor::GetPixel()) to r,g,b triplet. The r,g,b triplet will be [0,255].
Convert machine dependent pixel value (obtained via RGB2Pixel or via Number2Pixel() or via TColor::GetPixel()) to a hexidecimal string. This string can be directly passed to, for example, TGClient::GetColorByName(). String will be reused so copy immediately if needed.
Save a color with index > 228 as a C++ statement(s) on output stream out.
Static function creating a color table with several connected linear gradients.
The table is constructed by tracing lines between the given points in RGB space. Each color value may have a value between 0 and 1. The difference between consecutive "Stops" values gives the fraction of space in the whole table that should be used for the interval between the corresponding color values.
Normally the first element of Stops should be 0 and the last should be 1. If this is not true, fewer than NColors will be used in proportion with the total interval between the first and last elements of Stops.
This definition is similar to the povray-definition of gradient color tables.
For instance:
UInt_t Number = 3; Double_t Red[3] = { 0.0, 1.0, 1.0 }; Double_t Green[3] = { 0.0, 0.0, 1.0 }; Double_t Blue[3] = { 1.0, 0.0, 1.0 }; Double_t Stops[3] = { 0.0, 0.4, 1.0 };This defines a table in which there are three color end points: RGB = {0, 0, 1}, {1, 0, 0}, and {1, 1, 1} = blue, red, white The first 40% of the table is used to go linearly from blue to red. The remaining 60% of the table is used to go linearly from red to white.
If you define a very short interval such that less than one color fits in it, no colors at all will be allocated. If this occurs for all intervals, ROOT will revert to the default palette.
Original code by Andreas Zoglauer (zog@mpe.mpg.de)
Static function. The color palette is used by the histogram classes (see TH1::Draw options). For example TH1::Draw("col") draws a 2-D histogram with cells represented by a box filled with a color CI function of the cell content. if the cell content is N, the color CI used will be the color number in colors[N],etc. If the maximum cell content is > ncolors, all cell contents are scaled to ncolors.
if ncolors <= 0 a default palette (see below) of 50 colors is defined. The colors defined in this palette are OK for coloring pads, labels.
if ncolors == 1 && colors == 0, then a Pretty Palette with a Spectrum Violet->Red is created. It is recommended to use this Pretty palette when drawing legos, surfaces or contours.
if ncolors > 50 and colors=0, the DeepSea palette is used. (see TColor::CreateGradientColorTable for more details)
if ncolors > 0 and colors = 0, the default palette is used with a maximum of ncolors.
The default palette defines:
index 0->9 : grey colors from light to dark grey index 10->19 : "brown" colors index 20->29 : "blueish" colors index 30->39 : "redish" colors index 40->49 : basic colorsThe color numbers specified in the palette can be viewed by selecting the item "colors" in the "VIEW" menu of the canvas toolbar. The color parameters can be changed via TColor::SetRGB.
{ h=GetHue(); l=GetLight(); s=GetSaturation(); }