@@ -442,21 +442,37 @@ def make_colorscale(colors, scale=None):
442
442
return colorscale
443
443
444
444
445
- def find_intermediate_color (lowcolor , highcolor , intermed ):
445
+ def find_intermediate_color (lowcolor , highcolor , intermed , colortype = 'tuple' ):
446
446
"""
447
447
Returns the color at a given distance between two colors
448
448
449
449
This function takes two color tuples, where each element is between 0
450
450
and 1, along with a value 0 < intermed < 1 and returns a color that is
451
- intermed-percent from lowcolor to highcolor
451
+ intermed-percent from lowcolor to highcolor. If colortype is set to 'rgb',
452
+ the function will automatically convert the rgb type to a tuple, find the
453
+ intermediate color and return it as an rgb color.
452
454
"""
455
+ if colortype == 'rgb' :
456
+ # convert to tuple color, eg. (1, 0.45, 0.7)
457
+ lowcolor = unlabel_rgb (lowcolor )
458
+ highcolor = unlabel_rgb (highcolor )
459
+
453
460
diff_0 = float (highcolor [0 ] - lowcolor [0 ])
454
461
diff_1 = float (highcolor [1 ] - lowcolor [1 ])
455
462
diff_2 = float (highcolor [2 ] - lowcolor [2 ])
456
463
457
- return (lowcolor [0 ] + intermed * diff_0 ,
458
- lowcolor [1 ] + intermed * diff_1 ,
459
- lowcolor [2 ] + intermed * diff_2 )
464
+ inter_med_tuple = (
465
+ lowcolor [0 ] + intermed * diff_0 ,
466
+ lowcolor [1 ] + intermed * diff_1 ,
467
+ lowcolor [2 ] + intermed * diff_2
468
+ )
469
+
470
+ if colortype == 'rgb' :
471
+ # back to an rgb string, e.g. rgb(30, 20, 10)
472
+ inter_med_rgb = label_rgb (inter_med_tuple )
473
+ return inter_med_rgb
474
+
475
+ return inter_med_tuple
460
476
461
477
462
478
def unconvert_from_RGB_255 (colors ):
@@ -498,29 +514,39 @@ def convert_to_RGB_255(colors):
498
514
return (rgb_components [0 ], rgb_components [1 ], rgb_components [2 ])
499
515
500
516
501
- def n_colors (lowcolor , highcolor , n_colors ):
517
+ def n_colors (lowcolor , highcolor , n_colors , colortype = 'tuple' ):
502
518
"""
503
519
Splits a low and high color into a list of n_colors colors in it
504
520
505
521
Accepts two color tuples and returns a list of n_colors colors
506
522
which form the intermediate colors between lowcolor and highcolor
507
- from linearly interpolating through RGB space
523
+ from linearly interpolating through RGB space. If colortype is 'rgb'
524
+ the function will return a list of colors in the same form.
508
525
"""
526
+ if colortype == 'rgb' :
527
+ # convert to tuple
528
+ lowcolor = unlabel_rgb (lowcolor )
529
+ highcolor = unlabel_rgb (highcolor )
530
+
509
531
diff_0 = float (highcolor [0 ] - lowcolor [0 ])
510
532
incr_0 = diff_0 / (n_colors - 1 )
511
533
diff_1 = float (highcolor [1 ] - lowcolor [1 ])
512
534
incr_1 = diff_1 / (n_colors - 1 )
513
535
diff_2 = float (highcolor [2 ] - lowcolor [2 ])
514
536
incr_2 = diff_2 / (n_colors - 1 )
515
- color_tuples = []
537
+ list_of_colors = []
516
538
517
539
for index in range (n_colors ):
518
540
new_tuple = (lowcolor [0 ] + (index * incr_0 ),
519
541
lowcolor [1 ] + (index * incr_1 ),
520
542
lowcolor [2 ] + (index * incr_2 ))
521
- color_tuples .append (new_tuple )
543
+ list_of_colors .append (new_tuple )
544
+
545
+ if colortype == 'rgb' :
546
+ # back to an rgb string
547
+ list_of_colors = color_parser (list_of_colors , label_rgb )
522
548
523
- return color_tuples
549
+ return list_of_colors
524
550
525
551
526
552
def label_rgb (colors ):
0 commit comments