ROOT  6.06/09
Reference Guide
TColorWheel.cxx
Go to the documentation of this file.
1 // @(#)root/gpad:$Id$
2 // Author: Rene Brun 10/03/2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "Riostream.h"
13 #include "TROOT.h"
14 #include "TColorWheel.h"
15 #include "TCanvas.h"
16 #include "TText.h"
17 #include "TGraph.h"
18 #include "TArc.h"
19 #include "TLine.h"
20 #include "TColor.h"
21 #include "TMath.h"
22 
24 
25 /** \class TColorWheel
26 \ingroup gpad
27 
28 Draw the ROOT Color Wheel.
29 
30 The wheel contains the recommended 216 colors to be used in web applications.
31 The colors in the Color Wheel are created by TColor::CreateColorWheel.
32 
33 Using this color set for your text, background or graphics will give your
34 application a consistent appearance across different platforms and browsers.
35 
36 Colors are grouped by hue, the aspect most important in human perception.
37 Touching color chips have the same hue, but with different brightness and vividness.
38 
39 Colors of slightly different hues __clash__. If you intend to display
40 colors of the same hue together, you should pick them from the same group.
41 
42 Each color chip is identified by a mnemonic (e.g. kYellow) and a number.
43 The keywords, kRed, kBlue, kYellow, kPink, etc are defined in the header file __Rtypes.h__
44 that is included in all ROOT other header files. We strongly recommend to use these keywords
45 in your code instead of hardcoded color numbers, e.g.:
46 ~~~ {.cpp}
47  myObject.SetFillColor(kRed);
48  myObject.SetFillColor(kYellow-10);
49  myLine.SetLineColor(kMagenta+2);
50 ~~~
51 
52 Begin_Macro
53 {
54  TColorWheel *w = new TColorWheel();
55  w->Draw();
56 }
57 End_Macro
58 */
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// constructor
62 
63 TColorWheel::TColorWheel() :TNamed("wheel","ROOT Color Wheel")
64 {
65  fCanvas = 0;
66  fArc = 0;
67  fLine = 0;
68  fText = 0;
69  fGraph = 0;
70  fRmin = 2.1;
71  fRmax = 9.5;
72  fR0 = 4;
73  fDr = 1;
74  fRgray = 1.8;
75  fX[ 0] = fR0-0.2*fDr; fY[ 0] = 0;
76  fX[ 1] = fR0+fDr; fY[ 1] = 0.75*fDr;
77  fX[ 2] = fR0+fDr; fY[ 2] = -0.75*fDr;
78  fX[ 3] = fR0+2.2*fDr; fY[ 3] = 1.5*fDr;
79  fX[ 4] = fR0+2.2*fDr; fY[ 4] = 0;
80  fX[ 5] = fR0+2.2*fDr; fY[ 5] = -1.5*fDr;
81  fX[ 6] = fR0+3.4*fDr; fY[ 6] = 2.2*fDr;
82  fX[ 7] = fR0+3.4*fDr; fY[ 7] = 0.7*fDr;
83  fX[ 8] = fR0+3.4*fDr; fY[ 8] = -0.7*fDr;
84  fX[ 9] = fR0+3.4*fDr; fY[ 9] = -2.2*fDr;
85  fX[10] = fR0+4.6*fDr; fY[10] = 2.8*fDr;
86  fX[11] = fR0+4.6*fDr; fY[11] = 1.4*fDr;
87  fX[12] = fR0+4.6*fDr; fY[12] = 0;
88  fX[13] = fR0+4.6*fDr; fY[13] = -1.4*fDr;
89  fX[14] = fR0+4.6*fDr; fY[14] = -2.8*fDr;
90 
91  SetBit(kCanDelete);
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// destructor
96 
98 {
99  //delete fCanvas; please don't do that
100  delete fArc;
101  delete fLine;
102  delete fText;
103  delete fGraph;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// always return the color wheel
108 
110 {
111  if (px+py < 0) return 1;
112  return 0;
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// Paint the color wheel
117 
119 {
120  if (!fCanvas) {
121  fCanvas = new TCanvas("wheel","ROOT Color Wheel",10,10,400,400);
123  }
124  fCanvas->Range(-10.5,-10.5,10.5,10.5);
126  fCanvas->SetFillColor(TColor::GetColor(243,241,174));
127  AppendPad(option);
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Return the color number pointed by the mouse
132 
134 {
135  Double_t x = fCanvas->AbsPixeltoX(px);
136  Double_t y = fCanvas->AbsPixeltoY(py);
137  Int_t n = 0;
138 
139  n = InGray(x,y); if (n >= 0) return n;
140  n = InCircles (x,y,kMagenta, 0); if (n >= 0) return n;
141  n = InRectangles(x,y,kPink, 30); if (n >= 0) return n;
142  n = InCircles (x,y,kRed, 60); if (n >= 0) return n;
143  n = InRectangles(x,y,kOrange, 90); if (n >= 0) return n;
144  n = InCircles (x,y,kYellow,120); if (n >= 0) return n;
145  n = InRectangles(x,y,kSpring,150); if (n >= 0) return n;
146  n = InCircles (x,y,kGreen, 180); if (n >= 0) return n;
147  n = InRectangles(x,y,kTeal, 210); if (n >= 0) return n;
148  n = InCircles (x,y,kCyan, 240); if (n >= 0) return n;
149  n = InRectangles(x,y,kAzure, 270); if (n >= 0) return n;
150  n = InCircles (x,y,kBlue, 300); if (n >= 0) return n;
151  n = InRectangles(x,y,kViolet,330); if (n >= 0) return n;
152  return -1;
153 }
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// Return the color number pointed by the mouse
157 
159 {
160  static char info[50];
161  info[0] = 0;
162 
163  Int_t n = GetColor(px,py);
164  if (n < 0) return info;
165  TColor *color = gROOT->GetColor(n);
166  if (!color) return info;
167  Int_t r = (Int_t)(255.01*color->GetRed());
168  Int_t g = (Int_t)(255.01*color->GetGreen());
169  Int_t b = (Int_t)(255.01*color->GetBlue());
170  snprintf(info,50,"col %d, %s, r=%3d, g=%3d, b=%3d",n,color->GetName(),r,g,b);
171  return info;
172 }
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// Return the color number when the mouse point to a circle
176 
178 {
179  Double_t ang = angle*TMath::DegToRad();
180  Double_t u,v;
181  Rotate(x,y,u,v,ang);
182  Double_t r2 = 0.7*0.7*fDr*fDr;
183  for (Int_t i=0;i<15;i++) {
184  Double_t dx = u-fX[i];
185  Double_t dy = v-fY[i];
186  if (dx*dx+dy*dy < r2) return coffset+i-10;
187  }
188  return -1;
189 }
190 
191 ////////////////////////////////////////////////////////////////////////////////
192 /// Return the color number when the mouse point to the gray circle
193 
195 {
196  if (x*x+y*y > fRgray*fRgray) return -1;
198  if (ang < 0) ang += 360;
199  if (ang < 60) return kWhite;
200  if (ang < 120) return kGray;
201  if (ang < 180) return kGray+1;
202  if (ang < 240) return kGray+2;
203  if (ang < 300) return kGray+3;
204  return kBlack;
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// Return the color number when the mouse point to a rectangle
209 
211 {
212  Double_t ang = angle*TMath::DegToRad();
213  Double_t u,v;
214  Rotate(x,y,u,v,ang);
215  if (TMath::Abs(v) > 1) return -1;
216  if (u < fRmin || u > fRmax) return -1;
217  Int_t div = (Int_t)(10*(u-fRmin)/(fRmax-fRmin));
218  if (v > 0) return coffset + div+1;
219  return coffset+div-9;
220 }
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 /// Paint the color wheel
224 
225 void TColorWheel::Paint(Option_t * /*option*/)
226 {
227  if (!fArc) {
228  fArc = new TArc;
229  fLine = new TLine;
230  fText = new TText();
231  fGraph = new TGraph();
232  }
233  PaintGray();
234  PaintCircles (kMagenta, 0);
235  PaintRectangles(kPink, 30);
236  PaintCircles (kRed, 60);
238  PaintCircles (kYellow,120);
240  PaintCircles (kGreen, 180);
241  PaintRectangles(kTeal, 210);
242  PaintCircles (kCyan, 240);
243  PaintRectangles(kAzure, 270);
244  PaintCircles (kBlue, 300);
246 
247  fText->SetTextFont(72);
249  fText->SetTextAlign(11);
250  fText->SetTextSize(0.03);
251  fText->SetTextAngle(0);
252  fText->PaintText(-10.2,-10.2,"ROOT Color Wheel");
253 }
254 
255 ////////////////////////////////////////////////////////////////////////////////
256 /// Draw one color of type circle
257 
259 {
260  Double_t u,v;
261  Rotate(x,y,u,v,ang);
262  Int_t colorn = coffset+n;
263  TColor *color = gROOT->GetColor(colorn);
264  if (!color) return;
265  fArc->SetFillColor(colorn);
266  fArc->SetLineColor(14);
267  Double_t r = 0.7*fDr;
268  fArc->PaintEllipse(u,v,r,r,0,360,0);
269  fText->SetTextSize(0.03);
270  fText->SetTextAlign(22);
271  if (255*color->GetLight() <150 && n != 0) fText->SetTextColor(0);
272  if (n>0) fText->PaintText(u,v,Form("+%d",n));
273  else fText->PaintText(u,v,Form("%d", n));
274 }
275 
276 ////////////////////////////////////////////////////////////////////////////////
277 /// Draw all colors of type circle
278 
279 void TColorWheel::PaintCircles(Int_t coffset, Double_t angle) const
280 {
281  Double_t ang = TMath::DegToRad()*angle;
282  Double_t u,v,u0,v0;
283  Rotate(fR0+4.6*fDr,2.8*fDr,u0,v0,ang);
284  Rotate(fR0+5.8*fDr,2.1*fDr,u,v,ang);
285  fLine->PaintLine(u,v,u0,v0);
286  fText->SetTextAlign(22);
287  fText->SetTextFont(72);
288  fText->SetTextColor(1);
289  fText->SetTextSize(0.03);
290  Double_t tangle = angle-90;
291  if (angle == 240) tangle = -30;
292  if (angle == 300) tangle = 30;
293  TColor *col = gROOT->GetColor(coffset);
294  if (!col) return;
295  fText->SetTextAngle(tangle);
296  fText->PaintText(u,v,col->GetName());
297 
298  for (Int_t i=0;i<15;i++) {
299  PaintCircle(coffset,i-10, fX[i], fY[i], ang);
300  }
301 }
302 
303 ////////////////////////////////////////////////////////////////////////////////
304 /// Draw all colors of type rectangle
305 
306 void TColorWheel::PaintRectangles(Int_t coffset, Double_t angle) const
307 {
308  Double_t ang = TMath::DegToRad()*angle;
309  Double_t rmin = fRmin, rmax=fRmax;
310  Double_t dr = (rmax-rmin)/10;
311  Double_t dy = -1.0;
312 
313  Double_t u,v,u0,v0;
314  Rotate(rmax+0.62*dr,0,u,v,ang);
315  Rotate(rmax-dr,0.9*dy,u0,v0,ang);
316  fLine->PaintLine(u,v,u0,v0);
317  fText->SetTextAlign(22);
318  fText->SetTextFont(72);
319  fText->SetTextColor(1);
320  fText->SetTextSize(0.03);
321  Double_t tangle = angle+90;
322  if (angle == 30) tangle = -60;
323  if (angle == 90) tangle = 0;
324  if (angle == 150) tangle = 60;
325  if (angle == 210) tangle = -60;
326  if (angle == 270) tangle = 0;
327  fText->SetTextAngle(tangle);
328  TColor *color = gROOT->GetColor(coffset);
329  if (!color) return;
330  fText->PaintText(u,v,color->GetName());
331 
332  Double_t x[5],y[5];
333  Int_t n=-10;
334  for (Int_t j=0;j<2;j++) {
335  for (Int_t i=0;i<10;i++) {
336  n++;
337  Int_t colorn = coffset +n;
338  color = gROOT->GetColor(colorn);
339  Rotate(rmin+i*dr, 0,x[0],y[0],ang);
340  Rotate(rmin+i*dr, dy,x[1],y[1],ang);
341  Rotate(rmin+i*dr+dr,dy,x[2],y[2],ang);
342  Rotate(rmin+i*dr+dr, 0,x[3],y[3],ang);
343  fGraph->SetFillColor(colorn);
344  fGraph->PaintGraph(4,x,y,"f");
345  Rotate(rmin+i*dr+0.5*dr,0.5*dy,x[0],y[0],ang);
346  fText->SetTextSize(0.03);
347  fText->SetTextAlign(22);
348  if (color) {
349  if (255*color->GetLight() <110) fText->SetTextColor(0);
350  }
351  Double_t tang = angle-90;
352  if (angle > 180) tang -=180;
353  fText->SetTextAngle(tang);
354  if (n > 0) fText->PaintText(x[0],y[0],Form("+%d",n));
355  else fText->PaintText(x[0],y[0],Form("%d",n));
356  }
357  dy=1;
358  }
359 
360  Rotate(rmin,-dy,x[0],y[0],ang);
361  Rotate(rmax,-dy,x[1],y[1],ang);
362  Rotate(rmax, dy,x[2],y[2],ang);
363  Rotate(rmin, dy,x[3],y[3],ang);
364  Rotate(rmin,-dy,x[4],y[4],ang);
365  fGraph->SetLineColor(1);
366  fGraph->SetLineWidth(1);
367  fGraph->PaintGraph(5,x,y,"l");
368  fLine->SetLineWidth(1);
369  Rotate(rmin+3*dr,-dy,x[0],y[0],ang);
370  Rotate(rmin+3*dr, dy,x[1],y[1],ang);
371  fLine->PaintLine(x[0],y[0],x[1],y[1]);
372  Rotate(rmin+6*dr,-dy,x[0],y[0],ang);
373  Rotate(rmin+6*dr, dy,x[1],y[1],ang);
374  fLine->PaintLine(x[0],y[0],x[1],y[1]);
375  Rotate(rmin+9*dr,-dy,x[0],y[0],ang);
376  Rotate(rmin+9*dr, dy,x[1],y[1],ang);
377  fLine->PaintLine(x[0],y[0],x[1],y[1]);
378  Rotate(rmin+7*dr,-dy,x[0],y[0],ang);
379  Rotate(rmin+7*dr, dy,x[1],y[1],ang);
380  fLine->PaintLine(x[0],y[0],x[1],y[1]);
381  Rotate(rmin+6*dr,0,x[0],y[0],ang);
382  Rotate(rmax, 0,x[1],y[1],ang);
383  fLine->PaintLine(x[0],y[0],x[1],y[1]);
384 }
385 
386 ////////////////////////////////////////////////////////////////////////////////
387 /// Draw the gray colors + white + black
388 
390 {
391  Double_t r = fRgray;
393  fArc->PaintEllipse(0,0,r,r,0,60,0);
395  fArc->PaintEllipse(0,0,r,r,60,120,0);
396  fArc->SetFillColor(kGray+1);
397  fArc->PaintEllipse(0,0,r,r,120,180,0);
398  fArc->SetFillColor(kGray+2);
399  fArc->PaintEllipse(0,0,r,r,180,240,0);
400  fArc->SetFillColor(kGray+3);
401  fArc->PaintEllipse(0,0,r,r,240,300,0);
403  fArc->PaintEllipse(0,0,r,r,300,360,0);
404 
405  fText->SetTextAlign(22);
406  fText->SetTextFont(62);
407  fText->SetTextColor(1);
408  fText->SetTextSize(0.02);
409  fText->SetTextAngle(40);
410  fText->PaintText(0.5*r,0.3*r,"kWhite");
411  fText->SetTextAngle(0);
412  fText->PaintText(0,0.8*r,"kGray");
413  fText->SetTextColor(10);
414  fText->SetTextFont(72);
415  fText->SetTextSize(0.03);
416  fText->PaintText(-0.6*r, 0.3*r,"+1");
417  fText->PaintText(-0.6*r,-0.3*r,"+2");
418  fText->PaintText(0,-0.6*r,"+3");
419  fText->SetTextAngle(-40);
420  fText->SetTextSize(0.02);
421  fText->SetTextFont(62);
422  fText->PaintText(0.5*r,-0.35*r,"kBlack");
423 }
424 
425 ////////////////////////////////////////////////////////////////////////////////
426 /// Rotate point x,y with an angle=ang
427 
429 {
430  u = x*TMath::Cos(ang) + y*TMath::Sin(ang);
431  v = x*TMath::Sin(ang) - y*TMath::Cos(ang);
432 }
Int_t InCircles(Double_t x, Double_t y, Int_t coffset, Double_t angle) const
Return the color number when the mouse point to a circle.
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
virtual void PaintEllipse(Double_t x1, Double_t y1, Double_t r1, Double_t r2, Double_t phimin, Double_t phimax, Double_t theta, Option_t *option="")
Draw this ellipse with new coordinates.
Definition: TEllipse.cxx:527
virtual void ToggleEventStatus()
Toggle event statusbar.
Definition: TCanvas.cxx:2124
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
Int_t InGray(Double_t x, Double_t y) const
Return the color number when the mouse point to the gray circle.
Int_t InRectangles(Double_t x, Double_t y, Int_t coffset, Double_t angle) const
Return the color number when the mouse point to a rectangle.
void PaintGraph(Int_t npoints, const Double_t *x, const Double_t *y, Option_t *chopt)
Draw the (x,y) as a graph.
Definition: TGraph.cxx:1916
void Rotate(Double_t x, Double_t y, Double_t &u, Double_t &v, Double_t ang) const
Rotate point x,y with an angle=ang.
const char Option_t
Definition: RtypesCore.h:62
void PaintCircles(Int_t coffset, Double_t angle) const
Draw all colors of type circle.
Definition: Rtypes.h:61
TLine * fLine
pointer to utility arc
Definition: TColorWheel.h:46
Double_t DegToRad()
Definition: TMath.h:50
Definition: Rtypes.h:60
virtual void SetBorderMode(Short_t bordermode)
Definition: TPad.h:316
Double_t fY[15]
Definition: TColorWheel.h:43
Definition: Rtypes.h:60
Definition: Rtypes.h:62
#define gROOT
Definition: TROOT.h:340
Double_t RadToDeg()
Definition: TMath.h:49
virtual ~TColorWheel()
destructor
Definition: TColorWheel.cxx:97
int Int_t
Definition: RtypesCore.h:41
Float_t GetGreen() const
Definition: TColor.h:61
Definition: Rtypes.h:61
Definition: Rtypes.h:61
void PaintRectangles(Int_t coffset, Double_t angle) const
Draw all colors of type rectangle.
Double_t fRgray
Definition: TColorWheel.h:41
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition: TObject.cxx:164
virtual void SetTextFont(Font_t tfont=62)
Definition: TAttText.h:59
void PaintGray() const
Draw the gray colors + white + black.
Double_t x[n]
Definition: legend1.C:17
virtual void Draw(Option_t *option="")
Paint the color wheel.
Definition: Rtypes.h:62
RooCmdArg Color(Color_t color)
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
Float_t GetBlue() const
Definition: TColor.h:62
Float_t GetLight() const
Definition: TColor.h:64
Base class for several text objects.
Definition: TText.h:42
Double_t ATan2(Double_t, Double_t)
Definition: TMath.h:454
TCanvas * fCanvas
Definition: TColorWheel.h:44
Definition: Rtypes.h:62
virtual void SetTextAlign(Short_t align=11)
Definition: TAttText.h:55
Definition: Rtypes.h:60
virtual void PaintLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Draw this line with new coordinates.
Definition: TLine.cxx:380
Create an Arc.
Definition: TArc.h:29
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
ROOT::R::TRInterface & r
Definition: Object.C:4
SVector< double, 2 > v
Definition: Dict.h:5
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
Definition: Rtypes.h:62
virtual void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Set world coordinate system for the pad.
Definition: TPad.cxx:4627
virtual void Paint(Option_t *option="")
Paint the color wheel.
virtual void SetTextAngle(Float_t tangle=0)
Definition: TAttText.h:56
virtual Int_t GetColor(Int_t px, Int_t py) const
Return the color number pointed by the mouse.
Double_t fRmin
Definition: TColorWheel.h:37
char * Form(const char *fmt,...)
A simple line.
Definition: TLine.h:41
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb"...
Definition: TColor.cxx:1666
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual char * GetObjectInfo(Int_t px, Int_t py) const
Return the color number pointed by the mouse.
Double_t Cos(Double_t)
Definition: TMath.h:424
Double_t fRmax
Definition: TColorWheel.h:38
The Canvas class.
Definition: TCanvas.h:48
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
The color creation and management class.
Definition: TColor.h:23
Float_t GetRed() const
Definition: TColor.h:60
Definition: Rtypes.h:61
void PaintCircle(Int_t coffset, Int_t n, Double_t x, Double_t y, Double_t ang) const
Draw one color of type circle.
Double_t fR0
Definition: TColorWheel.h:39
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
Double_t Sin(Double_t)
Definition: TMath.h:421
virtual Int_t DistancetoPrimitive(Int_t px, Int_t py)
always return the color wheel
Definition: Rtypes.h:62
TGraph * fGraph
pointer to utility text
Definition: TColorWheel.h:48
Double_t AbsPixeltoX(Int_t px)
Definition: TPad.h:171
virtual void SetTextColor(Color_t tcolor=1)
Definition: TAttText.h:57
Definition: Rtypes.h:61
Double_t fDr
Definition: TColorWheel.h:40
TArc * fArc
Canvas used to draw the Color Wheel.
Definition: TColorWheel.h:45
virtual void SetTextSize(Float_t tsize=1)
Definition: TAttText.h:60
Double_t AbsPixeltoY(Int_t py)
Definition: TPad.h:172
virtual void PaintText(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates.
Definition: TText.cxx:740
const Int_t n
Definition: legend1.C:16
Draw the ROOT Color Wheel.
Definition: TColorWheel.h:34
Double_t fX[15]
Definition: TColorWheel.h:42
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322
Definition: Rtypes.h:62
TText * fText
pointer to utility line
Definition: TColorWheel.h:47