-
Notifications
You must be signed in to change notification settings - Fork 0
/
colouring.py
195 lines (137 loc) · 5.21 KB
/
colouring.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#colouring.py
#Colouring algorithms for Fractale
import math
import cmath
from cspace import CSpace
class Colouring(object):
'''Class for colouring escape-time fractals.'''
'''Due to load order, this stuff has to go first.'''
#Tools
def interpolate(self,n):
#Determine what the colour would be if n was one of the integers it
#sits between.
floor = self.pallet[int(n)%len(self.pallet)]
ceiling = self.pallet[(int(n)+1)%len(self.pallet)]
#N can be negative. Bytes cant.
absn = abs(n)
#Determine distance of decimal part of n from ceiling and floor.
cDist = absn - int(absn)
fDist = 1-cDist
#Return a colour that far from the two primaries.
return [abs(int(cDist*ceiling[i] + fDist*floor[i])) for i in range(3)]
def rotate(self,n):
'''Rotate the pallet.
n = any non-negative number.
TODO: Fractional rots. W/Interpolation, should allow the simulation
through not reality of the Fractional Iteration Method.
'''
self.pallet = self.pallet[rot%len(pals[indice]):] +\
self.pallet[:rot%len(pals[indice])]
#Colouring Algorithms
###Interior+Exterior
def mono(self,z,escTime,itr,c):
ext = [0,0,0]
inte = [255,255,255]
if escTime == itr:
return ext
return ext
def negarc(self,z,escTime,itr,c):
z=z[-1]
return self.interpolate(z.real - abs(z.imag))
###Exterior-only
def eta(self,z,escTime,itr,c):
if type(escTime) is int:
return self.pallet[escTime%(len(self.pallet))]
else:
if escTime is complex:
escTime = float(escTime.real)
return self.interpolate(escTime)
def etaSm(self, z, escTime, itr,c):
z = z[-1]
escTime = abs(escTime + 1 - (cmath.log( cmath.log(abs(z)) /
cmath.log(2) ) / cmath.log(2)))
return self.interpolate(escTime.real)
#Experimental
def arcinv(self, z, escTime, itr, c):
z = z[-1] #This algorithm only uses the last iteration of z.
#z = (cmath.log( cmath.log(z) /
#cmath.log(2) ) / cmath.log(2))
r = abs(z.real)
g = abs(z.imag)
b = abs(z)
rgb = [i for i in [r,g,b]]
self.CSpace.setRGB(rgb)
self.CSpace.rgb2hsv()
self.CSpace.hsv[2] = sum(rgb)-self.CSpace.hsv[2]
return [int(255*(2-i)/2)%256 for i in self.CSpace.hsv2rgb()]
def recursiveArc(self, z, escTime, itr, c):
#Maps points on the fractal to int/ext of mandelbrot
z = z[-1] #only use last z.
if abs(z) == 0: z = -1
c = (z/abs(z))*.255 - 1
z = [0]
zenith = 1
i=0
while (abs(zenith) < 4) and i != 44:
i += 1
z += [z[-1] ** 2 + c]
zenith = z[-1]
return self.arcinv(z,i,0,0)
def cPic(self,n):
if n==0: function = self.mono
elif n==1: function = self.eta
elif n==2: function = self.negarc
elif n==3: function = self.etaSm
elif n == 4: function = self.arcinv
elif n == 5: function = self.recursiveArc
elif n==92271: function = self.expy
return function
#Algorithm Control Vars
pal = 0
inside = 0
outside = 0
#Int/Ext Algorithms
iCAs = [mono,negarc]
oCAs = [eta,negarc,mono]
#Basic Pallets
pale = [
#Basic Rainbow
[[255, 0, 0],[255, 69, 0],[255, 255, 0],
[0, 255, 0],[0, 128, 128],[0, 0, 255],
[128, 0, 128],[255, 0, 255]],
#Green-Blue-Red slide.
[[127, 127, 0], [63, 191, 0], [0, 255, 0],
[0, 191, 63], [0, 127, 127], [0, 63, 191],
[0, 0, 255], [63, 0, 191], [127, 0, 127],
[191, 0, 63], [255, 0, 0], [191, 63, 0]],
#Classic Blue-Black-Amber-White
[[25,7,26],[9,1,47],[4,4,73],[0,7,100],
[12,44,138],[24,82,177],[57,125,209],
[134,181,229],[211,236,248],[241,233,191],
[248,201,95],[255,170,0],[204,128,0],
[157,87,0],[106,52,3],[66,30,15]],
#Smooth Rainbow
[[255, 0, 0], [255, 63, 0], [255, 127, 0], [255, 191, 0],
[255, 255, 0], [191, 255, 0], [127, 255, 0], [63, 255, 0],
[0, 255, 0], [0, 255, 63], [0, 255, 127], [0, 255, 191],
[0, 255, 255], [0, 191, 255], [0, 127, 255], [0, 63, 255],
[0, 0, 255], [63, 0, 255], [127, 0, 255], [191, 0, 255],
[255, 0, 255], [255, 0, 191], [255, 0, 127], [255, 0, 63]],
#Less Smooth Rainbow
[[255, 0, 0], [255, 127, 0],
[255, 255, 0], [127, 255, 0],
[0, 255, 0], [0, 255, 127],
[0, 255, 255], [0, 127, 255],
[0, 0, 255], [127, 0, 255],
[255, 0, 255], [255, 0, 127]]
]
def __init__(self):
self.pallet = self.pale[self.pal]
self.insideC = self.iCAs[self.inside]
self.outsideC = self.oCAs[self.outside]
self.CSpace = CSpace()
def updateInside(self,inside):
self.inside = inside
def updateMulti(self,**kargs):
for i in kargs: setattr(self,i,kargs[i])
self.__init__()