Skip to content

Commit 88a0239

Browse files
committed
added a new 'colorscale_to_colors' method and finished writing all tests
1 parent f4763ab commit 88a0239

File tree

2 files changed

+101
-16
lines changed

2 files changed

+101
-16
lines changed

plotly/colors.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ def validate_colors(colors):
230230
"Whoops! The elements in your colors tuples "
231231
"cannot exceed 1.0."
232232
)
233+
return colors
233234

234235

235236
def convert_colors_to_same_type(colors, colortype='rgb'):
@@ -343,7 +344,7 @@ def convert_dict_colors_to_same_type(colors, colortype='rgb'):
343344

344345
def make_colorscale(colors, scale=None):
345346
"""
346-
Makes a colorscale from a list of colors and scale
347+
Makes a colorscale from a list of colors and a scale
347348
348349
Takes a list of colors and scales and constructs a colorscale based
349350
on the colors in sequential order. If 'scale' is left empty, a linear-
@@ -354,16 +355,16 @@ def make_colorscale(colors, scale=None):
354355
"""
355356
colorscale = []
356357

358+
# validate minimum colors length of 2
359+
if len(colors) < 2:
360+
raise exceptions.PlotlyError("You must input a list of colors that "
361+
"has at least two colors.")
362+
357363
if not scale:
358364
scale_incr = 1./(len(colors) - 1)
359365
return [[i * scale_incr, color] for i, color in enumerate(colors)]
360366

361367
else:
362-
# validate minimum colors length of 2
363-
if len(colors) < 2:
364-
raise exceptions.PlotlyError("You must input a list of colors "
365-
"that has at least two colors.")
366-
367368
# validate scale
368369
if len(colors) != len(scale):
369370
raise exceptions.PlotlyError("The length of colors and scale "
@@ -504,3 +505,13 @@ def hex_to_rgb(value):
504505
rgb_section_length = hex_total_length // 3
505506
return tuple(int(value[i:i + rgb_section_length], 16)
506507
for i in range(0, hex_total_length, rgb_section_length))
508+
509+
510+
def colorscale_to_colors(colorscale):
511+
"""
512+
Converts a colorscale into a list of colors
513+
"""
514+
color_list = []
515+
for color in colorscale:
516+
color_list.append(color[1])
517+
return color_list
Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,115 @@
1-
import math
21
from unittest import TestCase
32

43
from nose.tools import raises
54
import plotly.tools as tls
65
from plotly.exceptions import PlotlyError
76
import plotly.colors as colors
87

9-
"""
8+
109
class TestColors(TestCase):
1110

1211
def test_validate_colors(self):
1312

1413
# test string input
15-
color = 'foo'
14+
color_string = 'foo'
1615

1716
pattern = ("If your colors variable is a string, it must be a "
1817
"Plotly scale, an rgb color or a hex color.")
1918

2019
self.assertRaisesRegexp(PlotlyError, pattern, colors.validate_colors,
21-
color)
20+
color_string)
2221

2322
# test rgb color
24-
color2 = 'rgb(265,0,0)'
23+
color_string2 = 'rgb(265, 0, 0)'
2524

2625
pattern2 = ("Whoops! The elements in your rgb colors tuples cannot "
2726
"exceed 255.0.")
2827

2928
self.assertRaisesRegexp(PlotlyError, pattern2, colors.validate_colors,
30-
color2)
29+
color_string2)
3130

3231
# test tuple color
33-
34-
color3 = (1.1, 1, 1)
32+
color_tuple = (1, 1, 2)
3533

3634
pattern3 = ("Whoops! The elements in your colors tuples cannot "
3735
"exceed 1.0.")
3836

3937
self.assertRaisesRegexp(PlotlyError, pattern3, colors.validate_colors,
40-
color3)
41-
"""
38+
color_tuple)
39+
40+
def test_convert_colors_to_same_type(self):
41+
42+
# test string input
43+
color_string = 'foo'
44+
45+
pattern = ("If your colors variable is a string, it must be a "
46+
"Plotly scale, an rgb color or a hex color.")
47+
48+
self.assertRaisesRegexp(PlotlyError, pattern,
49+
colors.convert_colors_to_same_type,
50+
color_string)
51+
52+
# test colortype
53+
color_tuple = (1, 1, 1)
54+
colortype = 2
55+
56+
pattern2 = ("You must select either rgb or tuple for your colortype "
57+
"variable.")
58+
59+
self.assertRaisesRegexp(PlotlyError, pattern2,
60+
colors.convert_colors_to_same_type,
61+
color_tuple, colortype)
62+
63+
def test_convert_dict_colors_to_same_type(self):
64+
65+
# test colortype
66+
color_dict = dict(apple='rgb(1, 1, 1)')
67+
colortype = 2
68+
69+
pattern = ("You must select either rgb or tuple for your colortype "
70+
"variable.")
71+
72+
self.assertRaisesRegexp(PlotlyError, pattern,
73+
colors.convert_dict_colors_to_same_type,
74+
color_dict, colortype)
75+
76+
def test_make_colorscale(self):
77+
78+
# test minimum colors length
79+
color_list = [(0, 0, 0)]
80+
81+
pattern = (
82+
"You must input a list of colors that has at least two colors."
83+
)
84+
85+
self.assertRaisesRegexp(PlotlyError, pattern, colors.make_colorscale,
86+
color_list)
87+
88+
# test length of colors and scale
89+
color_list2 = [(0, 0, 0), (1, 1, 1)]
90+
scale = [0]
91+
92+
pattern2 = ("The length of colors and scale must be the same.")
93+
94+
self.assertRaisesRegexp(PlotlyError, pattern2, colors.make_colorscale,
95+
color_list2, scale)
96+
97+
# test first and last number of scale
98+
scale2 = [0, 2]
99+
100+
pattern3 = ("The first and last number in scale must be 0.0 and 1.0 "
101+
"respectively.")
102+
103+
self.assertRaisesRegexp(PlotlyError, pattern3, colors.make_colorscale,
104+
color_list2, scale2)
105+
106+
# test for strictly increasing scale
107+
color_list3 = [(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)]
108+
scale3 = [0, 1, 1]
109+
110+
pattern4 = ("'scale' must be a list that contains an increasing "
111+
"sequence of numbers where the first and last number are"
112+
"0.0 and 1.0 respectively.")
113+
114+
self.assertRaisesRegexp(PlotlyError, pattern4, colors.make_colorscale,
115+
color_list3, scale3)

0 commit comments

Comments
 (0)