-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenCanvas.py
56 lines (49 loc) · 1.71 KB
/
ScreenCanvas.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
import copy
class Canvas:
'''
Nice abstraction to work on a Canvas.
Used by Display, Transportation and Patterns
It is the output of a pattern
At the moment it uses a list of lists, but the implementation
may change to use a numpy matrix, or an Array for performance reasons
The values of the color channels on the canvas should be floats from 0 to 1.
'''
def __init__(self,height, width, colorArray=None):
if (colorArray==None):
self._colorArray = [[(0,0,0) for x in xrange(width)] for y in xrange(height)]
else:
self._colorArray=colorArray
self.height=height
self.width=width
def __getitem__(self, arg):
if isinstance(arg,tuple):
y,x = arg
return self.getRow(y)[x]
else:
y=arg
return self.getRow(y)
def __setitem__(self,arg,value):
if isinstance(arg,tuple):
y,x = arg
rowY= self.getRow(y)
rowY[x]= value
else:
y=arg
rowY= self.getRow(y)
rowY=value
def getRow(self, y):
return self._colorArray[y]
def mapFunction(self,function):
for y in xrange(self.height):
for x in xrange(self.width):
self[y,x]=function(self[y,x], y, x)
def getByte(self, y,x):
return _floatToByte(self[y,x])
def _floatToByte(self, f):
return max(min(255,round(f*255)),0)
def __deepcopy__(self,memo):
height=self.height
width=self.width
newColorarray=[[copy.deepcopy(self[y,x]+tuple()) for x in xrange(width)] for y in xrange(height)]
newone=Canvas(self.height,self.width,newColorarray)
return newone