def test_get_color_rgba_changed(self): value_type = ConstantValue(color=Color()) with self.assertTraitChanges(value_type, 'updated'): value_type.color.rgba = (0.4, 0.2, 0.6, 0.8) self.assertEqual( value_type.get_color(self.model, [0], [0]), Color(rgba=(0.4, 0.2, 0.6, 0.8)) )
def test_validate_color(self): color = (0.4, 0.2, 0.6, 1.0) trait = RGBAColor() validated = trait.validate(None, None, Color(rgba=color)) self.assertIs( validated, color )
def set_editor_value(self, model, row, column, value): """ Set the editable representation of the underlying value. The default expects a string that can be parsed to a color value. Parameters ---------- model : AbstractDataModel The data model holding the data. row : sequence of int The row in the data model being queried. column : sequence of int The column in the data model being queried. value : str A string that can be parsed to a color value. Returns ------- success : bool Whether or not the value was successfully set. """ try: color = Color.from_str(value) except Exception: raise DataViewSetError() return super().set_editor_value(model, row, column, color)
def test_set_text(self): value = ColorValue() value.set_text(self.model, [0], [0], "red") self.model.set_value.assert_called_once_with( [0], [0], Color(rgba=(1.0, 0.0, 0.0, 1.0)), )
def test_set_editor_value(self): value = ColorValue() value.set_editor_value(self.model, [0], [0], "#3399CC66") self.model.set_value.assert_called_once_with( [0], [0], Color(rgba=(0.2, 0.6, 0.8, 0.4)), )
def get_color(self, model, row, column): """ Get data-associated colour values for the given item. The default implementation returns white. color : Color instance The color associated with the cell. """ return Color(rgba=(1.0, 1.0, 1.0, 1.0))
def test_set_text_no_set_value(self): self.model.can_set_value = Mock(return_value=False) value = ColorValue() value.set_text(self.model, [0], [0], "red") self.model.set_value.assert_called_once_with( [0], [0], Color(rgba=(1.0, 0.0, 0.0, 1.0)), )
def validate(self, object, name, value): if isinstance(value, Color): return value if isinstance(value, str): try: return Color.from_str(value) except ColorParseError: self.error(object, name, value) is_array = (np is not None and isinstance(value, (np.ndarray, np.void))) if is_array or isinstance(value, Sequence): channels = tuple(value) if len(channels) == 4: return Color(rgba=channels) elif len(channels) == 3: return Color(rgb=channels) self.error(object, name, value)
def get_color(self, model, row, column): """ Get data-associated colour values for the given item. The default implementation returns white. Parameters ---------- model : AbstractDataModel The data model holding the data. row : sequence of int The row in the data model being queried. column : sequence of int The column in the data model being queried. Returns ------- color : Color instance The color associated with the cell. """ return Color(rgba=(1.0, 1.0, 1.0, 1.0))
def test_hex(self): color = Color(rgba=(0.4, 0.2, 0.6, 0.8)) hex_value = color.hex() self.assertEqual(hex_value, "#663399CC")
def test_hex_black(self): color = Color(rgba=(0.0, 0.0, 0.0, 1.0)) hex_value = color.hex() self.assertEqual(hex_value, "#000000FF")
def test_from_str_duplicate_argument(self): with self.assertRaises(TypeError): Color.from_str('rebeccapurple', rgba=(1.0, 1.0, 1.0, 1.0))
def test_toolkit_round_trip(self): color = Color(rgba=(0.4, 0.2, 0.6, 0.8)) toolkit_color = color.to_toolkit() result = Color.from_toolkit(toolkit_color) self.assertEqual(result.rgba, (0.4, 0.2, 0.6, 0.8))
def test_from_str_hex(self): color = Color.from_str('#663399ff') self.assertEqual(color.rgba, (0.4, 0.2, 0.6, 1.0))
def test_from_str_extra_argument(self): color = Color.from_str('#663399', alpha=0.5) self.assertEqual(color.rgba, (0.4, 0.2, 0.6, 0.5))
def test_init_hls(self): color = Color(hls=(0.4, 0.2, 0.6)) self.assert_tuple_almost_equal(color.rgba, (0.08, 0.32, 0.176, 1.0))
def test_init(self): color = Color() self.assertEqual(color.rgba, (1.0, 1.0, 1.0, 1.0))
def test_set_hlsa(self): color = Color() color.hlsa = (0.4, 0.2, 0.6, 0.8) self.assert_tuple_almost_equal(color.rgba, (0.08, 0.32, 0.176, 0.8))
def test_get_hls(self): color = Color(rgba=(0.08, 0.32, 0.176, 0.8)) self.assert_tuple_almost_equal(color.hls, (0.4, 0.2, 0.6))
def test_has_color(self): value_type = ConstantValue(color=Color(rgba=(0.4, 0.2, 0.6, 0.8))) self.assertTrue(value_type.has_color(self.model, [0], [0]))
def test_init_rgb(self): color = Color(rgb=(0.4, 0.2, 0.6)) self.assertEqual(color.rgba, (0.4, 0.2, 0.6, 1.0))
def test_init_rgba(self): color = Color(rgba=(0.4, 0.2, 0.6, 0.8)) self.assertEqual(color.rgba, (0.4, 0.2, 0.6, 0.8))
def test_eq_not_equal(self): color_1 = Color(rgba=(0.4, 0.2, 0.6, 0.8)) color_2 = Color(rgba=(0.4, 0.4, 0.6, 0.8)) self.assertTrue(color_1 != color_2) self.assertFalse(color_1 == color_2)
def test_from_str_name(self): color = Color.from_str('rebeccapurple') self.assertEqual(color.rgba, (0.4, 0.2, 0.6, 1.0))
def test_init_r_g_b_a(self): color = Color(red=0.4, green=0.2, blue=0.6, alpha=0.8) self.assertEqual(color.rgba, (0.4, 0.2, 0.6, 0.8))
def test_get_is_dark(self): color = Color(rgba=(0.08, 0.32, 0.176, 0.8)) self.assertTrue(color.is_dark)
def test_get_color(self): value_type = ConstantValue(color='rebeccapurple') self.assertEqual( value_type.get_color(self.model, [0], [0]), Color(rgba=(0.4, 0.2, 0.6, 1.0)) )
def test_init_r_g_b(self): color = Color(red=0.4, green=0.2, blue=0.6) self.assertEqual(color.rgba, (0.4, 0.2, 0.6, 1.0))
def favorite_color(): return Color(hsv=(uniform(0.0, 1.0), 1.0, 1.0))
def test_init_hsv(self): color = Color(hsv=(0.4, 0.2, 0.6)) self.assert_tuple_almost_equal(color.rgba, (0.48, 0.6, 0.528, 1.0))