@@ -442,21 +442,37 @@ def make_colorscale(colors, scale=None):
442442 return colorscale
443443
444444
445- def find_intermediate_color (lowcolor , highcolor , intermed ):
445+ def find_intermediate_color (lowcolor , highcolor , intermed , colortype = 'tuple' ):
446446 """
447447 Returns the color at a given distance between two colors
448448
449449 This function takes two color tuples, where each element is between 0
450450 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.
452454 """
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+
453460 diff_0 = float (highcolor [0 ] - lowcolor [0 ])
454461 diff_1 = float (highcolor [1 ] - lowcolor [1 ])
455462 diff_2 = float (highcolor [2 ] - lowcolor [2 ])
456463
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
460476
461477
462478def unconvert_from_RGB_255 (colors ):
@@ -498,29 +514,39 @@ def convert_to_RGB_255(colors):
498514 return (rgb_components [0 ], rgb_components [1 ], rgb_components [2 ])
499515
500516
501- def n_colors (lowcolor , highcolor , n_colors ):
517+ def n_colors (lowcolor , highcolor , n_colors , colortype = 'tuple' ):
502518 """
503519 Splits a low and high color into a list of n_colors colors in it
504520
505521 Accepts two color tuples and returns a list of n_colors colors
506522 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.
508525 """
526+ if colortype == 'rgb' :
527+ # convert to tuple
528+ lowcolor = unlabel_rgb (lowcolor )
529+ highcolor = unlabel_rgb (highcolor )
530+
509531 diff_0 = float (highcolor [0 ] - lowcolor [0 ])
510532 incr_0 = diff_0 / (n_colors - 1 )
511533 diff_1 = float (highcolor [1 ] - lowcolor [1 ])
512534 incr_1 = diff_1 / (n_colors - 1 )
513535 diff_2 = float (highcolor [2 ] - lowcolor [2 ])
514536 incr_2 = diff_2 / (n_colors - 1 )
515- color_tuples = []
537+ list_of_colors = []
516538
517539 for index in range (n_colors ):
518540 new_tuple = (lowcolor [0 ] + (index * incr_0 ),
519541 lowcolor [1 ] + (index * incr_1 ),
520542 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 )
522548
523- return color_tuples
549+ return list_of_colors
524550
525551
526552def label_rgb (colors ):
0 commit comments