-
Notifications
You must be signed in to change notification settings - Fork 41
/
testColors.py
executable file
·212 lines (180 loc) · 6.92 KB
/
testColors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#! /usr/bin/env python
# Author: Izaak Neutelings (June 2018)
# https://ineuteli.web.cern.ch/ineuteli/public/TColor/
from math import ceil, sqrt
# import CMS_lumi as CMS_lumi, tdrstyle as tdrstyle
import ROOT
from ROOT import gROOT, gPad, gStyle, TGaxis,\
TCanvas, TBox, TText, TLatex,\
TColor, kBlack, kWhite, kGray,\
kBlue, kAzure, kCyan, kTeal, kGreen, kSpring, kYellow, kOrange, kRed, kPink, kMagenta, kViolet
ROOT.gROOT.SetBatch(ROOT.kTRUE)
color_dict = {
'kWhite': kWhite, 'kBlack': kBlack, 'kGray': kGray,
'kBlue': kBlue, 'kAzure': kAzure, 'kCyan': kCyan,
'kTeal': kTeal, 'kGreen': kGreen, 'kSpring': kSpring,
'kYellow': kYellow, 'kOrange': kOrange,
'kRed': kRed, 'kPink': kPink,
'kMagenta': kMagenta, 'kViolet': kViolet,
}
def makeTColorWheel():
#canvas = TCanvas("canvas","canvas",100,100,800,800)
colorWheel = TColorWheel()
colorWheel.Draw()
canvas = colorWheel.GetCanvas()
canvas.SetCanvasSize(1000, 1010)
canvas.SaveAs("TColorWheel.pdf")
def drawColorTable(clist=list(range(0,50)),nrow=None,ncol=None,cmax=10,tag="",label=False,RBG=False,newRBG=True,div=2):
# https://root.cern.ch/doc/master/src_2TPad_8cxx_source.html#l01611
if not ncol:
ncol = min(cmax,len(clist))
if not nrow:
nrow = 1 if len(clist)<=cmax else int(ceil(len(clist)/float(cmax)))
x1 = y1 = 0.
x2 = y2 = 20.
hs = (y2-y1)/nrow
ws = (x2-x1)/ncol
if label or RBG:
width = 170*ncol
height = 80*nrow
else:
width = 110*ncol
height = 80*nrow
scale = 400./height
if 400.<height: scale = sqrt(scale)
canvas = TCanvas("c","Fill Area colors",0,0,width,height)
canvas.SetFillColor(0)
canvas.Clear()
canvas.Range(x1,y1,x2,y2)
text = TText(0,0,"")
text.SetTextFont(61)
text.SetTextSize(0.07*scale)
text.SetTextAlign(22)
box = TBox()
for r in range(0,nrow):
ylow = y2 - hs*(r+0.1)
yup = y2 - hs*(r+0.9)
for c in range(0,ncol):
i = ncol*r+c
if i >= len(clist): break
xlow = x1 + ws*(c+0.1)
xup = x1 + ws*(c+0.9)
color = clist[ncol*r+c]
box.SetFillStyle(1001)
box.SetFillColor(color)
box.DrawBox(xlow, ylow, xup, yup)
box.SetFillStyle(0)
box.SetLineColor(1)
box.DrawBox(xlow, ylow, xup, yup)
if color==1: text.SetTextColor(0)
else: text.SetTextColor(1)
name = "%d"%color
if (isinstance(label,int) and i%div==label) or label:
name = getColorString(color)
if (not isinstance(RBG,bool) and isinstance(RBG,int) and i%div==RBG) or (RBG and not isinstance(label,int)):
name = getRGBString(color)
elif newRBG and color>=924:
name = getRGBString(color)
text.DrawText(0.5*(xlow+xup), 0.5*(ylow+yup), name)
if i >= len(clist): break
canvas.SaveAs("TColorTable%s.png"%tag)
canvas.SaveAs("TColorTable%s.pdf"%tag)
def getColorString(color):
if gROOT.GetColor(color)!=None:
name = gROOT.GetColor(color).GetName()
if name[0]=='k':
return name
#imin = -1
#dmin = 51;
#cmin = 'kWhite';
#for colkey, coli in color_dict.iteritems():
# diff = color - coli
# name = gROOT.GetColor(imin).GetName()
# if diff==0:
# return colkey;
# if abs(diff)<abs(dmin):
# imin = coli
# dmin = diff
# cmin = colkey;
#if abs(dmin)<51 and gROOT.GetColor(imin)!=None:
# return "%s%+d"%(cmin,dmin)
return "%s"%color
def getRGB(color):
return color.GetRed(), color.GetGreen(), color.GetBlue()
def getRGBString(color):
if isinstance(color,int):
color = gROOT.GetColor(color)
if not color: return "255,255,255"
return "%d,%d,%d"%(color.GetRed()*255, color.GetGreen()*255, color.GetBlue()*255)
def findClosestExistingColor(*args,**kwargs):
r,g,b = args[:3] if len(args)>2 else getRGB(gROOT.GetColor(args[0]))
cmax = kwargs.get('N',924)
dmin = 999
imin = -1
cmin = None
color = None
for i, col in findNonWhiteColors(cmax):
r2,g2,b2 = getRGB(col)
rbgdiff = dRGB(r,g,b,r2,g2,b2)
if rbgdiff<dmin:
#print " (%.3f,%.3f,%.3f) vs. %4d, (%.3f,%.3f,%.3f), %5.4f"%(r,g,b,imin,r2,g2,b2,rbgdiff)
imin, cmin, dmin = i, col, rbgdiff
return imin, cmin, dmin
def findNonWhiteColors(N=924):
for i in range(0,N):
color = gROOT.GetColor(i)
if i==kWhite:
yield kWhite, color
if color==None or getRGB(color)==(0.0,0.0,0.0):
continue
yield i, color
def dRGB(r1,g1,b1,r2,g2,b2):
return sqrt((r2-r1)**2+(g2-g1)**2+(b2-b1)**2)
def main():
#makeTColorWheel()
#drawColorTable(range(0,50))
#drawColorTable([kBlue+i for i in range(-25,25)],tag="_kBlue",label=True)
#drawColorTable([kRed+i for i in range(-25,25)],tag="_kRed",label=True)
#drawColorTable([kGreen+i for i in range(-25,25)],tag="_kGreen",label=True)
#drawColorTable([kAzure+i for i in range(-25,25)],tag="_kAzure",label=True)
#drawColorTable(range(0,1000),tag="_all",label=True)
#DYcols = [
# kOrange-4, TColor.GetColor(100,182,232), kGreen-6, TColor.GetColor(240,175,60),
# kOrange+5, kOrange, kYellow-9, kOrange+6, kOrange-5,
# kOrange-6, kOrange-8, kYellow-3,
#]
#drawColorTable(DYcols,tag="_DY",label=True,cmax=4)
#babycols = [
# TColor.GetColor( 91,187,235), TColor.GetColor(177,227,129),
# TColor.GetColor(255,134,196), TColor.GetColor(254,236,153),
# TColor.GetColor( 83,170,213),
#]
#drawColorTable(babycols,tag="_baby",label=True,cmax=4)
linecols = [
kRed+1, kBlue, kGreen+2, kOrange+7, kViolet+1, kYellow+1,
kRed, kAzure, kGreen+1, kOrange-3, kViolet+6, kYellow+1,
kPink+1, kAzure-9, kGreen-9, kOrange-9, kViolet-9, kYellow-9,
kMagenta-4, kOrange+3, kOrange+4, kOrange-7, kOrange+7, kRed-9, 5, kOrange, kCyan
]
drawColorTable(linecols,tag="_line",label=True,cmax=6)
#HTTcolors = [ TColor.GetColor(155,152,204), TColor.GetColor(248,206,104),
# TColor.GetColor(135,206,250), TColor.GetColor(100,182,232),
# TColor.GetColor(155,152,204), TColor.GetColor(100,222,106),
# TColor.GetColor(100,222,106), TColor.GetColor(248,206,104),
# TColor.GetColor(140,180,220), TColor.GetColor(200,140,220),
# TColor.GetColor(250,202,255), TColor.GetColor(222,90,106),
# TColor.GetColor(240,175,60), TColor.GetColor(222,140,106), ]
#drawColorTable(HTTcolors,tag="_HTT",label=True)
#
#colorPairs = [ ]
#for icol in HTTcolors:
# color = gROOT.GetColor(icol)
# imin, cmin, dmin = findClosestExistingColor(icol)
# r1,g1,b1 = getRGB(color)
# r2,g2,b2 = getRGB(cmin)
# #print "%4d, (%.3f,%.3f,%.3f) vs. %4d, (%.3f,%.3f,%.3f), %5.4f"%(icol,r1,g1,b1,imin,r2,g2,b2,dmin)
# colorPairs += [icol,imin]
#drawColorTable(colorPairs,tag="_HTTPairs",label=True)
#drawColorTable(colorPairs,tag="_HTTPairsRBG",label=True,RBG=0)
if __name__ == '__main__':
main()