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
24
25/** \class TColorWheel
26\ingroup gpad
27
28Draw the ROOT Color Wheel.
29
30The wheel contains the recommended 216 colors to be used in web applications.
31The colors in the Color Wheel are created by TColor::CreateColorWheel.
32
33Using this color set for your text, background or graphics will give your
34application a consistent appearance across different platforms and browsers.
35
36Colors are grouped by hue, the aspect most important in human perception.
37Touching color chips have the same hue, but with different brightness and vividness.
38
39Colors of slightly different hues __clash__. If you intend to display
40colors of the same hue together, you should pick them from the same group.
41
42Each color chip is identified by a mnemonic (e.g. kYellow) and a number.
43The keywords, kRed, kBlue, kYellow, kPink, etc are defined in the header file __Rtypes.h__
44that is included in all ROOT other header files. We strongly recommend to use these keywords
45in 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
52Begin_Macro
53{
54 TColorWheel *w = new TColorWheel();
55 cw = new TCanvas("cw","cw",0,0,400,400);
56 w->SetCanvas(cw);
57 w->Draw();
58}
59End_Macro
60*/
61
62////////////////////////////////////////////////////////////////////////////////
63/// constructor
64
65TColorWheel::TColorWheel() :TNamed("wheel","ROOT Color Wheel")
66{
67 fRmin = 2.1;
68 fRmax = 9.5;
69 fR0 = 4;
70 fDr = 1;
71 fRgray = 1.8;
72 fX[ 0] = fR0-0.2*fDr; fY[ 0] = 0;
73 fX[ 1] = fR0+fDr; fY[ 1] = 0.75*fDr;
74 fX[ 2] = fR0+fDr; fY[ 2] = -0.75*fDr;
75 fX[ 3] = fR0+2.2*fDr; fY[ 3] = 1.5*fDr;
76 fX[ 4] = fR0+2.2*fDr; fY[ 4] = 0;
77 fX[ 5] = fR0+2.2*fDr; fY[ 5] = -1.5*fDr;
78 fX[ 6] = fR0+3.4*fDr; fY[ 6] = 2.2*fDr;
79 fX[ 7] = fR0+3.4*fDr; fY[ 7] = 0.7*fDr;
80 fX[ 8] = fR0+3.4*fDr; fY[ 8] = -0.7*fDr;
81 fX[ 9] = fR0+3.4*fDr; fY[ 9] = -2.2*fDr;
82 fX[10] = fR0+4.6*fDr; fY[10] = 2.8*fDr;
83 fX[11] = fR0+4.6*fDr; fY[11] = 1.4*fDr;
84 fX[12] = fR0+4.6*fDr; fY[12] = 0;
85 fX[13] = fR0+4.6*fDr; fY[13] = -1.4*fDr;
86 fX[14] = fR0+4.6*fDr; fY[14] = -2.8*fDr;
87
89}
90
91////////////////////////////////////////////////////////////////////////////////
92/// destructor
93
95{
96 //delete fCanvas; please don't do that
97 delete fArc;
98 delete fLine;
99 delete fText;
100 delete fGraph;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// always return the color wheel
105
107{
108 if (px+py < 0) return 1;
109 return 0;
110}
111
112////////////////////////////////////////////////////////////////////////////////
113/// Paint the color wheel
114
116{
117 if (!fCanvas) {
118 fCanvas = new TCanvas("wheel","ROOT Color Wheel",10,10,400,400);
120 }
121 fCanvas->Range(-10.5,-10.5,10.5,10.5);
125}
126
127////////////////////////////////////////////////////////////////////////////////
128/// Return the color number pointed by the mouse
129
131{
134 Int_t n = 0;
135
136 n = InGray(x,y); if (n >= 0) return n;
137 n = InCircles (x,y,kMagenta, 0); if (n >= 0) return n;
138 n = InRectangles(x,y,kPink, 30); if (n >= 0) return n;
139 n = InCircles (x,y,kRed, 60); if (n >= 0) return n;
140 n = InRectangles(x,y,kOrange, 90); if (n >= 0) return n;
141 n = InCircles (x,y,kYellow,120); if (n >= 0) return n;
142 n = InRectangles(x,y,kSpring,150); if (n >= 0) return n;
143 n = InCircles (x,y,kGreen, 180); if (n >= 0) return n;
144 n = InRectangles(x,y,kTeal, 210); if (n >= 0) return n;
145 n = InCircles (x,y,kCyan, 240); if (n >= 0) return n;
146 n = InRectangles(x,y,kAzure, 270); if (n >= 0) return n;
147 n = InCircles (x,y,kBlue, 300); if (n >= 0) return n;
148 n = InRectangles(x,y,kViolet,330); if (n >= 0) return n;
149 return -1;
150}
151
152////////////////////////////////////////////////////////////////////////////////
153/// Return the color number pointed by the mouse
154
156{
157 static char info[50];
158 info[0] = 0;
159
160 Int_t n = GetColor(px,py);
161 if (n < 0) return info;
162 TColor *color = gROOT->GetColor(n);
163 if (!color) return info;
164 Int_t r = (Int_t)(255.01*color->GetRed());
165 Int_t g = (Int_t)(255.01*color->GetGreen());
166 Int_t b = (Int_t)(255.01*color->GetBlue());
167 int res = snprintf(info,sizeof(info),"col %d, %s, r=%3d, g=%3d, b=%3d",n,color->GetName(),r,g,b);
168 // check improbable error condition, suppress gcc9 warnings
169 if ((res < 0) || (res >= (int) sizeof(info)))
170 info[0] = 0;
171 return info;
172}
173
174////////////////////////////////////////////////////////////////////////////////
175/// Return the color number when the mouse point to a circle
176
178{
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{
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
225void 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();
236 PaintCircles (kRed, 60);
238 PaintCircles (kYellow,120);
240 PaintCircles (kGreen, 180);
242 PaintCircles (kCyan, 240);
244 PaintCircles (kBlue, 300);
246
247 fText->SetTextFont(72);
249 fText->SetTextAlign(11);
250 fText->SetTextSize(0.03);
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
280{
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);
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
307{
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);
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);
367 fGraph->PaintGraph(5,x,y,"l");
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);
397 fArc->PaintEllipse(0,0,r,r,120,180,0);
399 fArc->PaintEllipse(0,0,r,r,180,240,0);
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);
408 fText->SetTextSize(0.02);
409 fText->SetTextAngle(40);
410 fText->PaintText(0.5*r,0.3*r,"kWhite");
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}
#define b(i)
Definition RSha256.hxx:100
#define g(i)
Definition RSha256.hxx:105
int Int_t
Definition RtypesCore.h:45
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
@ kTeal
Definition Rtypes.h:67
@ kGray
Definition Rtypes.h:65
@ kPink
Definition Rtypes.h:67
@ kRed
Definition Rtypes.h:66
@ kOrange
Definition Rtypes.h:67
@ kBlack
Definition Rtypes.h:65
@ kGreen
Definition Rtypes.h:66
@ kMagenta
Definition Rtypes.h:66
@ kWhite
Definition Rtypes.h:65
@ kCyan
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
@ kAzure
Definition Rtypes.h:67
@ kYellow
Definition Rtypes.h:66
@ kViolet
Definition Rtypes.h:67
@ kSpring
Definition Rtypes.h:67
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint angle
#define gROOT
Definition TROOT.h:407
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
#define snprintf
Definition civetweb.c:1540
Create an Arc.
Definition TArc.h:26
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:42
virtual void SetTextAngle(Float_t tangle=0)
Set the text angle.
Definition TAttText.h:43
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:44
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:46
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:47
The Canvas class.
Definition TCanvas.h:23
virtual void ToggleEventStatus()
Toggle event statusbar.
Definition TCanvas.cxx:2417
Draw the ROOT Color Wheel.
Definition TColorWheel.h:23
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.
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
Draw one color of type circle.
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.
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
TColorWheel()
constructor
Int_t InGray(Double_t x, Double_t y) const
Return the color number when the mouse point to the gray circle.
Double_t fX[15]
X coordinates of the center of circles.
Definition TColorWheel.h:31
virtual Int_t GetColor(Int_t px, Int_t py) const
Return the color number pointed by the mouse.
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
Draw the gray colors + white + black.
char * GetObjectInfo(Int_t px, Int_t py) const override
Return the color number pointed by the mouse.
void Paint(Option_t *option="") override
Paint the color wheel.
void PaintCircles(Int_t coffset, Double_t angle) const
Draw all colors of type circle.
void Draw(Option_t *option="") override
Paint the color wheel.
Double_t fDr
Circles radius.
Definition TColorWheel.h:29
void PaintRectangles(Int_t coffset, Double_t angle) const
Draw all colors of type rectangle.
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
always return the color wheel
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.
TGraph * fGraph
! pointer to utility graph
Definition TColorWheel.h:37
~TColorWheel() override
destructor
The color creation and management class.
Definition TColor.h:19
Float_t GetRed() const
Definition TColor.h:58
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:1823
Float_t GetLight() const
Definition TColor.h:62
Float_t GetBlue() const
Definition TColor.h:60
Float_t GetGreen() const
Definition TColor.h:59
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:530
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
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:1960
Use the TLine constructor to create a simple line.
Definition TLine.h:22
virtual void PaintLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Draw this line with new coordinates.
Definition TLine.cxx:399
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:47
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:184
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:780
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:62
Double_t AbsPixeltoY(Int_t py) override
Definition TPad.h:169
Double_t AbsPixeltoX(Int_t px) override
Definition TPad.h:168
void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2) override
Set world coordinate system for the pad.
Definition TPad.cxx:5221
void SetBorderMode(Short_t bordermode) override
Definition TPad.h:321
Base class for several text objects.
Definition TText.h:22
virtual void PaintText(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates.
Definition TText.cxx:752
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:646
constexpr Double_t DegToRad()
Conversion from degree to radian: .
Definition TMath.h:79
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:594
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:588
constexpr Double_t RadToDeg()
Conversion from radian to degree: .
Definition TMath.h:72
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123