File: test_color_trait.py

package info (click to toggle)
python-traitsui 8.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,232 kB
  • sloc: python: 58,982; makefile: 113
file content (156 lines) | stat: -rw-r--r-- 5,840 bytes parent folder | download
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# (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

from pyface.qt import QtGui
from pyface.color import Color as PyfaceColor
from traits.api import HasStrictTraits, TraitError

from traitsui.qt.color_trait import PyQtColor


class ObjectWithColor(HasStrictTraits):

    color = PyQtColor()


class ObjectWithColorAllowsNone(HasStrictTraits):

    color = PyQtColor(allow_none=True)


class TestPyQtColor(unittest.TestCase):

    def test_default(self):
        obj = ObjectWithColor()

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (255, 255, 255, 255))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (255, 255, 255, 255))

    def test_tuple_rgb(self):
        obj = ObjectWithColor(color=(0, 128, 255))

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0, 128, 255, 255))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (0, 128, 255, 255))

    def test_tuple_rgba(self):
        obj = ObjectWithColor(color=(0, 128, 255, 64))

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0, 128, 255, 64))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (0, 128, 255, 64))

    def test_name_string(self):
        obj = ObjectWithColor(color="rebeccapurple")

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0x66, 0x33, 0x99, 0xff))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (0x66, 0x33, 0x99, 0xff))

    def test_name_string_with_space(self):
        obj = ObjectWithColor(color="rebecca purple")

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0x66, 0x33, 0x99, 0xff))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (0x66, 0x33, 0x99, 0xff))

    def test_rgb_string(self):
        obj = ObjectWithColor(color="(0, 128, 255)")

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0, 128, 255, 255))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (0, 128, 255, 255))

    def test_rgba_string(self):
        obj = ObjectWithColor(color="(0, 128, 255, 64)")

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0, 128, 255, 64))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (0, 128, 255, 64))

    def test_rgb_int_string_3(self):
        obj = ObjectWithColor(color="#08f")

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0x00, 0x88, 0xff, 0xff))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (0x00, 0x88, 0xff, 0xff))

    def test_rgb_int_string_6(self):
        obj = ObjectWithColor(color="#0088ff")

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0x00, 0x88, 0xff, 0xff))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (0x00, 0x88, 0xff, 0xff))

    def test_rgb_int_string_12(self):
        obj = ObjectWithColor(color="#00008888ffff")

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0x00, 0x88, 0xff, 0xff))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (0x00, 0x88, 0xff, 0xff))

    def test_rgb_int(self):
        obj = ObjectWithColor(color=0x0088ff)

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0x00, 0x88, 0xff, 0xff))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (0x00, 0x88, 0xff, 0xff))

    def test_qcolor(self):
        obj = ObjectWithColor(color=QtGui.QColor(0, 128, 255, 64))

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0, 128, 255, 64))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (0, 128, 255, 64))

    def test_pyface_color(self):
        obj = ObjectWithColor(color=PyfaceColor(rgba=(0.0, 0.5, 1.0, 0.25)))

        self.assertIsInstance(obj.color, QtGui.QColor)
        self.assertEqual(obj.color.getRgb(), (0, 128, 255, 64))
        self.assertIsInstance(obj.color_, QtGui.QColor)
        self.assertEqual(obj.color_.getRgb(), (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)")