Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TAttLine.cxx
Go to the documentation of this file.
1// @(#)root/base:$Id$
2// Author: Rene Brun 28/11/94
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, 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 "TAttLine.h"
13#include "TVirtualPad.h"
14#include "TVirtualPadPainter.h"
15#include "TVirtualPadEditor.h"
16#include "TStyle.h"
17#include "TColor.h"
18#include <cmath>
19#include <iostream>
20
21using std::sqrt;
22
23/** \class TAttLine
24\ingroup Base
25\ingroup GraphicsAtt
26
27Line Attributes class.
28
29This class is used (in general by secondary inheritance)
30by many other classes (graphics, histograms). It holds all the line attributes.
31
32## Line attributes
33Line attributes are:
34
35 - [Line Color](\ref ATTLINE1)
36 - [Line Width](\ref ATTLINE2)
37 - [Line Style](\ref ATTLINE3)
38
39\anchor ATTLINE1
40## Line Color
41The line color is a color index (integer) pointing in the ROOT
42color table.
43The line color of any class inheriting from `TAttLine` can
44be changed using the method `SetLineColor` and retrieved using the
45method `GetLineColor`.
46The following table shows the first 50 default colors.
47
48Begin_Macro
49{
50 TCanvas *c = new TCanvas("c","Fill Area colors",0,0,500,200);
51 c->DrawColorTable();
52 return c;
53}
54End_Macro
55
56### Color transparency
57`SetLineColorAlpha()`, allows to set a transparent color.
58In the following example the line color of the histogram `histo`
59is set to blue with an opacity of 35% (i.e. a transparency of 65%).
60(The color `kBlue` itself is internally stored as fully opaque.)
61
62~~~ {.cpp}
63histo->SetLineColorAlpha(kBlue, 0.35);
64~~~
65
66The transparency is available on all platforms when the flag `OpenGL.CanvasPreferGL` is set to `1`
67in `$ROOTSYS/etc/system.rootrc`, or on Mac with the Cocoa backend. On the file output
68it is visible with PDF, PNG, Gif, JPEG, SVG, TeX ... but not PostScript.
69
70Alternatively, you can call at the top of your script `gSytle->SetCanvasPreferGL();`.
71Or if you prefer to activate GL for a single canvas `c`, then use `c->SetSupportGL(true);`.
72
73\anchor ATTLINE2
74## Line Width
75The line width is expressed in pixel units.
76The line width of any class inheriting from `TAttLine` can
77be changed using the method `SetLineWidth` and retrieved using the
78method `GetLineWidth`.
79The following picture shows the line widths from 1 to 10 pixels.
80
81Begin_Macro
82{
83 TCanvas *Lw = new TCanvas("Lw","test",500,200);
84 TText t;
85 t.SetTextAlign(32);
86 t.SetTextSize(0.08);
87 Int_t i=1;
88 for (float s=0.1; s<1.0 ; s+=0.092) {
89 TLine *lh = new TLine(0.15,s,.85,s);
90 lh->SetLineWidth(i);
91 t.DrawText(0.1,s,Form("%d",i++));
92 lh->Draw();
93 }
94}
95End_Macro
96
97\anchor ATTLINE3
98## Line Style
99Line styles are identified via integer numbers. The line style of any class
100inheriting from `TAttLine` can be changed using the method
101`SetLineStyle` and retrieved using the method `GetLineStyle`.
102
103The first 10 line styles are predefined as shown on the following picture:
104
105Begin_Macro
106{
107 TCanvas *Ls = new TCanvas("Ls","test",500,200);
108 TText t;
109 t.SetTextAlign(32);
110 t.SetTextSize(0.08);
111 Int_t i=1;
112 for (float s=0.1; s<1.0 ; s+=0.092) {
113 TLine *lh = new TLine(0.15,s,.85,s);
114 lh->SetLineStyle(i);
115 lh->SetLineWidth(3);
116 t.DrawText(0.1,s,Form("%d",i++));
117 lh->Draw();
118 }
119}
120End_Macro
121
122Some line styles can be accessed via the following enum:
123
124~~~ {.cpp}
125 kSolid = 1
126 kDashed = 2
127 kDotted = 3
128 kDashDotted = 4
129~~~
130
131Additional line styles can be defined using `TStyle::SetLineStyleString`.
132For example the line style number 11 can be defined as follow:
133~~~ {.cpp}
134 gStyle->SetLineStyleString(11,"400 200");
135~~~
136Existing line styles (1 to 10) can be redefined using the same method.
137 */
138
139////////////////////////////////////////////////////////////////////////////////
140/// AttLine default constructor.
141
143{
144 if (!gStyle) {fLineColor=1; fLineWidth=1; fLineStyle=1; return;}
145 fLineColor = gStyle->GetLineColor();
146 fLineWidth = gStyle->GetLineWidth();
147 fLineStyle = gStyle->GetLineStyle();
148}
149
150////////////////////////////////////////////////////////////////////////////////
151/// AttLine normal constructor.
152/// Line attributes are taking from the argument list
153///
154/// - color : must be one of the valid color index
155/// - style : 1=solid, 2=dash, 3=dash-dot, 4=dot-dot. New styles can be
156/// defined using TStyle::SetLineStyleString.
157/// - width : expressed in pixel units
158
160{
161 fLineColor = color;
162 fLineWidth = width;
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// AttLine destructor.
168
170{
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Copy this line attributes to a new TAttLine.
175
176void TAttLine::Copy(TAttLine &attline) const
177{
178 attline.fLineColor = fLineColor;
179 attline.fLineStyle = fLineStyle;
180 attline.fLineWidth = fLineWidth;
181}
182
183////////////////////////////////////////////////////////////////////////////////
184/// Compute distance from point px,py to a line.
185/// Compute the closest distance of approach from point px,py to this line.
186/// The distance is computed in pixels units.
187///
188/// Algorithm:
189///~~~ {.cpp}
190/// A(x1,y1) P B(x2,y2)
191/// -----------------+------------------------------
192/// |
193/// |
194/// |
195/// |
196/// M(x,y)
197///
198/// Let us call a = distance AM A=a**2
199/// b = distance BM B=b**2
200/// c = distance AB C=c**2
201/// d = distance PM D=d**2
202/// u = distance AP U=u**2
203/// v = distance BP V=v**2 c = u + v
204///
205/// D = A - U
206/// D = B - V = B -(c-u)**2
207/// ==> u = (A -B +C)/2c
208///~~~
209
211{
212 Double_t xl, xt, yl, yt;
213 Double_t x = px;
214 Double_t y = py;
215 Double_t x1 = gPad->XtoAbsPixel(xp1);
216 Double_t y1 = gPad->YtoAbsPixel(yp1);
217 Double_t x2 = gPad->XtoAbsPixel(xp2);
218 Double_t y2 = gPad->YtoAbsPixel(yp2);
219 if (x1 < x2) {xl = x1; xt = x2;}
220 else {xl = x2; xt = x1;}
221 if (y1 < y2) {yl = y1; yt = y2;}
222 else {yl = y2; yt = y1;}
223 if (x < xl-2 || x> xt+2) return 9999; //following algorithm only valid in the box
224 if (y < yl-2 || y> yt+2) return 9999; //surrounding the line
225 Double_t xx1 = x - x1;
226 Double_t xx2 = x - x2;
227 Double_t x1x2 = x1 - x2;
228 Double_t yy1 = y - y1;
229 Double_t yy2 = y - y2;
230 Double_t y1y2 = y1 - y2;
231 Double_t a = xx1*xx1 + yy1*yy1;
232 Double_t b = xx2*xx2 + yy2*yy2;
233 Double_t c = x1x2*x1x2 + y1y2*y1y2;
234 if (c <= 0) return 9999;
235 Double_t v = sqrt(c);
236 Double_t u = (a - b + c)/(2*v);
237 Double_t d = std::abs(a - u*u);
238 if (d < 0) return 9999;
239
240 return Int_t(sqrt(d) - 0.5*Double_t(fLineWidth));
241}
242
243////////////////////////////////////////////////////////////////////////////////
244/// Change current line attributes if necessary.
245
246void TAttLine::Modify()
247{
248 if (gPad)
249 ModifyOn(*gPad);
250}
251
252////////////////////////////////////////////////////////////////////////////////
253/// Change current line attributes on specified pad
254
256{
257 auto pp = pad.GetPainter();
258 if (!pp)
259 return;
260
261 Bool_t normal_style = (fLineStyle > 0) && (fLineStyle < 30);
262 Bool_t normal_width = (fLineWidth >= 0) && (fLineWidth < 100);
263
264 if (normal_style && normal_width)
265 pp->SetAttLine(*this);
266 else {
267 TAttLine att1(*this);
268 if (!normal_style)
269 att1.SetLineStyle(1);
270 if (!normal_width)
271 att1.SetLineWidth(std::abs(fLineWidth % 100));
272 pp->SetAttLine(att1);
273 }
274}
275
276////////////////////////////////////////////////////////////////////////////////
277/// Reset this line attributes to default values.
278
280{
281 fLineColor = 1;
282 fLineStyle = 1;
283 fLineWidth = 1;
284}
285
286////////////////////////////////////////////////////////////////////////////////
287/// Save line attributes as C++ statement(s) on output stream out.
288
289void TAttLine::SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef, Int_t stydef, Int_t widdef)
290{
291 // save line settings if gStyle configured differently - so 1,1,1 is not default
292 Bool_t force_saving = (coldef == 1) && (stydef == 1) && (widdef == 1) && gStyle &&
293 ((gStyle->GetLineColor() != 1) || (gStyle->GetLineWidth() != 1) || (gStyle->GetLineStyle() != 1));
294
295 if ((fLineColor != coldef) || force_saving)
296 out << " " << name << "->SetLineColor(" << TColor::SavePrimitiveColor(fLineColor) << ");\n";
297 if ((fLineStyle != stydef) || force_saving)
298 out << " " << name << "->SetLineStyle(" << fLineStyle << ");\n";
299 if ((fLineWidth != widdef) || force_saving)
300 out << " " << name << "->SetLineWidth(" << fLineWidth << ");\n";
301}
302
303////////////////////////////////////////////////////////////////////////////////
304/// Invoke the DialogCanvas Line attributes.
305
307{
309}
310
311////////////////////////////////////////////////////////////////////////////////
312/// Set a transparent line color.
313/// \param lcolor defines the line color
314/// \param lalpha defines the percentage of opacity from 0. (fully transparent) to 1. (fully opaque).
315/// \note lalpha is ignored (treated as 1) if the TCanvas has no GL support activated.
316
318{
319 fLineColor = TColor::GetColorTransparent(lcolor, lalpha);
320}
321
323{
324 SetLineColor(lcolor.number());
325}
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
short Width_t
Line width (short).
Definition RtypesCore.h:98
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
short Color_t
Color number (short).
Definition RtypesCore.h:99
short Style_t
Style number (short).
Definition RtypesCore.h:96
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
char name[80]
Definition TGX11.cxx:148
externTStyle * gStyle
Definition TStyle.h:442
#define gPad
virtual void ModifyOn(TVirtualPad &pad)
virtual void Modify()
virtual void SetLineAttributes()
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Width_t fLineWidth
Line width.
Definition TAttLine.h:26
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
virtual ~TAttLine()
Style_t fLineStyle
Line style.
Definition TAttLine.h:25
void Copy(TAttLine &attline) const
Color_t fLineColor
Line color.
Definition TAttLine.h:24
Int_t DistancetoLine(Int_t px, Int_t py, Double_t xp1, Double_t yp1, Double_t xp2, Double_t yp2)
virtual void SetLineColorAlpha(Color_t lcolor, Float_t lalpha)
virtual void ResetAttLine(Option_t *option="")
Int_t number() const
Definition TColor.h:151
static Int_t GetColorTransparent(Int_t color, Float_t a)
static TString SavePrimitiveColor(Int_t ci)
static void UpdateLineAttributes(Int_t col, Int_t sty, Int_t width)
Update line attributes via the pad editor.
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual TVirtualPadPainter * GetPainter()=0
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
TCanvas * style()
Definition style.C:1