Skip to content

Commit 072e3c5

Browse files
committed
adding new tutorial
1 parent bff0665 commit 072e3c5

7 files changed

+329099
-0
lines changed

color_demos/_01_customized_color.C

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
// This script demo how to set customize color
3+
// color picker
4+
// https://www.w3schools.com/colors/colors_picker.asp
5+
void _01_customized_color() {
6+
7+
8+
int rgb [] = { 102, 255, 225 }; // light blue
9+
// normally, each r, g, b ranges from 0 to 255,
10+
// but in ROOT, each r, g, b range from 0 to 1.
11+
// so you have to divide them by 255.
12+
13+
// to use a customize color,
14+
// we have to find a "free" color index to register the new color
15+
// it can be done by TColor::GetFreeColorIndex()
16+
// then construct the color.
17+
18+
int myColor_idx = TColor::GetFreeColorIndex(); // get the free index.
19+
20+
TColor* myColor = new TColor( myColor_idx, rgb[0]/255., rgb[1]/255., rgb[2]/255. );
21+
// use free index, and rgb values to instantiate TColor object.
22+
23+
24+
// part 2
25+
// demo for how to use customized color.
26+
27+
TF1* func = new TF1("func", "sin(x)", -1, 1 );
28+
29+
// put in the color idx
30+
func->SetLineColor( myColor_idx );
31+
32+
func->SetLineWidth( 10 );
33+
func->Draw();
34+
}
35+

color_demos/_01_customized_color.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import ROOT
2+
3+
""" This script demo how to set customize color """
4+
5+
# color picker
6+
# https://www.w3schools.com/colors/colors_picker.asp
7+
8+
rgb = (146, 50, 195) # light purple.
9+
# normally, each r, g, b ranges from 0 to 255,
10+
# but in ROOT, each r, g, b range from 0 to 1.
11+
# so you have to divide them by 255.
12+
13+
14+
# we have to find a free color index to register our color
15+
# it can be done by TColor::GetFreeColorIndex()
16+
# then construct the color.
17+
myColor_idx = ROOT.TColor.GetFreeColorIndex()
18+
myColor = ROOT.TColor( myColor_idx, rgb[0]/255., rgb[1]/255., rgb[2]/255. );
19+
20+
21+
# just use TF1 object to demo the color.
22+
func = ROOT.TF1("func", "sin(x)", -1, 1 )
23+
func.SetLineColor( myColor_idx ) # put in the color idx.
24+
func.SetLineWidth( 10 )
25+
func.Draw()

color_demos/_02_built_in_color.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import ROOT
2+
3+
4+
# TColor Reference
5+
# https://root.cern.ch/doc/master/classTColor.html
6+
7+
if( 0 ):
8+
# to show the color_index for built-in 50 colors
9+
10+
canvas_1 = ROOT.TCanvas("canvas_1","50 basic colors");
11+
12+
canvas_1.DrawColorTable();
13+
14+
# usage: xxx.SetLineColor( color_index )
15+
16+
17+
18+
if( 0 ):
19+
20+
# there are 15 color names
21+
22+
color_names\
23+
= ( ROOT.kWhite, ROOT.kBlack, ROOT.kGray, ROOT.kRed, ROOT.kGreen,\
24+
ROOT.kBlue , ROOT.kYellow, ROOT.kMagenta, ROOT.kCyan, ROOT.kOrange,\
25+
ROOT.kSpring, ROOT.kTeal, ROOT.kAzure, ROOT.kViolet,ROOT.kPink )
26+
27+
28+
canvas_2 = ROOT.TCanvas("canvas_2","color name");
29+
canvas_2.Range( 0, 0, 5, 3 );
30+
31+
# use box to demo the colors.
32+
box = ROOT.TBox();
33+
box.SetFillStyle(1001)
34+
x1 = 0.2; x2=0.8; y1 = 0.2; y2=0.8
35+
36+
for idx in range(15):
37+
box.SetFillColor( color_names[idx] )
38+
box.DrawBox( x1, y1, x2, y2 );
39+
40+
# to check the position.
41+
# print "idx=%2d, x1 =%4.1f, x2 =%4.1f, y1 =%4.1f, y2 =%4.1f " \
42+
# %(idx, x1, x2, y1, y2 )
43+
44+
x1 +=1; x2+=1;
45+
46+
# to move to upper row.
47+
if( (idx+1) %5 ==0 ):
48+
x1 = 0.2; x2=0.8;
49+
y1+=1; y2+=1
50+
51+
52+
53+
54+
if( 1 ):
55+
canvas_3 = ROOT.TCanvas("canvas_3","color wheel", 800, 800);
56+
colorwheel = ROOT.TColorWheel();
57+
58+
colorwheel.SetCanvas( canvas_3 );
59+
colorwheel.Draw();
60+
61+
# usage: xxx.SetLineColor( ROOT.kRed+2 )
62+
pass
63+

color_demos/_03_contour.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import ROOT
2+
from array import array
3+
4+
# in this script, we discuss how to set contour layers.
5+
6+
PI = ROOT.TMath.Pi()
7+
8+
# create a canvas, and draw a function z=f(x,y).
9+
canvas_1 = ROOT.TCanvas("canvas_1","");
10+
func = ROOT.TF2("f2","cos(x)*cos(y)", -PI/2,PI/2, -PI/2, PI/2);
11+
func.SetLineWidth(2); # the mesh
12+
func.SetLineColor(ROOT.kGray);
13+
14+
if( 0 ):
15+
# example 1
16+
# by default for surf1z, we have colors for 20 layers.
17+
# and z values are equally divided.
18+
func.Draw("surf1z")
19+
20+
21+
22+
if( 0 ):
23+
# example 2
24+
# Let's use just 4 colors, and set z values are equally divided
25+
nLevels = 4
26+
func.SetContour( nLevels )
27+
func.Draw("surf1z")
28+
29+
if( 0 ):
30+
# example 3
31+
# 4 colors, but z values are non-equally divided
32+
# from the previous example or formula, we know z ranges from 0 to 1.
33+
34+
# suppose, we want to divide in the following way:
35+
# 0----0.7-----0.8------0.9------>inf
36+
# col1 | col2 | col3 | col4
37+
nLevels = 4
38+
cutValueArray = array( 'd', [ 0, 0.7, 0.8, 0.9 ] )
39+
func.SetContour( nLevels, cutValueArray )
40+
func.Draw("surf1z")
41+
42+
43+
if( 1 ):
44+
pass
45+
# example 4
46+
# customized Palette
47+
# let's say, we still want the division setting from example3
48+
# but we want to customize our colors.
49+
# we define 4 colors :
50+
# color1 = 232, 185, 171
51+
# color2 = 224, 152, 145
52+
# color3 = 203, 118, 158
53+
# color4 = 140, 95, 102
54+
# note: you can set more colors, but since we only have nLevels =4
55+
# there redundant colors will not be shown.
56+
57+
rgb_set = [ (232, 185, 171), (224, 152, 145),\
58+
(203, 118, 158), ( 140, 95, 102)]
59+
60+
myColorIdxList = []
61+
myColors = []
62+
for i in range( len(rgb_set) ):
63+
64+
myColorIdx = ROOT.TColor.GetFreeColorIndex()
65+
myColorIdxList.append( myColorIdx )
66+
67+
rgb = rgb_set[i]
68+
myColor = ROOT.TColor( myColorIdx, rgb[0]/255., rgb[1]/255., rgb[2]/255. );
69+
myColors.append( myColor )
70+
71+
colorAarry = array( 'i', myColorIdxList )
72+
ROOT.gStyle.SetPalette( len(colorAarry), colorAarry )
73+
# there are many default Palette.
74+
# https://root.cern.ch/doc/master/classTColor.html
75+
76+
# the following is the same for example3
77+
nLevels = 4
78+
cutValueArray = array( 'd', [ 0, 0.7, 0.8, 0.9 ] )
79+
func.SetContour( nLevels, cutValueArray )
80+
func.Draw("surf1z")
81+

0 commit comments

Comments
 (0)