~ubuntu-branches/debian/squeeze/python-imaging/squeeze

« back to all changes in this revision

Viewing changes to PIL/ImageColor.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-08-28 23:14:10 UTC
  • mfrom: (2.1.5 edgy)
  • Revision ID: james.westby@ubuntu.com-20060828231410-lca9enmne3ecmkup
Tags: 1.1.5-11
* python-imaging-sane: Depend on python-numarray. Closes: #382190.
* Add dependencies on ${shlibs:Depends}, lost in -6. Closes: #378596.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
2
2
# The Python Imaging Library
3
 
# $Id: //modules/pil/PIL/ImageColor.py#2 $
 
3
# $Id: ImageColor.py 2281 2005-02-07 20:42:04Z fredrik $
4
4
#
5
5
# map CSS3-style colour description strings to RGB
6
6
#
7
7
# History:
8
8
# 2002-10-24 fl   Added support for CSS-style color strings
9
9
# 2002-12-15 fl   Added RGBA support
 
10
# 2004-03-27 fl   Fixed remaining int() problems for Python 1.5.2
 
11
# 2004-07-19 fl   Fixed gray/grey spelling issues
10
12
#
11
 
# Copyright (c) 2002 by Secret Labs AB
12
 
# Copyright (c) 2002 by Fredrik Lundh
 
13
# Copyright (c) 2002-2004 by Secret Labs AB
 
14
# Copyright (c) 2002-2004 by Fredrik Lundh
13
15
#
14
16
# See the README file for information on usage and redistribution.
15
17
#
17
19
import Image
18
20
import re, string
19
21
 
 
22
try:
 
23
    x = int("a", 16)
 
24
except TypeError:
 
25
    # python 1.5.2 doesn't support int(x,b)
 
26
    str2int = string.atoi
 
27
else:
 
28
    str2int = int
 
29
 
20
30
##
21
31
# Convert color string to RGB tuple.
22
32
#
23
33
# @param color A CSS3-style colour string.
24
34
# @return An RGB-tuple.
25
 
# @exception ValueError String format not supported.
 
35
# @exception ValueError If the color string could not be interpreted
 
36
#    as an RGB value.
26
37
 
27
38
def getrgb(color):
28
39
    # FIXME: add RGBA support
44
55
    m = re.match("#\w\w\w$", color)
45
56
    if m:
46
57
        return (
47
 
            int(color[1]*2, 16),
48
 
            int(color[2]*2, 16),
49
 
            int(color[3]*2, 16)
 
58
            str2int(color[1]*2, 16),
 
59
            str2int(color[2]*2, 16),
 
60
            str2int(color[3]*2, 16)
50
61
            )
51
62
    m = re.match("#\w\w\w\w\w\w$", color)
52
63
    if m:
53
64
        return (
54
 
            int(color[1:3], 16),
55
 
            int(color[3:5], 16),
56
 
            int(color[5:7], 16)
 
65
            str2int(color[1:3], 16),
 
66
            str2int(color[3:5], 16),
 
67
            str2int(color[5:7], 16)
57
68
            )
58
69
    m = re.match("rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$", color)
59
70
    if m:
60
71
        return (
61
 
            int(m.group(1)),
62
 
            int(m.group(2)),
63
 
            int(m.group(3))
 
72
            str2int(m.group(1)),
 
73
            str2int(m.group(2)),
 
74
            str2int(m.group(3))
64
75
            )
65
76
    m = re.match("rgb\(\s*(\d+)%\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)$", color)
66
77
    if m:
67
78
        return (
68
 
            int((int(m.group(1)) * 255) / 100.0 + 0.5),
69
 
            int((int(m.group(2)) * 255) / 100.0 + 0.5),
70
 
            int((int(m.group(3)) * 255) / 100.0 + 0.5)
 
79
            int((str2int(m.group(1)) * 255) / 100.0 + 0.5),
 
80
            int((str2int(m.group(2)) * 255) / 100.0 + 0.5),
 
81
            int((str2int(m.group(3)) * 255) / 100.0 + 0.5)
71
82
            )
72
83
    m = re.match("hsl\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)$", color)
73
84
    if m:
98
109
    return color
99
110
 
100
111
colormap = {
101
 
    # X11 colour table (from "CSS3 module: Color working draft").  this
102
 
    # is a superset of HTML 4.0 color names used in CSS 1.
 
112
    # X11 colour table (from "CSS3 module: Color working draft"), with
 
113
    # gray/grey spelling issues fixed.  This is a superset of HTML 4.0
 
114
    # colour names used in CSS 1.
103
115
    "aliceblue": "#f0f8ff",
104
116
    "antiquewhite": "#faebd7",
105
117
    "aqua": "#00ffff",
125
137
    "darkcyan": "#008b8b",
126
138
    "darkgoldenrod": "#b8860b",
127
139
    "darkgray": "#a9a9a9",
 
140
    "darkgrey": "#a9a9a9",
128
141
    "darkgreen": "#006400",
129
142
    "darkkhaki": "#bdb76b",
130
143
    "darkmagenta": "#8b008b",
136
149
    "darkseagreen": "#8fbc8f",
137
150
    "darkslateblue": "#483d8b",
138
151
    "darkslategray": "#2f4f4f",
 
152
    "darkslategrey": "#2f4f4f",
139
153
    "darkturquoise": "#00ced1",
140
154
    "darkviolet": "#9400d3",
141
155
    "deeppink": "#ff1493",
142
156
    "deepskyblue": "#00bfff",
143
157
    "dimgray": "#696969",
 
158
    "dimgrey": "#696969",
144
159
    "dodgerblue": "#1e90ff",
145
160
    "firebrick": "#b22222",
146
161
    "floralwhite": "#fffaf0",
151
166
    "gold": "#ffd700",
152
167
    "goldenrod": "#daa520",
153
168
    "gray": "#808080",
 
169
    "grey": "#808080",
154
170
    "green": "#008000",
155
171
    "greenyellow": "#adff2f",
156
172
    "honeydew": "#f0fff0",
168
184
    "lightcyan": "#e0ffff",
169
185
    "lightgoldenrodyellow": "#fafad2",
170
186
    "lightgreen": "#90ee90",
 
187
    "lightgray": "#d3d3d3",
171
188
    "lightgrey": "#d3d3d3",
172
189
    "lightpink": "#ffb6c1",
173
190
    "lightsalmon": "#ffa07a",
174
191
    "lightseagreen": "#20b2aa",
175
192
    "lightskyblue": "#87cefa",
176
193
    "lightslategray": "#778899",
 
194
    "lightslategrey": "#778899",
177
195
    "lightsteelblue": "#b0c4de",
178
196
    "lightyellow": "#ffffe0",
179
197
    "lime": "#00ff00",
226
244
    "skyblue": "#87ceeb",
227
245
    "slateblue": "#6a5acd",
228
246
    "slategray": "#708090",
 
247
    "slategrey": "#708090",
229
248
    "snow": "#fffafa",
230
249
    "springgreen": "#00ff7f",
231
250
    "steelblue": "#4682b4",