-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_b.py
729 lines (606 loc) · 27.7 KB
/
color_b.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# updated python code to be compatible with python 3+ (print statements, etc)
# rlc color_b.py version 8.0
# Copyright (c) 2004 Robert L. Campbell
# added user defined colors for 3 color ramp- Mark A. Wall
# added selection string to color names to avoid overlap between different selections
import colorsys,sys,re
from pymol import cmd
# main function called from within PyMOL
def color_b(selection='all',item='b',mode='hist',gradient='bgr',nbins=11,sat=1.,value=1.,minimum='',maximum='',user_rgb=''):
"""
AUTHOR
Robert L. Campbell with enhancements from James Stroud
USAGE
color_b selection='sel',item='b', 'q', 'partial_charge' or 'formal_charge'
gradient='bgr' or 'rgb' or 'bwr' or 'rwb' or 'bmr' or 'rmb' or
'rw' or 'wr' or 'ry' or 'yr' or 'gw' or 'wg' or 'bw' or wb' or
'gy' or 'yg' or 'gray' or 'reversegray' or 'user'
mode='hist' or 'ramp' (default is 'hist')
[minimum=''],[maximum=20.],
nbins=11, sat=1.0, value=1.0,
user_rgb = '(r1,g1,b1,r2,g2,b2,r3,g3,b3') [for use with gradient=user]
The "item" argument allows specifying 'b', 'q', 'partial_charge'
or 'formal_charge'as the item to color on. The "color_q" function
is really just the same as "color_b item=q".
This function allows coloring of a selection as a function of
B-value or occupancy, following a gradient of colours. The
gradients can be:
'bgr': blue -> green -> red
'rgb': red -> green -> blue
'bwr': blue -> white -> red
'rwb': red -> white -> blue
'bmr': blue -> magenta -> red
'rmb': red -> magenta -> blue
'rw' : red -> white
'wr' : white -> red
'ry' : red -> yellow
'yr' : yellow -> red
'gw' : green -> white
'wg' : white -> green
'bw' : blue -> white
'wb' : white -> blue
'gy' : green -> yellow
'yg' : yellow -> green
'gray' : black -> white
'reversegray' : white -> black
'user' : user defined in this script
('rainbow' and 'reverserainbow' can be used as synonyms for
'bgr' and 'rgb' respectively and 'grey' can be used as a synonym for 'gray').
User-defined gradients are entered on the command line in
parentheses as either integers between 0 and 255 or floats between
0 and 1. If any one value is larger than 1, then it is assumed
that all are being entered as integers between 0 and 255. Hence one can type:
color_b selection, gradient=user, user_rgb=(0,0,1, 0,.5,1., 1.,.5,0.)
or
color_b selection, gradient=user, user_rgb=(0,0,255, 0,128,255, 255,128,0.)
The division of B-value ranges can in either of two modes: 'hist' or
'ramp'. 'hist' is like a histogram (equal-sized B-value increments
leading to unequal numbers of atoms in each bin). 'ramp' as a ramp
of B-value ranges with the ranges chosen to provide an equal number
of atoms in each group.
You can also specify the lower or upper limits of the data used to determine
the color bins (minimum,maximum). e.g. color_b my_molecule, minimum=15., maximum=25.
You can also specify the saturation and value (i.e. the "s" and "v"
in the "HSV" color scheme) to be used for the gradient. The defaults
are 1.0 for both "sat" and "value".
In the case of the gray scale gradients, "sat" sets the minimum intensity
(normally black) and "value" sets the maximum (normally white)
usage:
from within PyMOL do "run color_b.py" to load the function definition.
Then you can use for example:
color_b (c. a | c. b),mode=ramp,gradient=bwr,nbins=30,sat=.5, value=1.
to color chains A and B with the Blue-White-Red gradient in 30 colors of equal
numbers of atoms in each color.
"""
nbins=int(nbins)
sat=float(sat)
value=float(value)
# make sure sat and value are in the range 0-1.0
sat = min(sat, 1.0)
sat = max(sat, 0.0)
value = min(value, 1.0)
value = max(value, 0.0)
if gradient == 'user' and user_rgb == '':
user_rgb = '50,50,195, 245,245,20, 255,20,20'
# make sure lowercase
gradient.lower()
mode.lower()
# Sanity checking
if nbins == 1:
print ("\n WARNING: You specified nbins=1, which doesn't make sense...resetting nbins=11\n")
nbins=11
if mode not in ('hist','ramp'):
print ("\n WARNING: Unknown mode ",mode, " -----> Nothing done.\n")
return
elif gradient not in ('bgr','rgb','rainbow','reverserainbow','bwr','rwb','user',
'bmr','rmb','rw','wr','ry','yr','gw','wg','bw','wb','gy',
'yg','gray','grey','reversegray','reversegrey','user'):
print ("\n WARNING: Unknown gradient: ",gradient, " -----> Nothing done.\n")
return
print ("MODE, GRADIENT, NBINS:", mode,gradient, nbins)
# get list of B-factors from selection
m = cmd.get_model(selection)
sel = []
b_list = []
if len(m.atom) == 0:
print ("Sorry, no atoms selected")
else:
if item == 'b':
for i in range(len(m.atom)):
b_list.append(m.atom[i].b)
elif item == 'q':
for i in range(len(m.atom)):
b_list.append(m.atom[i].q)
elif item == 'partial_charge':
for i in range(len(m.atom)):
b_list.append(m.atom[i].partial_charge)
elif item == 'formal_charge':
for i in range(len(m.atom)):
b_list.append(m.atom[i].formal_charge)
else:
print ("Not configured to work on item %s" % item)
return
max_b = max(b_list)
min_b = min(b_list)
print ("Minimum and Maximum B-values: ", min_b, max_b)
if mode == 'ramp':
# color in bins of equal numbers of atoms
b_list.sort()
# subtract 0.1 from the lowest B in order to ensure that the single
# atom with the lowest B value doesn't get omitted
# b_list[0] = b_list[0] - 0.1
bin_num = int(len(b_list)/nbins)
# sel.append(selection + " and (b < " + str(b_list[bin_num]) + " or b = " + str(b_list[bin_num]) + ")")
sel.append(selection + " and (%s < %4.4g" % (item,b_list[bin_num]) + " or %s = %4.4g" % (item,b_list[bin_num]) + ")")
for j in range(1,nbins):
# sel.append(selection + " and b > " + str(b_list[j*bin_num]))
sel.append(selection + " and %s > %4.4g" % (item,b_list[j*bin_num]))
#print ("Color select: ",sel[j])
elif mode == 'hist':
# check if minimum or maximum was specified and use the entered values
if minimum != '':
min_b = float(minimum)
if maximum != '':
max_b = float(maximum)
# histogram:
# color in bins of equal B-value ranges
# subtract 0.1 from the lowest B in order to ensure that the single
# atom with the lowest B value doesn't get omitted
bin_width = (max_b - min_b)/nbins
sel.append(selection + " and (%s < %4.4g" % (item,min_b + bin_width) + " or %s = %4.4g" % (item,min_b + bin_width) + ")")
for j in range(1,nbins):
sel.append(selection + " and %s > %4.4g" % (item,min_b + j*bin_width))
#print ("Color select: ",sel[j])
# call the function to create the gradient which returns a list of colours
colours = make_gradient(sel,gradient,nbins,sat,value, user_rgb)
# do the colouring now
for j in range(nbins):
print ("Color select: ",sel[j])
cmd.color(colours[j],sel[j])
def color_q(selection="all",mode="hist",gradient="bgr",nbins=11,sat=1.,value=1.,minimum='',maximum=''):
"""
USAGE
color_q(selection,gradient,mode,nbins,sat,value) ='sel',
gradient='bgr' or 'rgb' or 'bwr' or 'rwb' or 'bmr' or 'rmb'
'rw' or 'wr','gw' or 'wg' or 'bw' or 'wb' or 'gy' or 'yg' or 'gray' or 'reversegray' or 'user'
mode='hist' or 'ramp', q0=0.,q1=1.0,
nbins=11, sat=1.0, value=1.0)
This function allows coloring of a selection as a function of
occupancy. See color_b for details.
"""
item='q'
color_b(selection,item,mode,gradient,nbins,sat,value,minimum,maximum)
# function for creating the gradient
def make_gradient(sel,gradient,nbins,sat,value,user_rgb):
if gradient == 'bgr' or gradient == 'rainbow':
gradient = 'bgr'
col=[]
coldesc=[]
nbins = int(nbins)
for j in range(nbins):
# must append the str(sel[j]) to the color name so that it is unique
# for the selection
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# coldesc.append('col' + str(sel[j]) + str(j))
# create colors using hsv scale (fractional) starting at blue(.6666667)
# through red(0.00000) in intervals of .6666667/(nbins -1) (the "nbins-1"
# ensures that the last color is, in fact, red (0)
# rewrote this to use the colorsys module to convert hsv to rgb
hsv = (colorsys.TWO_THIRD - colorsys.TWO_THIRD * float(j) / (nbins-1), sat, value)
#convert to rgb and append to color list
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
#cmd.set_color("col" + gradient + str(j),col[j])
#print col[j],"defined as ", "col"+str(j)+str(sel[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
#print col[j],"defined as ", "col"+str(j)
elif gradient == 'user':
# --------------------------------------------
# Modified color ramp by Mark Wall 2007.07.20
# --------------------------------------------
# assign 3-color ramp values (rgb 0-255)
# could easily assign RGB color values between 0.0 and 1.0 below
# !!! Black must be at least 1,0,0 or div by zero error !!!
#
#r1, g1, b1 = 255, 255, 225 # low white
#r1, g1, b1 = 170, 170, 170 # low gray
user_rgb = re.compile('[\[\](){}]').sub('',user_rgb)
user_rgb_fract = 0
try:
r1,g1,b1, r2,g2,b2, r3,g3,b3 = map(int,user_rgb.split(','))
except ValueError:
r1,g1,b1, r2,g2,b2, r3,g3,b3 = map(float,user_rgb.split(','))
user_rgb_fract = 1
print ('user_rgb', r1,g1,b1, r2,g2,b2, r3,g3,b3)
# r1, g1, b1 = 50, 50, 195 # low med blue
# r2, g2, b2 = 245, 245, 20 # mid yellow
# r3, g3, b3 = 255, 20, 20 # high red
#
#r1, g1, b1 = 255, 20, 20 # low red
#r2, g2, b2 = 150, 150, 20 # mid yellow
#r3, g3, b3 = 20, 20, 195 # high med blue
#
#r1, g1, b1 = 1, 0, 0 # low black
#r2, g2, b2 = 155, 155, 155 # mid gray
#r3, g3, b3 = 255, 255, 255 # high white
#
#r1, g1, b1 = 0, 50, 200 # low blue
#r2, g2, b2 = 1, 0, 0 # mid black
#r3, g3, b3 = 255, 255, 20 # high yellow
#
#r1, g1, b1 = 0, 0, 1 # low black
#r2, g2, b2 = 200, 0, 0 # mid red
#r3, g3, b3 = 255, 255, 0 # high yellow
#
#r1, g1, b1 = 180, 170, 170 # low gray
#r2, g2, b2 = 250, 90, 40 # mid orange
#r3, g3, b3 = 255, 255, 0 # high yellow
#
#r1, g1, b1 = 235, 255, 255 # low white
#r2, g2, b2 = 55, 255, 255 # mid cyan
#r3, g3, b3 = 0, 0, 180 # high blue
#
# change color values to fractions
#
if max(r1,g1,b1,r2,g2,b2,r3,g3,b3) > 1:
r1, g1, b1 = float(r1)/255.0, float(g1)/255.0, float(b1)/255.0
r2, g2, b2 = float(r2)/255.0, float(g2)/255.0, float(b2)/255.0
r3, g3, b3 = float(r3)/255.0, float(g3)/255.0, float(b3)/255.0
col=[]
coldesc=[]
# print "r1,g1,b1, r2,g2,b2, r3,g3,b3", r1,g1,b1, r2,g2,b2, r3,g3,b3
for j in range(nbins//2):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from low to mid
rgb = [r1*((float(nbins)-float(j)*2.0)/float(nbins))+r2*(float(j)*2.0/float(nbins)), \
g1*((float(nbins)-float(j)*2.0)/float(nbins))+g2*(float(j)*2.0/float(nbins)), \
b1*((float(nbins)-float(j)*2.0)/float(nbins))+b2*(float(j)*2.0/float(nbins))]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
print (j,"rgb: %4.3f %4.3f %4.3f"% rgb)
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
for j in range(nbins//2,nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from mid to high
rgb = [r2*((float(nbins)-((float(j+1)-float(nbins)/2.0)*2.0))/float(nbins))+r3*(((float(j+1)-float(nbins)/2.0)*2.0)/float(nbins)), \
g2*((float(nbins)-((float(j+1)-float(nbins)/2.0)*2.0))/float(nbins))+g3*(((float(j+1)-float(nbins)/2.0)*2.0)/float(nbins)), \
b2*((float(nbins)-((float(j+1)-float(nbins)/2.0)*2.0))/float(nbins))+b3*(((float(j+1)-float(nbins)/2.0)*2.0)/float(nbins))]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
print (j,"rgb: %4.3f %4.3f %4.3f"% rgb)
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'rgb' or gradient == 'reverserainbow':
gradient = 'rgb'
col=[]
coldesc=[]
for j in range(nbins):
# must append the str(sel[j]) to the color name so that it is unique
# for the selection
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors using hsv scale (fractional) starting at red(.00000)
# through blue(0.66667) in intervals of .6666667/(nbins -1) (the "nbins-1"
# ensures that the last color is, in fact, red (0)
# rewrote this to use the colorsys module to convert hsv to rgb
hsv = (colorsys.TWO_THIRD * float(j) / (nbins-1), sat, value)
#convert to rgb and append to color list
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'bmr':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from blue through magenta to red
rgb = [min(1.0, float(j)*2/(nbins-1)), 0.0, min(1.0, float(nbins-j-1)*2/(nbins-1))]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'rmb':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from red through magenta to blue
rgb = [min(1.0, float(nbins-j-1)*2/(nbins-1)), 0.0, min(1.0, float(j)*2/(nbins-1))]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'rw':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from red through white
rgb = [1.0, float(j)/(nbins-1), float(j)/(nbins-1)]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'wr':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from white through red
rgb = [1.0, float(nbins-j-1)/(nbins-1), float(nbins-j-1)/(nbins-1)]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'ry':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from red through white
rgb = [1.0, float(j)/(nbins-1), 0]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'yr':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from white through red
rgb = [1.0, float(nbins-j-1)/(nbins-1), 0]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'gw':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from green through white
rgb = [float(j)/(nbins-1), 1.0, float(j)/(nbins-1)]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'wg':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from white through green
rgb = [float(nbins-j-1)/(nbins-1), 1.0, float(nbins-j-1)/(nbins-1)]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'bw':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from blue through white
rgb = [float(j)/(nbins-1), float(j)/(nbins-1), 1.0 ]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'wb':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from blue through white
rgb = [float(nbins-j-1)/(nbins-1), float(nbins-j-1)/(nbins-1), 1.0 ]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'wr':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from white through blue
rgb = [float(nbins-j-1)/(nbins-1), float(nbins-j-1)/(nbins-1), 1.0]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'gy':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from green through yellow
rgb = [float(j)/(nbins-1), 1.0, 0.]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'yg':
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from green through yellow
rgb = [float(nbins-j-1)/(nbins-1), 1.0, 0.]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'bwr':
col=[]
coldesc=[]
for j in range(nbins//2):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from blue to white
rgb = [min(1.0, float(j)*2/(nbins-1)), min(1.0,float(j)*2/(nbins-1)), min(1.0, float(nbins-j-1)*2/(nbins-1))]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
for j in range(nbins//2,nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from white to red
rgb = [min(1.0, float(j)*2/(nbins-1)), min(1.0,float(nbins-j-1)*2/(nbins-1)), min(1.0, float(nbins-j-1)*2/(nbins-1))]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'rwb':
col=[]
coldesc=[]
for j in range(nbins//2):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient from red to white
rgb = [min(1.0, float(nbins-j-1)*2/(nbins-1)), min(1.0,float(j)*2/(nbins-1)), min(1.0, float(j)*2/(nbins-1))]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
for j in range(nbins//2,nbins):
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# coldesc.append('col' + str(sel[j]) + str(j)))
# create colors in a gradient from white to blue
rgb = [min(1.0, float(nbins-j-1)*2/(nbins-1)), min(1.0,float(nbins-j-1)*2/(nbins-1)), min(1.0, float(j)*2/(nbins-1))]
# convert rgb to hsv, modify saturation and value and convert back to rgb
hsv = list(colorsys.rgb_to_hsv(rgb[0],rgb[1],rgb[2]))
hsv[1] = hsv[1]*sat
hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'gray' or gradient == 'grey':
# if it is "gray" then sat must be 0!
sat = 0.0
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient of grays from "sat" to "value"
hsv = [0, 0, sat + (value-sat)*float(j)/(nbins-1)]
# hsv[1] = hsv[1]*sat
# hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
elif gradient == 'reversegray' or gradient == 'reversegrey':
# if it is "gray" then sat must be 0!
sat = 0.0
col=[]
coldesc=[]
for j in range(nbins):
# coldesc.append('col' + str(sel[j]) + str(j))
coldesc.append('col' + gradient + str(j) + str(sel[j]))
# create colors in a gradient of grays from "sat" to "value"
hsv = [0, 0, value - (value-sat)*float(j)/(nbins-1)]
# hsv[1] = hsv[1]*sat
# hsv[2] = hsv[2]*value
rgb = colorsys.hsv_to_rgb(hsv[0],hsv[1],hsv[2])
col.append(rgb)
# cmd.set_color("col" + str(sel[j]) + str(j),col[j])
cmd.set_color("col" + gradient + str(j) + str(sel[j]),col[j])
#print coldesc
# return the gradient as a list of colors named by their gradient & index (i.e. colbw0,colbw1,colbw2,...)
return coldesc
# allow calling without parentheses: color_hist_b [selection=], [mode= ],[gradient= ],[nbins= ]
cmd.extend("color_b",color_b)
cmd.extend("color_q",color_q)