/
colormodel.py
144 lines (94 loc) · 4 KB
/
colormodel.py
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
# colormodel.py
# Walker M. White (wmw2), Lillian Lee (LJL2), Steve Marschner (srm2)
# Feb 25, 2013
"""Class for RGB color objects, where the channel values are all ints in 0..255.
In the docstrings below, we use David Gries' range notation: a..b,
where a and b are integers and b >= a-1, represents the set of integers
from a to b, inclusive. If b == a-1, then a..b indicates the empty range
that contains no integers.
Don't confuse this range notation with python's range() function. The Gries
".." range notation is useful in discussions and descriptions; Python's
range() function is useful in lines of actual code. a..b includes b;
range(a,b) does NOT include b.
"""
class RGB(object):
"""An instance is a RGB color value."""
# METHODS
def __init__(self, r, g, b, a=255):
"""**Constructor**: creates a new RGB object (r,g,b,a).
:param r: initial red value :param g: initial green value
:param b: initial blue value
:param a: initial alpha value (default 255)
The alpha channel is 255 by default, unless otherwise specified,
so you can create RGB objects by specifying only three parameters.
Thus, saying RGB(255, 255, 255) is a valid call.
**Precondition**: r, g, b, a must all be in 0..255
(using Gries range notation, explained in colormodel.py's docstring).
"""
for value in (r,g,b,a):
if type(value) != int:
raise TypeError("value %s is not an int" % repr(value))
if value < 0 or value > 255:
raise ValueError("value %s is outside of range [0,255]" % repr(value))
self.red = r
self.green = g
self.blue = b
self.alpha = a
def __eq__(self, other):
"""Returns: True if self and other are equivalent RGB colors. """
return (type(other) == RGB and self.red == other.red and
self.green == other.green and self.blue == other.blue and
self.alpha == other.alpha)
def __ne__(self, other):
"""Returns: True if self and other are not equivalent RGB colors. """
return (type(other) != RGB or self.red != other.red or
self.green != other.green or self.blue != other.blue or
self.alpha != other.alpha)
def __str__(self):
"""Returns: Readable string representation of this color. """
from a3 import rgb_to_string # local to prevent circular import
return rgb_to_string(self)
def __repr__(self):
"""Returns: Unambiguous string representation of this color. """
return "(red="+str(self.red)+",green="+str(self.green)+",blue="+str(self.blue)+")"
def glColor(self):
"""Returns: 4-element list of the attributes scaled to be between 0 and 1,
inclusive
This is a conversion of this object's values into a format that can be
used in openGL graphics"""
return [self.red/255.0, self.green/255.0, self.blue/255.0, self.alpha/255.0]
def turtleColor(self):
"""Returns: 3-element tuple of the attributes scaled to be between 0 and 1,
inclusive
This is a conversion of this object's values into a format that can be
used in turtle graphics in CS1110 A4."""
return (self.red/255.0, self.green/255.0, self.blue/255.0)
# Color Constants
# The color Carnelian, or Cornell Red
CARNELIAN = RGB(179, 27, 27)
#: The color white
WHITE = RGB(255, 255, 255)
#: The color light gray
LIGHT_GRAY = RGB(192, 192, 192)
#: The color gray
GRAY = RGB(128, 128, 128)
#: The color dark gray
DARK_GRAY = RGB(64, 64, 64)
#: The color black
BLACK = RGB(0, 0, 0)
#: The color red,
RED = RGB(255, 0, 0)
#: The color pink
PINK = RGB(255, 175, 175)
#: The color orange
ORANGE = RGB(255, 200, 0)
#: The color yellow
YELLOW = RGB(255, 255, 0)
#: The color green
GREEN = RGB(0, 255, 0)
#: The color magenta
MAGENTA = RGB(255, 0, 255)
#: The color cyan
CYAN = RGB(0, 255, 255)
#: The color blue
BLUE = RGB(0, 0, 255)