1
# ------------------------------------------------------------------------------
2
#
3
#  Copyright (c) 2005, Enthought, Inc.
4
#  All rights reserved.
5
#
6
#  This software is provided without warranty under the terms of the BSD
7
#  license included in LICENSE.txt and may be redistributed only
8
#  under the conditions described in the aforementioned license.  The license
9
#  is also available online at http://www.enthought.com/licenses/BSD.txt
10
#
11
#  Thanks for using Enthought open source!
12
#
13
#  Author: David C. Morrill
14
#  Data:   02/14/2005
15
#
16
# ------------------------------------------------------------------------------
17

18 3
""" Trait definitions for an RGB-based color, which is a tuple of the form
19
    (*red*, *green*, *blue*), where *red*, *green* and *blue* are floats in the
20
    range from 0.0 to 1.0.
21
"""
22

23

24

25 3
from traits.api import Trait, TraitError
26

27 3
from traits.trait_base import SequenceTypes
28

29
# -------------------------------------------------------------------------
30
#  Convert a number into an RGB tuple:
31
# -------------------------------------------------------------------------
32

33

34 3
def range_check(value):
35
    """ Checks that *value* can be converted to a value in the range 0.0 to 1.0.
36

37
    If so, it returns the floating point value; otherwise, it raises a TraitError.
38
    """
39 0
    value = float(value)
40 11
    if 0.0 <= value <= 1.0:
41 0
        return value
42 0
    raise TraitError
43

44

45 3
def convert_to_color(object, name, value):
46
    """ Converts a tuple or an integer to an RGB color value, or raises a
47
    TraitError if that is not possible.
48
    """
49 11
    if (type(value) in SequenceTypes) and (len(value) == 3):
50 0
        return (
51
            range_check(value[0]),
52
            range_check(value[1]),
53
            range_check(value[2]),
54
        )
55 11
    if isinstance(value, int):
56 0
        num = int(value)
57 0
        return (
58
            (num / 0x10000) / 255.0,
59
            ((num / 0x100) & 0xFF) / 255.0,
60
            (num & 0xFF) / 255.0,
61
        )
62 3
    raise TraitError
63

64

65 3
convert_to_color.info = (
66
    "a tuple of the form (r,g,b), where r, g, and b "
67
    "are floats in the range from 0.0 to 1.0, or an integer which in hex is of "
68
    "the form 0xRRGGBB, where RR is red, GG is green, and BB is blue"
69
)
70

71
# -------------------------------------------------------------------------
72
#  Standard colors:
73
# -------------------------------------------------------------------------
74

75
# RGB versions of standard colors
76 3
rgb_standard_colors = {
77
    "aquamarine": (0.439216, 0.858824, 0.576471),
78
    "black": (0.0, 0.0, 0.0),
79
    "blue": (0.0, 0.0, 1.0),
80
    "blue violet": (0.623529, 0.372549, 0.623529),
81
    "brown": (0.647059, 0.164706, 0.164706),
82
    "cadet blue": (0.372549, 0.623529, 0.623529),
83
    "coral": (1.0, 0.498039, 0.0),
84
    "cornflower blue": (0.258824, 0.258824, 0.435294),
85
    "cyan": (0.0, 1.0, 1.0),
86
    "dark grey": (0.184314, 0.184314, 0.184314),
87
    "dark green": (0.184314, 0.309804, 0.184314),
88
    "dark olive green": (0.309804, 0.309804, 0.184314),
89
    "dark orchid": (0.6, 0.196078, 0.8),
90
    "dark slate blue": (0.419608, 0.137255, 0.556863),
91
    "dark slate grey": (0.184314, 0.309804, 0.309804),
92
    "dark turquoise": (0.439216, 0.576471, 0.858824),
93
    "dim grey": (0.329412, 0.329412, 0.329412),
94
    "firebrick": (0.556863, 0.137255, 0.137255),
95
    "forest green": (0.137255, 0.556863, 0.137255),
96
    "gold": (0.8, 0.498039, 0.196078),
97
    "goldenrod": (0.858824, 0.858824, 0.439216),
98
    "grey": (0.501961, 0.501961, 0.501961),
99
    "green": (0.0, 1.0, 0.0),
100
    "green yellow": (0.576471, 0.858824, 0.439216),
101
    "indian red": (0.309804, 0.184314, 0.184314),
102
    "khaki": (0.623529, 0.623529, 0.372549),
103
    "light blue": (0.74902, 0.847059, 0.847059),
104
    "light grey": (0.752941, 0.752941, 0.752941),
105
    "light steel": (0.0, 0.0, 0.0),
106
    "blue": (0.0, 0.0, 1.0),
107
    "lime green": (0.196078, 0.8, 0.196078),
108
    "magenta": (1.0, 0.0, 1.0),
109
    "maroon": (0.556863, 0.137255, 0.419608),
110
    "medium aquamarine": (0.196078, 0.8, 0.6),
111
    "medium blue": (0.196078, 0.196078, 0.8),
112
    "medium forest green": (0.419608, 0.556863, 0.137255),
113
    "medium goldenrod": (0.917647, 0.917647, 0.678431),
114
    "medium orchid": (0.576471, 0.439216, 0.858824),
115
    "medium sea green": (0.258824, 0.435294, 0.258824),
116
    "medium slate blue": (0.498039, 0.0, 1.0),
117
    "medium spring green": (0.498039, 1.0, 0.0),
118
    "medium turquoise": (0.439216, 0.858824, 0.858824),
119
    "medium violet red": (0.858824, 0.439216, 0.576471),
120
    "midnight blue": (0.184314, 0.184314, 0.309804),
121
    "navy": (0.137255, 0.137255, 0.556863),
122
    "orange": (0.8, 0.196078, 0.196078),
123
    "orange red": (1.0, 0.0, 0.498039),
124
    "orchid": (0.858824, 0.439216, 0.858824),
125
    "pale green": (0.560784, 0.737255, 0.560784),
126
    "pink": (0.737255, 0.560784, 0.917647),
127
    "plum": (0.917647, 0.678431, 0.917647),
128
    "purple": (0.690196, 0.0, 1.0),
129
    "red": (1.0, 0.0, 0.0),
130
    "salmon": (0.435294, 0.258824, 0.258824),
131
    "sea green": (0.137255, 0.556863, 0.419608),
132
    "sienna": (0.556863, 0.419608, 0.137255),
133
    "sky blue": (0.196078, 0.6, 0.8),
134
    "slate blue": (0.0, 0.498039, 1.0),
135
    "spring green": (0.0, 1.0, 0.498039),
136
    "steel blue": (0.137255, 0.419608, 0.556863),
137
    "tan": (0.858824, 0.576471, 0.439216),
138
    "thistle": (0.847059, 0.74902, 0.847059),
139
    "turquoise": (0.678431, 0.917647, 0.917647),
140
    "violet": (0.309804, 0.184314, 0.309804),
141
    "violet red": (0.8, 0.196078, 0.6),
142
    "wheat": (0.847059, 0.847059, 0.74902),
143
    "white": (1.0, 1.0, 1.0),
144
    "yellow": (1.0, 1.0, 0.0),
145
    "yellow green": (0.6, 0.8, 0.196078),
146
}
147

148
# -------------------------------------------------------------------------
149
#  Define 'null' specific color trait:
150
# -------------------------------------------------------------------------
151

152
### Note: Declare the editor to be a function which returns the RGBColorEditor
153
# class from traits ui to avoid circular import issues. For backwards
154
# compatibility with previous Traits versions, the 'editors' folder in Traits
155
# project declares 'from api import *' in its __init__.py. The 'api' in turn
156
# can contain classes that have a RGBColor trait which lead to this file getting
157
# imported. This will lead to a circular import when declaring a RGBColor
158
# trait.
159

160

161 3
def get_rgb_color_editor(*args, **traits):
162 0
    from ..editors.rgb_color_editor import (
163
        ToolkitEditorFactory as RGBColorEditor,
164
    )
165

166 0
    return RGBColorEditor(*args, **traits)
167

168

169
# Trait whose value must be an RGB color
170 3
RGBColor = Trait(
171
    "white", convert_to_color, rgb_standard_colors, editor=get_rgb_color_editor
172
)

Read our documentation on viewing source code .

Loading