Skip to content

force rgb colors to be a triplet of integers, not floats #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions plotly/tests/test_core/test_tools/test_figure_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,32 +1402,32 @@ def test_2D_density_all_args(self):
point_size=3, height=800, width=800)

exp_2D_density_chart = {
'data': [{'marker': {'color': 'rgb(0.0, 0.0, 127.5)',
'data': [{'marker': {'color': 'rgb(0, 0, 128)',
'opacity': 0.4,
'size': 3},
'mode': 'markers',
'name': 'points',
'type': 'scatter',
'x': [1, 2],
'y': [2, 4]},
{'colorscale': [[0.0, 'rgb(122.0, 69.0, 121.0)'],
[0.25, 'rgb(213.0, 96.0, 115.0)'],
[0.5, 'rgb(236.0, 158.0, 105.0)'],
[0.75, 'rgb(255.0, 255.0, 51.0)'],
[1.0, 'rgb(249.9, 249.9, 249.9)']],
{'colorscale': [[0.0, 'rgb(122, 69, 121)'],
[0.25, 'rgb(213, 96, 115)'],
[0.5, 'rgb(236, 158, 105)'],
[0.75, 'rgb(255, 255, 51)'],
[1.0, 'rgb(250, 250, 250)']],
'name': 'density',
'ncontours': 20,
'reversescale': True,
'showscale': False,
'type': 'histogram2dcontour',
'x': [1, 2],
'y': [2, 4]},
{'marker': {'color': 'rgb(255.0, 237.0, 222.0)'},
{'marker': {'color': 'rgb(255, 237, 222)'},
'name': 'x density',
'type': 'histogram',
'x': [1, 2],
'yaxis': 'y2'},
{'marker': {'color': 'rgb(255.0, 237.0, 222.0)'},
{'marker': {'color': 'rgb(255, 237, 222)'},
'name': 'y density',
'type': 'histogram',
'xaxis': 'x2',
Expand Down
95 changes: 55 additions & 40 deletions plotly/tests/test_optional/temp-plot.html

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions plotly/tests/test_optional/test_figure_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,14 +843,14 @@ def test_trisurf_all_args(self):

exp_trisurf_plot = {
'data': [{'facecolor': np.array(
['rgb(144.0, 94.5, 132.0)',
'rgb(23.0, 190.0, 207.0)',
'rgb(144.0, 94.5, 132.0)',
'rgb(31.0, 119.0, 180.0)',
'rgb(144.0, 94.5, 132.0)',
'rgb(31.0, 119.0, 180.0)',
'rgb(144.0, 94.5, 132.0)',
'rgb(23.0, 190.0, 207.0)']
['rgb(144, 94, 132)',
'rgb(23, 190, 207)',
'rgb(144, 94, 132)',
'rgb(31, 119, 180)',
'rgb(144, 94, 132)',
'rgb(31, 119, 180)',
'rgb(144, 94, 132)',
'rgb(23, 190, 207)']
),
'i': np.array([3, 1, 1, 5, 7, 3, 5, 7]),
'j': np.array([1, 3, 5, 1, 3, 7, 7, 5]),
Expand Down Expand Up @@ -884,16 +884,16 @@ def test_trisurf_all_args(self):
-0.0, 0.0, 0.0, None, -0.0, 0.0, -1.0,
-0.0, None, 0.0, 0.0, 0.0, 0.0, None,
0.0, 0.0, 1.0, 0.0, None])},
{'colorscale': [[0.0, 'rgb(31.0, 119.0, 180.0)'],
[0.1111111111111111, 'rgb(255.0, 127.0, 14.0)'],
[0.2222222222222222, 'rgb(44.0, 160.0, 44.0)'],
[0.3333333333333333, 'rgb(214.0, 39.0, 40.0)'],
[0.4444444444444444, 'rgb(148.0, 103.0, 189.0)'],
[0.5555555555555556, 'rgb(140.0, 86.0, 75.0)'],
[0.6666666666666666, 'rgb(227.0, 119.0, 194.0)'],
[0.7777777777777778, 'rgb(127.0, 127.0, 127.0)'],
[0.8888888888888888, 'rgb(188.0, 189.0, 34.0)'],
[1.0, 'rgb(23.0, 190.0, 207.0)']],
{'colorscale': [[0.0, 'rgb(31, 119, 180)'],
[0.1111111111111111, 'rgb(255, 127, 14)'],
[0.2222222222222222, 'rgb(44, 160, 44)'],
[0.3333333333333333, 'rgb(214, 39, 40)'],
[0.4444444444444444, 'rgb(148, 103, 189)'],
[0.5555555555555556, 'rgb(140, 86, 75)'],
[0.6666666666666666, 'rgb(227, 119, 194)'],
[0.7777777777777778, 'rgb(127, 127, 127)'],
[0.8888888888888888, 'rgb(188, 189, 34)'],
[1.0, 'rgb(23, 190, 207)']],
'intensity': [0, 1],
'showscale': True,
'type': 'mesh3d',
Expand Down Expand Up @@ -1261,13 +1261,13 @@ def test_scatter_plot_matrix_kwargs(self):
)

exp_scatter_plot_matrix = {
'data': [{'marker': {'color': 'rgb(128.0, 0.0, 38.0)'},
'data': [{'marker': {'color': 'rgb(128, 0, 38)'},
'showlegend': False,
'type': 'histogram',
'x': [2, -15, -2, 0],
'xaxis': 'x1',
'yaxis': 'y1'},
{'marker': {'color': 'rgb(255.0, 255.0, 204.0)'},
{'marker': {'color': 'rgb(255, 255, 204)'},
'showlegend': False,
'type': 'histogram',
'x': [6, 5],
Expand Down Expand Up @@ -1324,7 +1324,7 @@ def test_df_dataframe_all_args(self):
'shapes': [{'opacity': 1,
'y1': 0.2,
'xref': 'x',
'fillcolor': 'rgb(31.0, 119.0, 180.0)',
'fillcolor': 'rgb(31, 119, 180)',
'yref': 'y',
'y0': -0.2,
'x0': '2009-01-01',
Expand All @@ -1334,7 +1334,7 @@ def test_df_dataframe_all_args(self):
{'opacity': 1,
'y1': 1.2,
'xref': 'x',
'fillcolor': 'rgb(255.0, 127.0, 14.0)',
'fillcolor': 'rgb(255, 127, 14)',
'yref': 'y',
'y0': 0.8,
'x0': '2009-03-05',
Expand Down Expand Up @@ -1543,7 +1543,7 @@ def test_violin_fig(self):

exp_violin = {
'data': [{'fill': 'tonextx',
'fillcolor': 'rgb(31.0, 119.0, 180.0)',
'fillcolor': 'rgb(31, 119, 180)',
'hoverinfo': 'text',
'line': {'color': 'rgb(0, 0, 0)',
'shape': 'spline',
Expand Down Expand Up @@ -1721,7 +1721,7 @@ def test_violin_fig(self):
1.96969697, 1.97979798, 1.98989899,
2.])},
{'fill': 'tonextx',
'fillcolor': 'rgb(31.0, 119.0, 180.0)',
'fillcolor': 'rgb(31, 119, 180)',
'hoverinfo': 'text',
'line': {'color': 'rgb(0, 0, 0)',
'shape': 'spline',
Expand Down Expand Up @@ -1921,7 +1921,7 @@ def test_violin_fig(self):
'x': [0],
'y': [1.5]},
{'hoverinfo': 'y',
'marker': {'color': 'rgb(31.0, 119.0, 180.0)',
'marker': {'color': 'rgb(31, 119, 180)',
'symbol': 'line-ew-open'},
'mode': 'markers',
'name': '',
Expand Down
40 changes: 27 additions & 13 deletions plotly/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import six
import math
import decimal

from plotly import utils
from plotly import exceptions
Expand Down Expand Up @@ -3177,10 +3178,9 @@ def _find_intermediate_color(lowcolor, highcolor, intermed):
diff_1 = float(highcolor[1] - lowcolor[1])
diff_2 = float(highcolor[2] - lowcolor[2])

inter_colors = (lowcolor[0] + intermed * diff_0,
lowcolor[1] + intermed * diff_1,
lowcolor[2] + intermed * diff_2)
return inter_colors
return (lowcolor[0] + intermed * diff_0,
lowcolor[1] + intermed * diff_1,
lowcolor[2] + intermed * diff_2)

@staticmethod
def _color_parser(colors, function):
Expand Down Expand Up @@ -3220,11 +3220,9 @@ def _unconvert_from_RGB_255(colors):
a value between 0 and 1

"""
un_rgb_color = (colors[0]/(255.0),
colors[1]/(255.0),
colors[2]/(255.0))

return un_rgb_color
return (colors[0]/(255.0),
colors[1]/(255.0),
colors[2]/(255.0))

@staticmethod
def _map_face2color(face, colormap, vmin, vmax):
Expand Down Expand Up @@ -3263,10 +3261,11 @@ def _map_face2color(face, colormap, vmin, vmax):
face_color = FigureFactory._find_intermediate_color(
colormap[low_color_index],
colormap[low_color_index + 1],
t * (len(colormap) - 1) - low_color_index)
t * (len(colormap) - 1) - low_color_index
)

face_color = FigureFactory._convert_to_RGB_255(face_color)
face_color = FigureFactory._label_rgb(face_color)

return face_color

@staticmethod
Expand Down Expand Up @@ -3335,7 +3334,7 @@ def _trisurf(x, y, z, simplices, show_colorbar, colormap=None,
facecolor.append(color)

# Make sure we have arrays to speed up plotting
facecolor = np.asarray(facecolor)
facecolor = list(facecolor)
ii, jj, kk = simplices.T

# make a colorscale from the colors
Expand Down Expand Up @@ -4563,9 +4562,24 @@ def _endpts_to_intervals(endpts):
def _convert_to_RGB_255(colors):
"""
Multiplies each element of a triplet by 255

Each coordinate of the color tuple is rounded to the nearest float and
then is turned into an integer. If a number is of the form x.5, then
if x is odd, the number rounds up to (x+1). Otherwise, it rounds down
to just x. This is the way rounding works in Python 3 and in current
statistical analysis to avoid rounding bias
"""
rgb_components = []

for component in colors:
rounded_num = decimal.Decimal(str(component*255.0)).quantize(
decimal.Decimal('1'), rounding=decimal.ROUND_HALF_EVEN
)
# convert rounded number to an integer from 'Decimal' form
rounded_num = int(rounded_num)
rgb_components.append(rounded_num)

return (colors[0]*255.0, colors[1]*255.0, colors[2]*255.0)
return (rgb_components[0], rgb_components[1], rgb_components[2])

@staticmethod
def _n_colors(lowcolor, highcolor, n_colors):
Expand Down
2 changes: 1 addition & 1 deletion plotly/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.12.7'
__version__ = '1.12.8'