/
color.py
109 lines (82 loc) · 2.69 KB
/
color.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
"""
color.py
The color module defines the Color class and some popular Color
objects.
"""
#-----------------------------------------------------------------------
class Color:
"""
A Color object models an RGB color.
"""
#-------------------------------------------------------------------
def __init__(self, r=0, g=0, b=0):
"""
Construct 'self' such that it has the given red ('r'),
green ('g'), and blue ('b') components.
"""
self._r = r # Red component
self._g = g # Green component
self._b = b # Blue component
#-------------------------------------------------------------------
def getRed(self):
"""
Return the red component of 'self'.
"""
return self._r
#-------------------------------------------------------------------
def getGreen(self):
"""
Return the green component of 'self'.
"""
return self._g
#-------------------------------------------------------------------
def getBlue(self):
"""
Return the blue component of 'self'.
"""
return self._b
#-------------------------------------------------------------------
def __str__(self):
"""
Return the string equivalent of 'self', that is, a
string of the form '#rrggbb'.
"""
return '#%02x%02x%02x' % (self._r, self._g, self._b)
#-----------------------------------------------------------------------
# Some predefined Color objects:
WHITE = Color(255, 255, 255)
BLACK = Color( 0, 0, 0)
RED = Color(255, 0, 0)
GREEN = Color( 0, 255, 0)
BLUE = Color( 0, 0, 255)
CYAN = Color( 0, 255, 255)
MAGENTA = Color(255, 0, 255)
YELLOW = Color(255, 255, 0)
DARK_RED = Color(128, 0, 0)
DARK_GREEN = Color( 0, 128, 0)
DARK_BLUE = Color( 0, 0, 128)
GRAY = Color(128, 128, 128)
DARK_GRAY = Color( 64, 64, 64)
LIGHT_GRAY = Color(192, 192, 192)
ORANGE = Color(255, 200, 0)
VIOLET = Color(238, 130, 238)
PINK = Color(255, 175, 175)
# Shade of blue used in Introduction to Programming in Java.
# It is Pantone 300U. The RGB values are approximately (9, 90, 166).
BOOK_BLUE = Color( 9, 90, 166)
BOOK_LIGHT_BLUE = Color(103, 198, 243)
# Shade of red used in Algorithms 4th edition
BOOK_RED = Color(150, 35, 31)
#-----------------------------------------------------------------------
def main():
"""
For testing:
"""
import stdio
c1 = Color(128, 128, 128)
stdio.writeln(c1)
stdio.writeln(c1.getRed())
stdio.writeln(c1.getGreen())
stdio.writeln(c1.getBlue())
if __name__ == '__main__':
main()