File: 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 (129 lines) | stat: -rw-r--r-- 4,044 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
# (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!

# ------------------------------------------------------------------------------
# Copyright (c) 2007, Riverbank Computing Limited
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD license.
# However, when used with the GPL version of PyQt the additional terms
# described in the PyQt GPL exception also apply

#
# Author: Riverbank Computing Limited
# ------------------------------------------------------------------------------

""" Trait definition for a PyQt-based color.
"""

from ast import literal_eval

from pyface.qt import QtGui
from pyface.color import Color as PyfaceColor
from pyface.util.color_helpers import channels_to_ints
from pyface.util.color_parser import color_table
from traits.api import Trait, TraitError


def convert_to_color(object, name, value):
    """Converts a number into a QColor object."""
    # Try the toolkit agnostic format.
    try:
        tup = literal_eval(value)
    except Exception:
        tup = value

    if isinstance(value, str):
        # Allow for spaces in the string value.
        value = value.replace(" ", "")

        # is it in the color table?
        if value in color_table:
            tup = channels_to_ints(color_table[value])

    if isinstance(tup, tuple):
        if 3 <= len(tup) <= 4 and all(isinstance(x, int) for x in tup):
            try:
                color = QtGui.QColor(*tup)
            except Exception:
                raise TraitError
        else:
            raise TraitError
    elif isinstance(value, PyfaceColor):
        color = value.to_toolkit()
    else:
        # Let the standard ctors handle the value.
        try:
            color = QtGui.QColor(value)
        except TypeError:
            raise TraitError

    if not color.isValid():
        raise TraitError

    return color


convert_to_color.info = (
    "a string of the form (r,g,b) or (r,g,b,a) where r, "
    "g, b, and a are integers from 0 to 255, a QColor "
    "instance, a Qt.GlobalColor, an integer which in hex "
    "is of the form 0xRRGGBB, a string of the form #RGB, "
    "#RRGGBB, #RRRGGGBBB or #RRRRGGGGBBBB"
)

# -------------------------------------------------------------------------
#  Standard colors:
# -------------------------------------------------------------------------

standard_colors = {}
for name, rgba in color_table.items():
    rgba_bytes = channels_to_ints(rgba)
    standard_colors[str(name)] = QtGui.QColor(*rgba_bytes)

# -------------------------------------------------------------------------
#  Callable that returns an instance of the PyQtToolkitEditorFactory for color
#  editors.
# -------------------------------------------------------------------------

### FIXME: We have declared the 'editor' to be a function instead of  the
# traitsui.qt.color_editor.ToolkitEditorFactory class, since the
# latter is leading to too many circular imports. In the future, try to see if
# there is a better way to do this.


def get_color_editor(*args, **traits):
    from traitsui.qt.color_editor import ToolkitEditorFactory

    return ToolkitEditorFactory(*args, **traits)


def PyQtColor(default="white", allow_none=False, **metadata):
    """Defines PyQt-specific color traits."""
    if default is None:
        allow_none = True

    if allow_none:
        return Trait(
            default,
            None,
            standard_colors,
            convert_to_color,
            editor=get_color_editor,
            **metadata,
        )

    return Trait(
        default,
        standard_colors,
        convert_to_color,
        editor=get_color_editor,
        **metadata,
    )