Skip to content
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

Allow bgcolor to be set to any color, not just string #177

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion chaco/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from base import NumericalSequenceTrait, PointTrait, ImageTrait, DimensionTrait, \
SortOrderTrait, bin_search, reverse_map_1d, right_shift, \
left_shift, sort_points, find_runs, arg_find_runs, \
point_line_distance
point_line_distance, colors_equal

# Data model
from abstract_data_source import AbstractDataSource
Expand Down
19 changes: 19 additions & 0 deletions chaco/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,24 @@ def point_line_distance(pt, p1, p2):

return sqrt(dot(diff,diff))

def standardize_color(color):
from enable.api import color_table
if isinstance(color, basestring):
color = color_table[color]
color = list(color)
if len(color) == 3:
color.append(1.0)
return tuple(color)

def colors_equal(color1, color2, match_alpha=False):
""" Determine whether two colors are equal. If match_alpha==True,
they must have the same alpha value as well. """
color1 = standardize_color(color1)
color2 = standardize_color(color2)
if not match_alpha:
color1 = color1[:3]
color2 = color2[:3]
return color1 == color2


#EOF
4 changes: 2 additions & 2 deletions chaco/data_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from numpy import array, transpose

from traits.api import Bool, Enum, Instance, Property
from enable.colors import color_table

from chaco.api import colors_equal
from abstract_overlay import AbstractOverlay
from axis import PlotAxis
from base_1d_mapper import Base1DMapper
Expand Down Expand Up @@ -270,7 +270,7 @@ def _init_components(self):
# make sure the grid and bgcolor are not the same color

grid_color = 'lightgray'
if color_table[self.bgcolor] == color_table[grid_color]:
if colors_equal(self.bgcolor, grid_color):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bgcolor is a ColorTrait (a mapped trait type). The RGBA value of all ColorTraits is available as self.<trait>_ . if we change this line if self.bgcolor_ == color_table[grid_color] it would be enough to fix this bug. In general comparing between mapped traits should always happen through the shadow <trait>_ attribute.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to rework the code as suggested above. The colors_equal can stay since it might be useful somewhere else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@itziakos That's not quite sufficient, because the user could set the bgcolor to a 3-tuple with no alpha. Even if there is an alpha value, we probably don't want to compare it in this case. Using if self.bgcolor_[:3] == color_table[grid_color][:3]: works, but IMO is not as clean as using the colors_equal function.

We probably want something like colors_almost_equal as well, because it could be set to a color indistinguishable by eye. I think that's overkill in this case, though. In fact, I am tempted to take out this check altogether and let the user be responsible for grid color. A simple check here does no harm, though, I suppose.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am tempted to take out this check altogether and let the user be responsible for grid color.

I agree, I think that it would be better if grid_color is an attribute in DataView with some basic default value given the bgcolor.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for making the grid_color an attribute controlled by the user. I don't see a strong necessity for the check then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I won't have time to do this for a couple weeks, so if anyone feels like taking it on, feel free.

grid_color = 'white'

if not self.x_grid and self.auto_grid:
Expand Down
49 changes: 46 additions & 3 deletions chaco/tests/base_utils_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from numpy import arange, array
from numpy.testing import assert_equal, assert_almost_equal

from chaco.api import bin_search, find_runs, reverse_map_1d, point_line_distance
from chaco.api import bin_search, find_runs, reverse_map_1d, point_line_distance, colors_equal
from enable.api import ColorTrait
from traits.api import HasTraits


class BinSearchTestCase(unittest.TestCase):
def test_ascending_data(self):
Expand Down Expand Up @@ -165,6 +168,46 @@ def test_point_on_line(self):
assert_almost_equal(dist, 0.0)


class SimpleColorTraits(HasTraits):
""" A class to hold a ColorTrait for testing """
color = ColorTrait

class ColorEqualTestCase(unittest.TestCase):

def test_strings(self):
color = 'blue'
inst1 = SimpleColorTraits(color=color)
inst2 = SimpleColorTraits(color=color)
self.assert_(colors_equal(inst1.color, inst2.color))

def test_tuples(self):
color = (0, 1, 0)
inst1 = SimpleColorTraits(color=color)
inst2 = SimpleColorTraits(color=color)
self.assert_(colors_equal(inst1.color, inst2.color))

def test_string_tuple(self):
color1 = 'red'
color2 = (1, 0, 0)
inst1 = SimpleColorTraits(color=color1)
inst2 = SimpleColorTraits(color=color2)
self.assert_(colors_equal(inst1.color, inst2.color))

def test_tuples_different_length(self):
color1 = (0, 1, 0)
color2 = (0, 1, 0, 1)
inst1 = SimpleColorTraits(color=color1)
inst2 = SimpleColorTraits(color=color2)
self.assert_(colors_equal(inst1.color, inst2.color))

def test_alpha(self):
color1 = (0, 1, 0, 1.0)
color2 = (0, 1, 0, 0.5)
inst1 = SimpleColorTraits(color=color1)
inst2 = SimpleColorTraits(color=color2)
self.assertTrue(colors_equal(inst1.color, inst2.color, match_alpha=False))
self.assertFalse(colors_equal(inst1.color, inst2.color, match_alpha=True))


if __name__ == '__main__':
import nose
nose.run()
unittest.main()
17 changes: 17 additions & 0 deletions chaco/tests/data_view_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ def test_empty(self):
self.assert_(dv.range2d.x_range==dv.index_range)
self.assert_(dv.range2d.y_range==dv.value_range)

def test_bgcolor(self):
""" Test setting background color """
color = 'blue'
dv = DataView(bgcolor=color)
self.assertEqual(dv.bgcolor, color)
color = (0,0,0,0)
dv = DataView(bgcolor=color)
self.assertEqual(dv.bgcolor, color)

def test_bg_gridcolor(self):
"""Test setting background to grid color"""
for color in ['lightgray', 'lightgray',
(0.827, 0.827, 0.827), (0.827, 0.827, 0.827, 1.0)]:
dv = DataView(bgcolor=color)
self.assertEqual(dv.bgcolor, color)
self.assertEqual(dv.x_grid.line_color, 'white')

def test_orientation(self):
dv = DataView()
x_mapper_start = dv.x_mapper
Expand Down
31 changes: 31 additions & 0 deletions chaco/tests/plot_test_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import unittest

from chaco.api import Plot


class PlotTestCase(unittest.TestCase):

def test_empty(self):
plot = Plot()
self.assertEqual(plot.orientation, "h")
self.assertEqual(plot.index_scale, "linear")
self.assertEqual(plot.bgcolor, "white")
self.assertEqual(plot.overlay_border, True)

self.assertEqual(plot.range2d.x_range, plot.index_range)
self.assertEqual(plot.range2d.y_range, plot.value_range)
self.assertEqual(plot.bgcolor, "white")

def test_bgcolor(self):
""" Test setting background color """
color = 'blue'
plot = Plot(bgcolor=color)
self.assertEqual(plot.bgcolor, color)
color = (0,0,0,0)
plot = Plot(bgcolor=color)
self.assertEqual(plot.bgcolor, color)


if __name__ == '__main__':
unittest.main()