Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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 "TROOT.h"
13#include "TColorWheel.h"
14#include "TCanvas.h"
15#include "TText.h"
16#include "TGraph.h"
17#include "TArc.h"
18#include "TLine.h"
19#include "TColor.h"
20#include "TMath.h"
21#include "snprintf.h"
22
23
24/** \class TColorWheel
25\ingroup gpad
26
27Draw the ROOT Color Wheel.
28
29The wheel contains the recommended 216 colors to be used in web applications.
30The colors in the Color Wheel are created by TColor::CreateColorWheel.
31
32Using this color set for your text, background or graphics will give your
33application a consistent appearance across different platforms and browsers.
34
35Colors are grouped by hue, the aspect most important in human perception.
36Touching color chips have the same hue, but with different brightness and vividness.
37
38Colors of slightly different hues __clash__. If you intend to display
39colors of the same hue together, you should pick them from the same group.
40
41Each color chip is identified by a mnemonic (e.g. kYellow) and a number.
42The keywords, kRed, kBlue, kYellow, kPink, etc are defined in the header file __Rtypes.h__
43that is included in all ROOT other header files. We strongly recommend to use these keywords
44in your code instead of hardcoded color numbers, e.g.:
45~~~ {.cpp}
46 myObject.SetFillColor(kRed);
47 myObject.SetFillColor(kYellow-10);
48 myLine.SetLineColor(kMagenta+2);
49~~~
50
51Begin_Macro
52{
53 TColorWheel *w = new TColorWheel();
54 auto cw = new TCanvas("cw","cw",0,0,400,400);
55 w->SetCanvas(cw);
56 w->Draw();
57}
58End_Macro
59*/
60
61////////////////////////////////////////////////////////////////////////////////
62/// constructor
63
64TColorWheel::TColorWheel() :TNamed("wheel","ROOT Color Wheel")
65{
66 fRmin = 2.1;
67 fRmax = 9.5;
68 fR0 = 4;
69 fDr = 1;
70 fRgray = 1.8;
71 fX[ 0] = fR0-0.2*fDr; fY[ 0] = 0;
72 fX[ 1] = fR0+fDr; fY[ 1] = 0.75*fDr;
73 fX[ 2] = fR0+fDr; fY[ 2] = -0.75*fDr;
74 fX[ 3] = fR0+2.2*fDr; fY[ 3] = 1.5*fDr;
75 fX[ 4] = fR0+2.2*fDr; fY[ 4] = 0;
76 fX[ 5] = fR0+2.2*fDr; fY[ 5] = -1.5*fDr;
77 fX[ 6] = fR0+3.4*fDr; fY[ 6] = 2.2*fDr;
78 fX[ 7] = fR0+3.4*fDr; fY[ 7] = 0.7*fDr;
79 fX[ 8] = fR0+3.4*fDr; fY[ 8] = -0.7*fDr;
80 fX[ 9] = fR0+3.4*fDr; fY[ 9] = -2.2*fDr;
81 fX[10] = fR0+4.6*fDr; fY[10] = 2.8*fDr;
82 fX[11] = fR0+4.6*fDr; fY[11] = 1.4*fDr;
83 fX[12] = fR0+4.6*fDr; fY[12] = 0;
84 fX[13] = fR0+4.6*fDr; fY[13] = -1.4*fDr;
85 fX[14] = fR0+4.6*fDr; fY[14] = -2.8*fDr;
86
87 SetBit(kCanDelete);
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// destructor
92
94{
95 //delete fCanvas; please don't do that
96 delete fArc;
97 delete fLine;
98 delete fText;
99 delete fGraph;
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// always return the color wheel
104
106{
107 if (px+py < 0) return 1;
108 return 0;
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Paint the color wheel
113
114void TColorWheel::Draw(Option_t *option)
115{
116 if (!fCanvas) {
117 fCanvas = new TCanvas("wheel","ROOT Color Wheel",10,10,400,400);
118 fCanvas->ToggleEventStatus();
119 }
120 fCanvas->Range(-10.5,-10.5,10.5,10.5);
121 fCanvas->SetBorderMode(0);
122 fCanvas->SetFillColor(TColor::GetColor(243,241,174));
123 AppendPad(option);
124}
125
126////////////////////////////////////////////////////////////////////////////////
127/// Return the color number pointed by the mouse
128
130{
131 Double_t x = fCanvas->AbsPixeltoX(px);
132 Double_t y = fCanvas->AbsPixeltoY(py);
133 Int_t n = 0;
134
135 n = InGray(x,y); if (n >= 0) return n;
136 n = InCircles (x,y,kMagenta, 0); if (n >= 0) return n;
137 n = InRectangles(x,y,kPink, 30); if (n >= 0) return n;
138 n = InCircles (x,y,kRed, 60); if (n >= 0) return n;
139 n = InRectangles(x,y,kOrange, 90); if (n >= 0) return n;
140 n = InCircles (x,y,kYellow,120); if (n >= 0) return n;
141 n = InRectangles(x,y,kSpring,150); if (n >= 0) return n;
142 n = InCircles (x,y,kGreen, 180); if (n >= 0) return n;
143 n = InRectangles(x,y,kTeal, 210); if (n >= 0) return n;
144 n = InCircles (x,y,kCyan, 240); if (n >= 0) return n;
145 n = InRectangles(x,y,kAzure, 270); if (n >= 0) return n;
146 n = InCircles (x,y,kBlue, 300); if (n >= 0) return n;
147 n = InRectangles(x,y,kViolet,330); if (n >= 0) return n;
148 return -1;
149}
150
151////////////////////////////////////////////////////////////////////////////////
152/// Return the color number pointed by the mouse
153
154char *TColorWheel::GetObjectInfo(Int_t px, Int_t py) const
155{
156 static char info[50];
157 info[0] = 0;
158
159 Int_t n = GetColor(px,py);
160 if (n < 0) return info;
161 TColor *color = gROOT->GetColor(n);
162 if (!color) return info;
163 Int_t r = (Int_t)(255.01*color->GetRed());
164 Int_t g = (Int_t)(255.01*color->GetGreen());
165 Int_t b = (Int_t)(255.01*color->GetBlue());
166 int res = snprintf(info,sizeof(info),"col %d, %s, r=%3d, g=%3d, b=%3d",n,color->GetName(),r,g,b);
167 // check improbable error condition, suppress gcc9 warnings
168 if ((res < 0) || (res >= (int) sizeof(info)))
169 info[0] = 0;
170 return info;
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Return the color number when the mouse point to a circle
175
177{
178 Double_t ang = angle*TMath::DegToRad();
179 Double_t u,v;
180 Rotate(x,y,u,v,ang);
181 Double_t r2 = 0.7*0.7*fDr*fDr;
182 for (Int_t i=0;i<15;i++) {
183 Double_t dx = u-fX[i];
184 Double_t dy = v-fY[i];
185 if (dx*dx+dy*dy < r2) return coffset+i-10;
186 }
187 return -1;
188}
189
190////////////////////////////////////////////////////////////////////////////////
191/// Return the color number when the mouse point to the gray circle
192
194{
195 if (x*x+y*y > fRgray*fRgray) return -1;
197 if (ang < 0) ang += 360;
198 if (ang < 60) return kWhite;
199 if (ang < 120) return kGray;
200 if (ang < 180) return kGray+1;
201 if (ang < 240) return kGray+2;
202 if (ang < 300) return kGray+3;
203 return kBlack;
204}
205
206////////////////////////////////////////////////////////////////////////////////
207/// Return the color number when the mouse point to a rectangle
208
210{
211 Double_t ang = angle*TMath::DegToRad();
212 Double_t u,v;
213 Rotate(x,y,u,v,ang);
214 if (TMath::Abs(v) > 1) return -1;
215 if (u < fRmin || u > fRmax) return -1;
216 Int_t div = (Int_t)(10*(u-fRmin)/(fRmax-fRmin));
217 if (v > 0) return coffset + div+1;
218 return coffset+div-9;
219}
220
221////////////////////////////////////////////////////////////////////////////////
222/// Paint the color wheel
223
224void TColorWheel::Paint(Option_t * /*option*/)
225{
226 if (!fArc) {
227 fArc = new TArc;
228 fLine = new TLine;
229 fText = new TText();
230 fGraph = new TGraph();
231 }
232 PaintGray();
235 PaintCircles (kRed, 60);
237 PaintCircles (kYellow,120);
239 PaintCircles (kGreen, 180);
241 PaintCircles (kCyan, 240);
243 PaintCircles (kBlue, 300);
245
246 fText->SetTextFont(72);
247 fText->SetTextColor(kBlue);
248 fText->SetTextAlign(11);
249 fText->SetTextSize(0.03);
250 fText->SetTextAngle(0);
251 fText->PaintText(-10.2,-10.2,"ROOT Color Wheel");
252}
253
254////////////////////////////////////////////////////////////////////////////////
255/// Draw one color of type circle
256
258{
259 Double_t u,v;
260 Rotate(x,y,u,v,ang);
261 Int_t colorn = coffset+n;
262 TColor *color = gROOT->GetColor(colorn);
263 if (!color) return;
264 fArc->SetFillColor(colorn);
265 fArc->SetLineColor(14);
266 Double_t r = 0.7*fDr;
267 fArc->PaintEllipse(u,v,r,r,0,360,0);
268 fText->SetTextSize(0.03);
269 fText->SetTextAlign(22);
270 if (255*color->GetLight() <150 && n != 0) fText->SetTextColor(0);
271 if (n>0) fText->PaintText(u,v,Form("+%d",n));
272 else fText->PaintText(u,v,Form("%d", n));
273}
274
275////////////////////////////////////////////////////////////////////////////////
276/// Draw all colors of type circle
277
278void TColorWheel::PaintCircles(Int_t coffset, Double_t angle) const
279{
280 Double_t ang = TMath::DegToRad()*angle;
281 Double_t u,v,u0,v0;
282 Rotate(fR0+4.6*fDr,2.8*fDr,u0,v0,ang);
283 Rotate(fR0+5.8*fDr,2.1*fDr,u,v,ang);
284 fLine->PaintLine(u,v,u0,v0);
285 fText->SetTextAlign(22);
286 fText->SetTextFont(72);
287 fText->SetTextColor(1);
288 fText->SetTextSize(0.03);
289 Double_t tangle = angle-90;
290 if (angle == 240) tangle = -30;
291 if (angle == 300) tangle = 30;
292 TColor *col = gROOT->GetColor(coffset);
293 if (!col) return;
294 fText->SetTextAngle(tangle);
295 fText->PaintText(u,v,col->GetName());
296
297 for (Int_t i=0;i<15;i++) {
298 PaintCircle(coffset,i-10, fX[i], fY[i], ang);
299 }
300}
301
302////////////////////////////////////////////////////////////////////////////////
303/// Draw all colors of type rectangle
304
305void TColorWheel::PaintRectangles(Int_t coffset, Double_t angle) const
306{
307 Double_t ang = TMath::DegToRad()*angle;
308 Double_t rmin = fRmin, rmax=fRmax;
309 Double_t dr = (rmax-rmin)/10;
310 Double_t dy = -1.0;
311
312 Double_t u,v,u0,v0;
313 Rotate(rmax+0.62*dr,0,u,v,ang);
314 Rotate(rmax-dr,0.9*dy,u0,v0,ang);
315 fLine->PaintLine(u,v,u0,v0);
316 fText->SetTextAlign(22);
317 fText->SetTextFont(72);
318 fText->SetTextColor(1);
319 fText->SetTextSize(0.03);
320 Double_t tangle = angle+90;
321 if (angle == 30) tangle = -60;
322 if (angle == 90) tangle = 0;
323 if (angle == 150) tangle = 60;
324 if (angle == 210) tangle = -60;
325 if (angle == 270) tangle = 0;
326 fText->SetTextAngle(tangle);
327 TColor *color = gROOT->GetColor(coffset);
328 if (!color) return;
329 fText->PaintText(u,v,color->GetName());
330
331 Double_t x[5],y[5];
332 Int_t n=-10;
333 for (Int_t j=0;j<2;j++) {
334 for (Int_t i=0;i<10;i++) {
335 n++;
336 Int_t colorn = coffset +n;
338 Rotate(rmin+i*dr, 0,x[0],y[0],ang);
339 Rotate(rmin+i*dr, dy,x[1],y[1],ang);
340 Rotate(rmin+i*dr+dr,dy,x[2],y[2],ang);
341 Rotate(rmin+i*dr+dr, 0,x[3],y[3],ang);
342 fGraph->SetFillColor(colorn);
343 fGraph->PaintGraph(4,x,y,"f");
344 Rotate(rmin+i*dr+0.5*dr,0.5*dy,x[0],y[0],ang);
345 fText->SetTextSize(0.03);
346 fText->SetTextAlign(22);
347 if (color) {
348 if (255*color->GetLight() <110) fText->SetTextColor(0);
349 }
350 Double_t tang = angle-90;
351 if (angle > 180) tang -=180;
352 fText->SetTextAngle(tang);
353 if (n > 0) fText->PaintText(x[0],y[0],Form("+%d",n));
354 else fText->PaintText(x[0],y[0],Form("%d",n));
355 }
356 dy=1;
357 }
358
359 Rotate(rmin,-dy,x[0],y[0],ang);
360 Rotate(rmax,-dy,x[1],y[1],ang);
361 Rotate(rmax, dy,x[2],y[2],ang);
362 Rotate(rmin, dy,x[3],y[3],ang);
363 Rotate(rmin,-dy,x[4],y[4],ang);
364 fGraph->SetLineColor(1);
365 fGraph->SetLineWidth(1);
366 fGraph->PaintGraph(5,x,y,"l");
367 fLine->SetLineWidth(1);
368 Rotate(rmin+3*dr,-dy,x[0],y[0],ang);
369 Rotate(rmin+3*dr, dy,x[1],y[1],ang);
370 fLine->PaintLine(x[0],y[0],x[1],y[1]);
371 Rotate(rmin+6*dr,-dy,x[0],y[0],ang);
372 Rotate(rmin+6*dr, dy,x[1],y[1],ang);
373 fLine->PaintLine(x[0],y[0],x[1],y[1]);
374 Rotate(rmin+9*dr,-dy,x[0],y[0],ang);
375 Rotate(rmin+9*dr, dy,x[1],y[1],ang);
376 fLine->PaintLine(x[0],y[0],x[1],y[1]);
377 Rotate(rmin+7*dr,-dy,x[0],y[0],ang);
378 Rotate(rmin+7*dr, dy,x[1],y[1],ang);
379 fLine->PaintLine(x[0],y[0],x[1],y[1]);
380 Rotate(rmin+6*dr,0,x[0],y[0],ang);
381 Rotate(rmax, 0,x[1],y[1],ang);
382 fLine->PaintLine(x[0],y[0],x[1],y[1]);
383}
384
385////////////////////////////////////////////////////////////////////////////////
386/// Draw the gray colors + white + black
387
388void TColorWheel::PaintGray() const
389{
390 Double_t r = fRgray;
391 fArc->SetFillColor(kWhite);
392 fArc->PaintEllipse(0,0,r,r,0,60,0);
393 fArc->SetFillColor(kGray);
394 fArc->PaintEllipse(0,0,r,r,60,120,0);
395 fArc->SetFillColor(kGray+1);
396 fArc->PaintEllipse(0,0,r,r,120,180,0);
397 fArc->SetFillColor(kGray+2);
398 fArc->PaintEllipse(0,0,r,r,180,240,0);
399 fArc->SetFillColor(kGray+3);
400 fArc->PaintEllipse(0,0,r,r,240,300,0);
401 fArc->SetFillColor(kBlack);
402 fArc->PaintEllipse(0,0,r,r,300,360,0);
403
404 fText->SetTextAlign(22);
405 fText->SetTextFont(62);
406 fText->SetTextColor(1);
407 fText->SetTextSize(0.02);
408 fText->SetTextAngle(40);
409 fText->PaintText(0.5*r,0.3*r,"kWhite");
410 fText->SetTextAngle(0);
411 fText->PaintText(0,0.8*r,"kGray");
412 fText->SetTextColor(10);
413 fText->SetTextFont(72);
414 fText->SetTextSize(0.03);
415 fText->PaintText(-0.6*r, 0.3*r,"+1");
416 fText->PaintText(-0.6*r,-0.3*r,"+2");
417 fText->PaintText(0,-0.6*r,"+3");
418 fText->SetTextAngle(-40);
419 fText->SetTextSize(0.02);
420 fText->SetTextFont(62);
421 fText->PaintText(0.5*r,-0.35*r,"kBlack");
422}
423
424////////////////////////////////////////////////////////////////////////////////
425/// Rotate point x,y with an angle=ang
426
428{
429 u = x*TMath::Cos(ang) + y*TMath::Sin(ang);
430 v = x*TMath::Sin(ang) - y*TMath::Cos(ang);
431}
ROOT::R::TRInterface & r
Definition Object.C:4
#define b(i)
Definition RSha256.hxx:100
#define g(i)
Definition RSha256.hxx:105
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
@ kTeal
Definition Rtypes.h:68
@ kGray
Definition Rtypes.h:66
@ kPink
Definition Rtypes.h:68
@ kRed
Definition Rtypes.h:67
@ kOrange
Definition Rtypes.h:68
@ kBlack
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:67
@ kMagenta
Definition Rtypes.h:67
@ kWhite
Definition Rtypes.h:66
@ kCyan
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:67
@ kAzure
Definition Rtypes.h:68
@ kYellow
Definition Rtypes.h:67
@ kViolet
Definition Rtypes.h:68
@ kSpring
Definition Rtypes.h:68
@ kCanDelete
Definition TObject.h:375
#define gROOT
Definition TROOT.h:417
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2496
#define snprintf
Definition civetweb.c:1579
Int_t InRectangles(Double_t x, Double_t y, Int_t coffset, Double_t angle) const
TCanvas * fCanvas
! Canvas used to draw the Color Wheel
Definition TColorWheel.h:33
TArc * fArc
! pointer to utility arc
Definition TColorWheel.h:34
TLine * fLine
! pointer to utility line
Definition TColorWheel.h:35
Double_t fRgray
Maximum radius of gray circle.
Definition TColorWheel.h:30
void PaintCircle(Int_t coffset, Int_t n, Double_t x, Double_t y, Double_t ang) const
Int_t InCircles(Double_t x, Double_t y, Int_t coffset, Double_t angle) const
virtual Int_t GetColor(Int_t px, Int_t py) const
Double_t fR0
Minimum radius for circles.
Definition TColorWheel.h:28
Double_t fY[15]
Y coordinates of the center of circles.
Definition TColorWheel.h:32
Int_t InGray(Double_t x, Double_t y) const
Double_t fX[15]
X coordinates of the center of circles.
Definition TColorWheel.h:31
Double_t fRmin
Minimum radius for rectangles.
Definition TColorWheel.h:26
TText * fText
! pointer to utility text
Definition TColorWheel.h:36
Double_t fRmax
Maximum radius for rectangles.
Definition TColorWheel.h:27
void PaintGray() const
char * GetObjectInfo(Int_t px, Int_t py) const override
Returns string containing info about the object at position (px,py).
void Paint(Option_t *option="") override
This method must be overridden if a class wants to paint itself.
void PaintCircles(Int_t coffset, Double_t angle) const
void Draw(Option_t *option="") override
Default Draw method for all objects.
Double_t fDr
Circles radius.
Definition TColorWheel.h:29
void PaintRectangles(Int_t coffset, Double_t angle) const
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Computes distance from point (px,py) to the object.
void Rotate(Double_t x, Double_t y, Double_t &u, Double_t &v, Double_t ang) const
TGraph * fGraph
! pointer to utility graph
Definition TColorWheel.h:37
~TColorWheel() override
static Int_t GetColor(const char *hexcolor)
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:204
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Double_t ATan2(Double_t y, Double_t x)
Returns the principal value of the arc tangent of y/x, expressed in radians.
Definition TMath.h:657
constexpr Double_t DegToRad()
Conversion from degree to radian: .
Definition TMath.h:82
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:605
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:599
constexpr Double_t RadToDeg()
Conversion from radian to degree: .
Definition TMath.h:75
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122