@@ -159,7 +159,6 @@ def color_parser(colors, function):
159
159
the color or iterable of colors. If given an iterable, it will only be
160
160
able to work with it if all items in the iterable are of the same type
161
161
- rgb string, hex string or tuple
162
-
163
162
"""
164
163
if isinstance (colors , str ):
165
164
return function (colors )
@@ -180,6 +179,10 @@ def color_parser(colors, function):
180
179
def validate_colors (colors , colors_list = None ):
181
180
"""
182
181
Validates color(s) and returns an error for invalid color(s)
182
+
183
+ :param (list) colors_list: whether a singleton color or a list/tuple of
184
+ colors is inputted, all the color types are appended to colors_list
185
+ so they can be easily iterated through for validation
183
186
"""
184
187
if colors_list is None :
185
188
colors_list = []
@@ -191,8 +194,8 @@ def validate_colors(colors, colors_list=None):
191
194
colors_list .append (colors )
192
195
else :
193
196
raise exceptions .PlotlyError (
194
- " If your colors variable is a string, it must be a "
195
- " Plotly scale, an rgb color or a hex color."
197
+ ' If your colors variable is a string, it must be a '
198
+ ' Plotly scale, an rgb color or a hex color.'
196
199
)
197
200
198
201
elif isinstance (colors , tuple ):
@@ -216,8 +219,8 @@ def validate_colors(colors, colors_list=None):
216
219
for value in each_color :
217
220
if value > 255.0 :
218
221
raise exceptions .PlotlyError (
219
- " Whoops! The elements in your rgb colors "
220
- " tuples cannot exceed 255.0."
222
+ ' Whoops! The elements in your rgb colors '
223
+ ' tuples cannot exceed 255.0.'
221
224
)
222
225
elif '#' in each_color :
223
226
each_color = color_parser (
@@ -227,10 +230,9 @@ def validate_colors(colors, colors_list=None):
227
230
for value in each_color :
228
231
if value > 1.0 :
229
232
raise exceptions .PlotlyError (
230
- " Whoops! The elements in your colors tuples "
231
- " cannot exceed 1.0."
233
+ ' Whoops! The elements in your colors tuples '
234
+ ' cannot exceed 1.0.'
232
235
)
233
- return
234
236
235
237
236
238
def convert_colors_to_same_type (colors , colortype = 'rgb' , scale = None ,
@@ -247,6 +249,12 @@ def convert_colors_to_same_type(colors, colortype='rgb', scale=None,
247
249
from the respective colorscale and the colors in that colorscale will also
248
250
be coverted to the selected colortype. If colors is None, then there is an
249
251
option to return portion of the DEFAULT_PLOTLY_COLORS
252
+
253
+ :param (list) colors_list: see docs for validate_colors()
254
+ :param (list) scale: see docs for validate_scale_values()
255
+
256
+ :rtype (tuple) (colors_list, scale) if scale is None in the function call,
257
+ then scale will remain None in the returned tuple
250
258
"""
251
259
if colors_list is None :
252
260
colors_list = []
@@ -278,8 +286,8 @@ def convert_colors_to_same_type(colors, colortype='rgb', scale=None,
278
286
279
287
if len (colors_list ) != len (scale ):
280
288
raise exceptions .PlotlyError (
281
- " Make sure that the length of your scale matches the length "
282
- " of your list of colors which is {}." .format (len (colors_list ))
289
+ ' Make sure that the length of your scale matches the length '
290
+ ' of your list of colors which is {}.' .format (len (colors_list ))
283
291
)
284
292
285
293
# convert all colors to rgb
@@ -315,8 +323,8 @@ def convert_colors_to_same_type(colors, colortype='rgb', scale=None,
315
323
colors_list [j ] = each_color
316
324
return (colors_list , scale )
317
325
else :
318
- raise exceptions .PlotlyError (" You must select either rgb or tuple "
319
- " for your colortype variable." )
326
+ raise exceptions .PlotlyError (' You must select either rgb or tuple '
327
+ ' for your colortype variable.' )
320
328
321
329
322
330
def convert_dict_colors_to_same_type (colors , colortype = 'rgb' ):
@@ -356,31 +364,36 @@ def convert_dict_colors_to_same_type(colors, colortype='rgb'):
356
364
)
357
365
return colors
358
366
else :
359
- raise exceptions .PlotlyError (" You must select either rgb or tuple "
360
- " for your colortype variable." )
367
+ raise exceptions .PlotlyError (' You must select either rgb or tuple '
368
+ ' for your colortype variable.' )
361
369
362
370
363
371
def validate_scale_values (scale ):
364
372
"""
365
373
Validates scale values from a colorscale
374
+
375
+ :param (list) scale: a strictly increasing list of floats that begins
376
+ with 0 and ends with 1. Its usage derives from a colorscale which is
377
+ a list of two-lists (a list with two elements) of the form
378
+ [value, color] which are used to determine how interpolation weighting
379
+ works between the colors in the colorscale. Therefore scale is just
380
+ the extraction of these values from the two-lists in order
366
381
"""
367
382
if len (scale ) < 2 :
368
- raise exceptions .PlotlyError (" You must input a list of scale values "
369
- " that has at least two values." )
383
+ raise exceptions .PlotlyError (' You must input a list of scale values '
384
+ ' that has at least two values.' )
370
385
371
386
if (scale [0 ] != 0 ) or (scale [- 1 ] != 1 ):
372
387
raise exceptions .PlotlyError (
373
- " The first and last number in your scale must be 0.0 and 1.0 "
374
- " respectively."
388
+ ' The first and last number in your scale must be 0.0 and 1.0 '
389
+ ' respectively.'
375
390
)
376
391
377
- for j in range (1 , len (scale )):
378
- if scale [j ] <= scale [j - 1 ]:
392
+ if not all (x < y for x , y in zip (scale , scale [1 :])):
379
393
raise exceptions .PlotlyError (
380
- "'scale' must be a list that contains an increasing "
394
+ "'scale' must be a list that contains a strictly increasing "
381
395
"sequence of numbers."
382
396
)
383
- return
384
397
385
398
386
399
def make_colorscale (colors , scale = None , colorscale = None ):
@@ -399,17 +412,17 @@ def make_colorscale(colors, scale=None, colorscale=None):
399
412
400
413
# validate minimum colors length of 2
401
414
if len (colors ) < 2 :
402
- raise exceptions .PlotlyError (" You must input a list of colors that "
403
- " has at least two colors." )
415
+ raise exceptions .PlotlyError (' You must input a list of colors that '
416
+ ' has at least two colors.' )
404
417
405
418
if scale is None :
406
419
scale_incr = 1. / (len (colors ) - 1 )
407
420
return [[i * scale_incr , color ] for i , color in enumerate (colors )]
408
421
409
422
else :
410
423
if len (colors ) != len (scale ):
411
- raise exceptions .PlotlyError (" The length of colors and scale "
412
- " must be the same." )
424
+ raise exceptions .PlotlyError (' The length of colors and scale '
425
+ ' must be the same.' )
413
426
414
427
validate_scale_values (scale )
415
428
@@ -424,7 +437,6 @@ def find_intermediate_color(lowcolor, highcolor, intermed):
424
437
This function takes two color tuples, where each element is between 0
425
438
and 1, along with a value 0 < intermed < 1 and returns a color that is
426
439
intermed-percent from lowcolor to highcolor
427
-
428
440
"""
429
441
diff_0 = float (highcolor [0 ] - lowcolor [0 ])
430
442
diff_1 = float (highcolor [1 ] - lowcolor [1 ])
@@ -442,7 +454,6 @@ def unconvert_from_RGB_255(colors):
442
454
Takes a (list of) color tuple(s) where each element is between 0 and
443
455
255. Returns the same tuples where each tuple element is normalized to
444
456
a value between 0 and 1
445
-
446
457
"""
447
458
return (colors [0 ]/ (255.0 ),
448
459
colors [1 ]/ (255.0 ),
@@ -458,6 +469,9 @@ def convert_to_RGB_255(colors, rgb_components=None):
458
469
if x is odd, the number rounds up to (x+1). Otherwise, it rounds down
459
470
to just x. This is the way rounding works in Python 3 and in current
460
471
statistical analysis to avoid rounding bias
472
+
473
+ :param (list) rgb_components: grabs the three R, G and B values to be
474
+ returned as computed in the function
461
475
"""
462
476
if rgb_components is None :
463
477
rgb_components = []
@@ -480,7 +494,6 @@ def n_colors(lowcolor, highcolor, n_colors):
480
494
Accepts two color tuples and returns a list of n_colors colors
481
495
which form the intermediate colors between lowcolor and highcolor
482
496
from linearly interpolating through RGB space
483
-
484
497
"""
485
498
diff_0 = float (highcolor [0 ] - lowcolor [0 ])
486
499
incr_0 = diff_0 / (n_colors - 1 )
@@ -512,7 +525,6 @@ def unlabel_rgb(colors):
512
525
513
526
This function takes either an 'rgb(a, b, c)' color or a list of
514
527
such colors and returns the color tuples in tuple(s) (a, b, c)
515
-
516
528
"""
517
529
str_vals = ''
518
530
for index in range (len (colors )):
0 commit comments