1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
# (C) Copyright 2008-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
import unittest
import wx
from pyface.color import Color as PyfaceColor
from traits.api import HasStrictTraits, TraitError
from traitsui.wx.color_trait import WxColor
class ObjectWithColor(HasStrictTraits):
color = WxColor()
class ObjectWithColorAllowsNone(HasStrictTraits):
color = WxColor(allow_none=True)
class TestWxColor(unittest.TestCase):
def test_default(self):
obj = ObjectWithColor()
self.assertIsInstance(obj.color, wx.Colour)
self.assertEqual(obj.color.Get(), (255, 255, 255, 255))
self.assertIsInstance(obj.color_, wx.Colour)
self.assertEqual(obj.color_.Get(), (255, 255, 255, 255))
def test_tuple_rgb(self):
obj = ObjectWithColor(color=(0, 128, 255))
self.assertIsInstance(obj.color, wx.Colour)
self.assertEqual(obj.color.Get(), (0, 128, 255, 255))
self.assertIsInstance(obj.color_, wx.Colour)
self.assertEqual(obj.color_.Get(), (0, 128, 255, 255))
def test_tuple_rgba(self):
obj = ObjectWithColor(color=(0, 128, 255, 64))
self.assertIsInstance(obj.color, wx.Colour)
self.assertEqual(obj.color.Get(), (0, 128, 255, 64))
self.assertIsInstance(obj.color_, wx.Colour)
self.assertEqual(obj.color_.Get(), (0, 128, 255, 64))
def test_name_string(self):
obj = ObjectWithColor(color="rebeccapurple")
self.assertIsInstance(obj.color, wx.Colour)
self.assertEqual(obj.color.Get(), (0x66, 0x33, 0x99, 0xff))
self.assertIsInstance(obj.color_, wx.Colour)
self.assertEqual(obj.color_.Get(), (0x66, 0x33, 0x99, 0xff))
def test_name_string_with_space(self):
obj = ObjectWithColor(color="rebecca purple")
self.assertIsInstance(obj.color, wx.Colour)
self.assertEqual(obj.color.Get(), (0x66, 0x33, 0x99, 0xff))
self.assertIsInstance(obj.color_, wx.Colour)
self.assertEqual(obj.color_.Get(), (0x66, 0x33, 0x99, 0xff))
def test_rgb_string(self):
obj = ObjectWithColor(color="(0, 128, 255)")
self.assertIsInstance(obj.color, wx.Colour)
self.assertEqual(obj.color.Get(), (0, 128, 255, 255))
self.assertIsInstance(obj.color_, wx.Colour)
self.assertEqual(obj.color_.Get(), (0, 128, 255, 255))
def test_rgba_string(self):
obj = ObjectWithColor(color="(0, 128, 255, 64)")
self.assertIsInstance(obj.color, wx.Colour)
self.assertEqual(obj.color.Get(), (0, 128, 255, 64))
self.assertIsInstance(obj.color_, wx.Colour)
self.assertEqual(obj.color_.Get(), (0, 128, 255, 64))
def test_rgb_int(self):
obj = ObjectWithColor(color=0x0088ff)
self.assertIsInstance(obj.color, wx.Colour)
self.assertEqual(obj.color.Get(), (0x00, 0x88, 0xff, 0xff))
self.assertIsInstance(obj.color_, wx.Colour)
self.assertEqual(obj.color_.Get(), (0x00, 0x88, 0xff, 0xff))
def test_pyface_color(self):
obj = ObjectWithColor(color=PyfaceColor(rgba=(0.0, 0.5, 1.0, 0.25)))
self.assertIsInstance(obj.color, wx.Colour)
self.assertEqual(obj.color.Get(), (0, 128, 255, 64))
self.assertIsInstance(obj.color_, wx.Colour)
self.assertEqual(obj.color_.Get(), (0, 128, 255, 64))
def test_default_none(self):
obj = ObjectWithColorAllowsNone(color=None)
self.assertIsNone(obj.color)
self.assertIsNone(obj.color_)
def test_bad_color(self):
with self.assertRaises(TraitError):
ObjectWithColor(color="not a color")
def test_bad_tuple(self):
with self.assertRaises(TraitError):
ObjectWithColor(color=(0xff, 0xff))
def test_bad_tuple_not_int(self):
with self.assertRaises(TraitError):
ObjectWithColor(color=("not an int", 0xff, 0xff))
def test_bad_tuple_string(self):
with self.assertRaises(TraitError):
ObjectWithColor(color="(0xff, 0xff)")
|