ROOT logo
ROOT » CORE » BASE » TColor

class TColor: public TNamed


TColor: Color Creation and Management

Color defined by RGB or HLS. At initialization time, a table of colors is generated. This linked list can be accessed from the TROOT object via the color index, e.g.
 TColor *color = gROOT->GetColor(kOrange);
 

Two sets of colors are initialized;

  • Colors with index 0 to 50 (old style palette)
  • The Color Wheel (recommended) with color indices from 300 to 1000.

Old Style Palette

output of MACRO_TColor_1_c
{
   TCanvas *c = new TCanvas("c","Fill Area colors",0,0,500,200);
   c.DrawColorTable();
   return c;
}

The ROOT Color Wheel

The wheel contains the recommended 216 colors to be used in web applications. The colors in the Color Wheel are created by TColor::CreateColorWheel.

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. We strongly recommend to use these keywords in your code instead of hardcoded color numbers, e.g.:

   myObject.SetFillColor(kRed);
   myObject.SetFillColor(kYellow-10);
   myLine.SetLineColor(kMagenta+2);
output of MACRO_TColor_3_wheel
{
   TColorWheel *w = new TColorWheel();
   w->Draw();
   return w->GetCanvas();
}

Bright and Dark colors

The dark and bright color are used to give 3-D effects when drawing various boxes (see TWbox, TPave, TPaveText, TPaveLabel, etc).
  • The Dark colors have an index = color_index+100
  • The Bright colors have an index = color_index+150
  • Pointers to the dark and bright colors can be retrieved via the TColor static functions:
          TColor *dark   = TColor::GetColorDark(color_index);
          TColor *bright = TColor::GetColorBright(color_index);
       

Gray scale view of of canvas with colors

One can toggle between a grayscale preview and the regular colored mode using TCanvas::SetGrayscale(). Note that in grayscale mode, access via RGB will return grayscale values according to ITU standards (and close to b&w printer grayscales), while access via HLS returns de-saturated grayscales. The table below show the ROOT Color Wheel in grayscale mode.
output of MACRO_TColor_5_wheel
{
   TColorWheel *w = new TColorWheel();
   w->Draw();
   w->GetCanvas()->SetGrayscale();
   w->GetCanvas()->Modified();
   w->GetCanvas()->Update();
   return w->GetCanvas();
}
 

Function Members (Methods)

public:
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()
voidTObject::AbstractMethod(const char* method) const
virtual voidTObject::AppendPad(Option_t* option = "")
const char*AsHexString() const
virtual voidTObject::Browse(TBrowser* b)
static TClass*Class()
static TClass*TNamed::Class()
virtual const char*TObject::ClassName() const
virtual voidTNamed::Clear(Option_t* option = "")
virtual voidTObject::Clear(Option_t* = "")
virtual TObject*TNamed::Clone(const char* newname = "") const
virtual TObject*TObject::Clone(const char* newname = "") const
virtual Int_tTNamed::Compare(const TObject* obj) const
virtual Int_tTObject::Compare(const TObject* obj) const
virtual voidCopy(TObject& color) const
virtual voidTNamed::Copy(TObject& named) const
virtual voidTObject::Copy(TObject& object) const
static voidCreateColorsCircle(Int_t offset, const char* name, UChar_t* rgb)
static voidCreateColorsGray()
static voidCreateColorsRectangle(Int_t offset, const char* name, UChar_t* rgb)
static voidCreateColorWheel()
static Int_tCreateGradientColorTable(UInt_t Number, Double_t* Length, Double_t* Red, Double_t* Green, Double_t* Blue, UInt_t NColors)
virtual voidTObject::Delete(Option_t* option = "")MENU
virtual Int_tTObject::DistancetoPrimitive(Int_t px, Int_t py)
virtual voidTObject::Draw(Option_t* option = "")
virtual voidTObject::DrawClass() constMENU
virtual TObject*TObject::DrawClone(Option_t* option = "") constMENU
virtual voidTObject::Dump() constMENU
virtual voidTObject::Error(const char* method, const char* msgfmt) const
virtual voidTObject::Execute(const char* method, const char* params, Int_t* error = 0)
virtual voidTObject::Execute(TMethod* method, TObjArray* params, Int_t* error = 0)
virtual voidTObject::ExecuteEvent(Int_t event, Int_t px, Int_t py)
virtual voidTObject::Fatal(const char* method, const char* msgfmt) const
virtual voidTNamed::FillBuffer(char*& buffer)
virtual TObject*TObject::FindObject(const char* name) const
virtual TObject*TObject::FindObject(const TObject* obj) const
Float_tGetAlpha() const
Float_tGetBlue() const
static Int_tGetColor(const char* hexcolor)
static Int_tGetColor(ULong_t pixel)
static Int_tGetColor(Float_t r, Float_t g, Float_t b)
static Int_tGetColor(Int_t r, Int_t g, Int_t b)
static Int_tGetColorBright(Int_t color)
static Int_tGetColorDark(Int_t color)
static Int_tGetColorPalette(Int_t i)
virtual Option_t*TObject::GetDrawOption() const
static Long_tTObject::GetDtorOnly()
virtual Float_tGetGrayscale() const
Float_tGetGreen() const
virtual voidGetHLS(Float_t& h, Float_t& l, Float_t& s) const
Float_tGetHue() const
virtual const char*TObject::GetIconName() const
Float_tGetLight() const
virtual const char*TNamed::GetName() const
virtual const char*TObject::GetName() const
Int_tGetNumber() const
static Int_tGetNumberOfColors()
virtual char*TObject::GetObjectInfo(Int_t px, Int_t py) const
static Bool_tTObject::GetObjectStat()
virtual Option_t*TObject::GetOption() const
ULong_tGetPixel() const
Float_tGetRed() const
virtual voidGetRGB(Float_t& r, Float_t& g, Float_t& b) const
Float_tGetSaturation() const
virtual const char*TNamed::GetTitle() const
virtual const char*TObject::GetTitle() const
virtual UInt_tTObject::GetUniqueID() const
virtual Bool_tTObject::HandleTimer(TTimer* timer)
virtual ULong_tTNamed::Hash() const
virtual ULong_tTObject::Hash() const
static voidHLS2RGB(Float_t h, Float_t l, Float_t s, Float_t& r, Float_t& g, Float_t& b)
static voidHLS2RGB(Int_t h, Int_t l, Int_t s, Int_t& r, Int_t& g, Int_t& b)
static voidHLStoRGB(Float_t h, Float_t l, Float_t s, Float_t& r, Float_t& g, Float_t& b)
static voidHSV2RGB(Float_t h, Float_t s, Float_t v, Float_t& r, Float_t& g, Float_t& b)
virtual voidTObject::Info(const char* method, const char* msgfmt) const
virtual Bool_tTObject::InheritsFrom(const char* classname) const
virtual Bool_tTObject::InheritsFrom(const TClass* cl) const
static voidInitializeColors()
virtual voidTObject::Inspect() constMENU
voidTObject::InvertBit(UInt_t f)
virtual TClass*IsA() const
virtual TClass*TNamed::IsA() const
virtual TClass*TObject::IsA() const
virtual Bool_tTObject::IsEqual(const TObject* obj) const
virtual Bool_tTObject::IsFolder() const
static Bool_tIsGrayscale()
Bool_tTObject::IsOnHeap() const
virtual Bool_tTNamed::IsSortable() const
Bool_tTObject::IsZombie() const
virtual voidls(Option_t* option = "") const
virtual voidTNamed::ls(Option_t* option = "") const
virtual voidTObject::ls(Option_t* option = "") const
voidTObject::MayNotUse(const char* method) const
virtual Bool_tTObject::Notify()
static ULong_tNumber2Pixel(Int_t ci)
static voidTObject::operator delete(void* ptr)
static voidTObject::operator delete(void* ptr, void* vp)
static voidTObject::operator delete[](void* ptr)
static voidTObject::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&)
TNamed&TNamed::operator=(const TNamed& rhs)
virtual voidTObject::Paint(Option_t* option = "")
static voidPixel2RGB(ULong_t pixel, Int_t& r, Int_t& g, Int_t& b)
static voidPixel2RGB(ULong_t pixel, Float_t& r, Float_t& g, Float_t& b)
static const char*PixelAsHexString(ULong_t pixel)
virtual voidTObject::Pop()
virtual voidPrint(Option_t* option = "") const
virtual voidTNamed::Print(Option_t* option = "") const
virtual voidTObject::Print(Option_t* option = "") const
virtual Int_tTObject::Read(const char* name)
virtual voidTObject::RecursiveRemove(TObject* obj)
voidTObject::ResetBit(UInt_t f)
static voidRGB2HLS(Float_t r, Float_t g, Float_t b, Float_t& h, Float_t& l, Float_t& s)
static voidRGB2HLS(Int_t r, Int_t g, Int_t b, Int_t& h, Int_t& l, Int_t& s)
static voidRGB2HSV(Float_t r, Float_t g, Float_t b, Float_t& h, Float_t& s, Float_t& v)
static ULong_tRGB2Pixel(Int_t r, Int_t g, Int_t b)
static ULong_tRGB2Pixel(Float_t r, Float_t g, Float_t b)
static voidRGBtoHLS(Float_t r, Float_t g, Float_t b, Float_t& h, Float_t& l, Float_t& s)
virtual voidTObject::SaveAs(const char* filename = "", Option_t* option = "") constMENU
static voidSaveColor(ostream& out, Int_t ci)
virtual voidTObject::SavePrimitive(basic_ostream<char,char_traits<char> >& out, Option_t* option = "")
voidTObject::SetBit(UInt_t f)
voidTObject::SetBit(UInt_t f, Bool_t set)
virtual voidTObject::SetDrawOption(Option_t* option = "")MENU
static voidTObject::SetDtorOnly(void* obj)
static voidSetGrayscale(Bool_t set = kTRUE)
virtual voidTNamed::SetName(const char* name)MENU
virtual voidTNamed::SetNameTitle(const char* name, const char* title)
static voidTObject::SetObjectStat(Bool_t stat)
static voidSetPalette(Int_t ncolors, Int_t* colors)
virtual voidSetRGB(Float_t r, Float_t g, Float_t b)
virtual voidTNamed::SetTitle(const char* title = "")MENU
virtual voidTObject::SetUniqueID(UInt_t uid)
virtual voidShowMembers(TMemberInspector& insp, char* parent)
virtual voidTNamed::ShowMembers(TMemberInspector& insp, char* parent)
virtual voidTObject::ShowMembers(TMemberInspector& insp, char* parent)
virtual Int_tTNamed::Sizeof() const
virtual voidStreamer(TBuffer& b)
virtual voidTNamed::Streamer(TBuffer& b)
voidStreamerNVirtual(TBuffer& b)
voidTNamed::StreamerNVirtual(TBuffer& b)
voidTObject::StreamerNVirtual(TBuffer& b)
virtual voidTObject::SysError(const char* method, const char* msgfmt) const
Bool_tTObject::TestBit(UInt_t f) const
Int_tTObject::TestBits(UInt_t f) const
virtual voidTObject::UseCurrentStyle()
virtual voidTObject::Warning(const char* method, const char* msgfmt) const
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0)
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0) const
protected:
virtual voidTObject::DoError(int level, const char* location, const char* fmt, va_list va) const
voidTObject::MakeZombie()
private:
voidAllocate()
static Float_tHLStoRGB1(Float_t rn1, Float_t rn2, Float_t huei)

Data Members

protected:
TStringTNamed::fNameobject identifier
TStringTNamed::fTitleobject title
private:
Float_tfAlphaAlpha (transparency)
Float_tfBlueFraction of Blue
Float_tfGreenFraction of Green
Float_tfHueHue
Float_tfLightLight
Int_tfNumberColor number identifier
Float_tfRedFraction of Red
Float_tfSaturationSaturation
static Bool_tfgGrayscaleModeif set, GetColor will return grayscale
static Bool_tfgInitDonekTRUE once ROOT colors have been initialized
static TArrayIfgPaletteColor palette

Class Charts

Inheritance Inherited Members Includes Libraries
Class Charts

Function documentation

TColor()
 Default ctor.
TColor(Int_t color, Float_t r, Float_t g, Float_t b, const char* name = "", Float_t a = 1)
 Normal color constructor. Initialize a color structure.
 Compute the RGB and HLS parameters
~TColor()
 Color destructor.
TColor(const TColor& color)
 Color copy ctor.
void InitializeColors()
 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).
const char * AsHexString() const
 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.
void Copy(TObject& color) const
 Copy this color to obj.
void CreateColorsGray()
 Create the Gray scale colors in the Color Wheel
void CreateColorsCircle(Int_t offset, const char* name, UChar_t* rgb)
 Create the "circle" colors in the Color Wheel
void CreateColorsRectangle(Int_t offset, const char* name, UChar_t* rgb)
 Create the "rectangular" colors in the Color Wheel
void CreateColorWheel()
 Static function steering the creation of all colors in the ROOT Color Wheel
Int_t GetColorPalette(Int_t i)
 static function Return color number i in current palette.
Int_t GetNumberOfColors()
 static function: Return number of colors in the color palette
ULong_t GetPixel() const
 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.
void HLS2RGB(Float_t hue, Float_t light, Float_t satur, Float_t &r, Float_t &g, Float_t &b)
 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].
Float_t HLStoRGB1(Float_t rn1, Float_t rn2, Float_t huei)
 Static method. Auxiliary to HLS2RGB().
void HLS2RGB(Int_t h, Int_t l, Int_t s, Int_t &r, Int_t &g, Int_t &b)
 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].
void HSV2RGB(Float_t h, Float_t s, Float_t v, Float_t& r, Float_t& g, Float_t& b)
 Static method to compute RGB from HSV.
 - The hue value runs from 0 to 360.
 - The saturation is the degree of strength or purity and is from 0 to 1.
   Purity is how much white is added to the color, so S=1 makes the purest
   color (no white).
 - Brightness value also ranges from 0 to 1, where 0 is the black.
 The returned r,g,b triplet is between [0,1].
void ls(Option_t* option = "") const
 List this color with its attributes.
void Print(Option_t* option = "") const
 Dump this color with its attributes.
void RGB2HLS(Float_t rr, Float_t gg, Float_t bb, Float_t &hue, Float_t &light, Float_t &satur)
 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].
void RGB2HSV(Float_t r, Float_t g, Float_t b, Float_t& h, Float_t& s, Float_t& v)
 Static method to compute HSV from RGB.
 - r,g,b triplet is between [0,1].
 The returned value:
 - The hue value runs from 0 to 360.
 - The saturation is the degree of strength or purity and is from 0 to 1.
   Purity is how much white is added to the color, so S=1 makes the purest
   color (no white).
 - Brightness value also ranges from 0 to 1, where 0 is the black.
void RGB2HLS(Int_t r, Int_t g, Int_t b, Int_t &h, Int_t &l, Int_t &s)
 Static method to compute HLS from RGB. The r,g,b triplet is between
 [0,255], hue, light and satur are between [0,255].
void SetRGB(Float_t r, Float_t g, Float_t b)
 Initialize this color and its associated colors.
void Allocate()
 Make this color known to the graphics system.
Int_t GetColor(const char *hexcolor)
 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".
 If specified color does not exist it will be created with as
 name "#rrggbb" with rr, gg and bb in hex between [0,FF].
Int_t GetColor(Float_t r, Float_t g, Float_t b)
 Static method returning color number for color specified by
 r, g and b. The r,g,b should be in the range [0,1].
 If specified color does not exist it will be created
 with as name "#rrggbb" with rr, gg and bb in hex between
 [0,FF].
Int_t GetColor(ULong_t pixel)
 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.
Int_t GetColor(Int_t r, Int_t g, Int_t b)
 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].
Int_t GetColorBright(Int_t color)
 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
Int_t GetColorDark(Int_t color)
 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
ULong_t Number2Pixel(Int_t ci)
 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.
ULong_t RGB2Pixel(Float_t r, Float_t g, Float_t b)
 Convert r,g,b to graphics system dependent pixel value.
 The r,g,b triplet must be [0,1].
ULong_t RGB2Pixel(Int_t r, Int_t g, Int_t b)
 Convert r,g,b to graphics system dependent pixel value.
 The r,g,b triplet must be [0,255].
void Pixel2RGB(ULong_t pixel, Float_t &r, Float_t &g, Float_t &b)
 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].
void Pixel2RGB(ULong_t pixel, Int_t &r, Int_t &g, Int_t &b)
 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].
const char * PixelAsHexString(ULong_t pixel)
 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.
void SaveColor(ostream& out, Int_t ci)
 Save a color with index > 228 as a C++ statement(s) on output stream out.
Bool_t IsGrayscale()
 Return whether all colors return grayscale values
void SetGrayscale(Bool_t set = kTRUE)
 Set whether all colors should return grayscale values
Int_t CreateGradientColorTable(UInt_t Number, Double_t* Length, Double_t* Red, Double_t* Green, Double_t* Blue, UInt_t NColors)
 STATIC function.
 Linear gradient color table:
 Red, Green and Blue are several RGB colors with values from 0.0 .. 1.0.
 Their number is "Intervals".
 Length is the length of the color interval between the RGB-colors:
 Imaging the whole gradient goes from 0.0 for the first RGB color to 1.0
 for the last RGB color, then each "Length"-entry in between stands for
 the length of the intervall between the according RGB colors.

 This definition is similar to the povray-definition of gradient
 color tables.

 In order to create a color table do the following:
 Define the RGB Colors:
 > UInt_t Number = 5;
 > Double_t Red[5]   = { 0.00, 0.09, 0.18, 0.09, 0.00 };
 > Double_t Green[5] = { 0.01, 0.02, 0.39, 0.68, 0.97 };
 > Double_t Blue[5]  = { 0.17, 0.39, 0.62, 0.79, 0.97 };
 Define the length of the (color)-interval between this points
 > Double_t Stops[5] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
 i.e. the color interval between Color 2 and Color 3 is
 0.79 - 0.62 => 17 % of the total palette area between these colors

  Original code by Andreas Zoglauer <zog@mpe.mpg.de>
void SetPalette(Int_t ncolors, Int_t* colors)
 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 TStyle::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 colors

  The 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.
void GetRGB(Float_t& r, Float_t& g, Float_t& b) const
{ r=GetRed(); g=GetGreen(); b=GetBlue(); }
void GetHLS(Float_t& h, Float_t& l, Float_t& s) const
{ h=GetHue(); l=GetLight(); s=GetSaturation(); }
Int_t GetNumber() const
{ return fNumber; }
Float_t GetRed() const
{ return IsGrayscale() ? GetGrayscale() : fRed; }
Float_t GetGreen() const
{ return IsGrayscale() ? GetGrayscale() : fGreen; }
Float_t GetBlue() const
{ return IsGrayscale() ? GetGrayscale() : fBlue; }
Float_t GetHue() const
{ return fHue; }
Float_t GetLight() const
{ return fLight; }
Float_t GetSaturation() const
{ return IsGrayscale() ? 0 : fSaturation; }
Float_t GetAlpha() const
{ return fAlpha; }
Float_t GetGrayscale() const
{ /*ITU*/ return 0.299f*fRed + 0.587f*fGreen + 0.114f*fBlue; }
void HLStoRGB(Float_t h, Float_t l, Float_t s, Float_t& r, Float_t& g, Float_t& b)
{ TColor::HLS2RGB(h, l, s, r, g, b); }
void RGBtoHLS(Float_t r, Float_t g, Float_t b, Float_t& h, Float_t& l, Float_t& s)
{ TColor::RGB2HLS(r, g, b, h, l, s); }