diff --git a/chaco/api.py b/chaco/api.py index 2e49a0b65..612f9a4ba 100644 --- a/chaco/api.py +++ b/chaco/api.py @@ -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 diff --git a/chaco/base.py b/chaco/base.py index 885ada03c..e108b7d2b 100644 --- a/chaco/base.py +++ b/chaco/base.py @@ -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 diff --git a/chaco/data_view.py b/chaco/data_view.py index 9a69bc8f9..f7191a555 100644 --- a/chaco/data_view.py +++ b/chaco/data_view.py @@ -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 @@ -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): grid_color = 'white' if not self.x_grid and self.auto_grid: diff --git a/chaco/tests/base_utils_test_case.py b/chaco/tests/base_utils_test_case.py index f4495def6..338caf318 100644 --- a/chaco/tests/base_utils_test_case.py +++ b/chaco/tests/base_utils_test_case.py @@ -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): @@ -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() diff --git a/chaco/tests/data_view_test_case.py b/chaco/tests/data_view_test_case.py index 1465a48d7..287cc4019 100644 --- a/chaco/tests/data_view_test_case.py +++ b/chaco/tests/data_view_test_case.py @@ -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 diff --git a/chaco/tests/plot_test_case.py b/chaco/tests/plot_test_case.py new file mode 100644 index 000000000..3be8a2fc0 --- /dev/null +++ b/chaco/tests/plot_test_case.py @@ -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() +