plasma.html
001| '''Plasma.
001|
001| #define PI 3.1415926535897932384626433832795
001|
001| uniform float fmod(u_time, 12*pi);
001| uniform vec2 u_k;
001| varying vec2 v_coords;
001|
001| void main() {
001| float v = 0.0;
001| vec2 c = v_coords * u_k - u_k/2.0;
001| v += sin((c.x+u_time));
001| v += sin((c.y+u_time)/2.0);
001| v += sin((c.x+c.y+u_time)/2.0);
001| c += u_k/2.0 * vec2(sin(u_time/3.0), cos(u_time/2.0));
001| v += sin(sqrt(c.x*c.x+c.y*c.y+1.0)+u_time);
001| v = v/2.0;
001| vec3 col = vec3(1, sin(PI*v), cos(PI*v));
001| gl_FragColor = vec4(col*.5 + .5, 1);
001| }
001|
001| Coder@Sonnack.com
001| December 2013
001| '''
002|
003| from sys import argv
004| from os import path
005| from datetime import datetime, date, timedelta
006| from random import random
007| from math import sqrt, cos, sin, pi, radians
008| from logger import logger, info, debug, trace
009| from colors import *
010| from img import Img, Chart, XYChart, fBox, tAdd, tSub, dispatch
011|
012| BasePath = r'C:\CJS\prj\Python\ws\PIL'
013| ImgsPath = path.join(BasePath, 'img')
014| Log = logger('plasma')
015|
016| ImageDims = (400, 400)
017|
018| fAverage = lambda cs: int(float(sum(cs)) / float(len(cs)))
019|
020| fPlusMinus = lambda r: int(r - (((2*r)+1) * random()))
021|
022| f0Gray = lambda a: int(256.0 * (float(a % 360)/360.0))
023| f1Gray = lambda a: int(127.0 + (127.0 * sin(radians(a))))
024| f2Gray = lambda a: int(127.0 + (127.0 * cos(radians(a))))
025|
026| f0Red = lambda a: int(127 + (127.0 * sin(radians(a))))
027| f0Green = lambda a: int(127 + (127.0 * cos(radians(a))))
028| f0Blue = lambda a: int(256 * (float(a % 360)/360.0))
029|
030| f1Red = lambda a: int(127 + (127.0 * cos(radians(a))))
031| f1Green = lambda a: int(127 + (127.0 * sin(radians(a))))
032| f1Blue = lambda a: int(127 + (127.0 * cos(radians(a*2.0))))
033|
034| f2Red = lambda a: int(127 + (127.0 * sin(radians(a))))
035| f2Green = lambda a: int(127 + (127.0 * cos(radians(a))))
036| f2Blue = lambda a: int(127 + (127.0 * cos(radians(-a))))
037|
038| f3Red = lambda a: int(127 + (127.0 * sin(radians(a*2.0))))
039| f3Green = lambda a: int(127 + (127.0 * sin(radians(a*3.0))))
040| f3Blue = lambda a: int(127 + (127.0 * sin(radians(a*4.0))))
041|
042| fAngle2Gray0 = lambda a: (f0Gray(a), f0Gray(a), f0Gray(a))
043| fAngle2Gray1 = lambda a: (f1Gray(a), f1Gray(a), f1Gray(a))
044| fAngle2Gray2 = lambda a: (f2Gray(a), f2Gray(a), f2Gray(a))
045|
046| fAngle2Color0 = lambda a: (f0Red(a), f0Green(a), f0Blue(a))
047| fAngle2Color1 = lambda a: (f1Red(a), f1Green(a), f1Blue(a))
048| fAngle2Color2 = lambda a: (f2Red(a), f2Green(a), f2Blue(a))
049| fAngle2Color3 = lambda a: (f3Red(a), f3Green(a), f3Blue(a))
050|
051| ColorMap = []
052|
053|
054|
055| class PlasmaCloud (object):
056| '''\
056| Plasma Cloud Image class.
056|
056| An instance of this class creates a data matrix and generates a Plasma Cloud
056| fractal (based on the fractint version of same). The data can be copied to
056| an image object and saved to create an image file.
056|
056| The algorithm starts with the image rectangle and randomly generates colors
056| for each of the corners. Then it begins a recursive process that takes any
056| rectangle (and its four corner colors) and:
056| 1. Determines the X and Y mid-points.
056| 2. This results in five new point; four side mid-points and a center mid-point.
056| 3. Generate random colors for all five new points.
056| OR use the existing colors if a side mid-point was visted previously.
056| 4. Recurse on each of the four sub-rectangles.
056| IF the rectangle warrents it by having at least one mid-point.
056| (IOW: Recursion ends when the sub-dividing reaches pixel level.)
056|
056| '''
057| Jitter = 200
058|
059| @staticmethod
060| def fJitter(v):
061| return fPlusMinus(min(v,PlasmaCloud.Jitter))
062|
063| def __init__ (self, dims, top):
064| self.dims = dims
065| self.top = top
066| self.data = [[int(-1) for x in range(self.dims[0])] for y in range(self.dims[1])]
067| self._initialize()
068| self._plot((0,0), (self.dims[0]-1, self.dims[1]-1), 0)
069|
070| def get (self, xy):
071| '''Get a point value given an (x,y) tuple.'''
072| self._check_xy_argument(xy)
073| x,y = xy
074| return self.data[x][y]
075|
076| def put (self, xy, value):
077| '''Set a point value using an (x,y) tuple.'''
078| self._check_xy_argument(xy)
079| x,y = xy
080| self.data[x][y] = value
081|
082| def copy_to_image (self, im, fcolor):
083| for y in range(self.dims[1]):
084| for x in range(self.dims[0]):
085| pt = (x,y)
086| c = self.get(pt)
087| im.draw.point(pt, ColorWhite if c < 0 else fcolor(c))
088|
089| def _check_xy_argument (self, xy):
090| '''Validate an Index pair.'''
091| if not (isinstance(xy,tuple) or isinstance(xy,list)):
092| raise TypeError('Index must be a tuple or list! (not a "%s")' % type(xy).__name__)
093| if (self.dims[0] <= xy[0]) or (self.dims[1] < xy[1]):
094| raise IndexError('Index out of bounds! %s' % str(xy))
095|
096| def _initialize (self):
097| '''Initialize the Plasma Plot. Generate the first four corner colors. '''
098| self.data[ 0 ][ 0 ] = int(random() * self.top)
099| self.data[ 0 ][self.dims[1]-1] = int(random() * self.top)
100| self.data[self.dims[0]-1][self.dims[1]-1] = int(random() * self.top)
101| self.data[self.dims[0]-1][ 0 ] = int(random() * self.top)
102|
103| def _plot (self, p_ulc, p_lrc, depth):
104| '''Plasma Plot. (Recursive!)'''
105|
106| x_mid = fAverage([p_ulc[0], p_lrc[0]])
107| y_mid = fAverage([p_ulc[1], p_lrc[1]])
108|
109| p_urc = (p_lrc[0], p_ulc[1])
110| p_llc = (p_ulc[0], p_lrc[1])
111|
112| p_ct = (x_mid, y_mid)
113| p_up = (x_mid, p_ulc[1])
114| p_dn = (x_mid, p_lrc[1])
115| p_lt = (p_ulc[0], y_mid)
116| p_rt = (p_lrc[0], y_mid)
117|
118| c_ulc = self.get(p_ulc)
119| c_urc = self.get(p_urc)
120| c_lrc = self.get(p_lrc)
121| c_llc = self.get(p_llc)
122|
123|
124| if p_ulc[0] < x_mid < p_lrc[0]:
125|
126| c_up = self.get(p_up)
127| if c_up < 0:
128| c_up = fAverage([c_ulc, c_urc]) + PlasmaCloud.fJitter(p_lrc[0] - p_ulc[0])
129| if self.top <= c_up: c_up = self.top;
130| if c_up < 0: c_up = 0
131| self.put(p_up, c_up)
132|
133| c_dn = self.get(p_dn)
134| if c_dn < 0:
135| c_dn = fAverage([c_lrc, c_llc]) + PlasmaCloud.fJitter(p_lrc[0] - p_ulc[0])
136| if self.top <= c_dn: c_dn = self.top;
137| if c_dn < 0: c_dn = 0
138| self.put(p_dn, c_dn)
139|
140|
141| if p_ulc[1] < y_mid < p_lrc[1]:
142|
143| c_rt = self.get(p_rt)
144| if c_rt < 0:
145| c_rt = fAverage([c_urc, c_lrc]) + PlasmaCloud.fJitter(p_lrc[1] - p_ulc[1])
146| if self.top <= c_rt: c_rt = self.top;
147| if c_rt < 0: c_rt = 0
148| self.put(p_rt, c_rt)
149|
150| c_lt = self.get(p_lt)
151| if c_lt < 0:
152| c_lt = fAverage([c_llc, c_ulc]) + PlasmaCloud.fJitter(p_lrc[1] - p_ulc[1])
153| if self.top <= c_lt: c_lt = self.top;
154| if c_lt < 0: c_lt = 0
155| self.put(p_lt, c_lt)
156|
157|
158| if (p_ulc[0] < x_mid < p_lrc[0]) and (p_ulc[1] < y_mid < p_lrc[1]):
159|
160| c_ct = fAverage([c_ulc,c_urc,c_lrc,c_llc]) + PlasmaCloud.fJitter(p_lrc[0] - p_ulc[0])
161| if self.top <= c_ct: c_ct = self.top;
162| if c_ct < 0: c_ct = 0
163| self.put(p_ct, c_ct)
164|
165|
166| if (1 < (x_mid - p_ulc[0])) or (1 < (y_mid - p_ulc[1])):
167| self._plot(p_ulc, p_ct , depth+1)
168| if (1 < (p_lrc[0] - x_mid)) or (1 < (y_mid - p_ulc[1])):
169| self._plot(p_up , p_rt , depth+1)
170| if (1 < (p_lrc[0] - x_mid)) or (1 < (p_lrc[1] - y_mid)):
171| self._plot(p_ct , p_lrc, depth+1)
172| if (1 < (x_mid - p_ulc[0])) or (1 < (p_lrc[1] - y_mid)):
173| self._plot(p_lt , p_dn , depth+1)
174|
175| def __len__ (self):
176| return len(self.data)
177|
178|
179|
180|
181|
182| def draw_colormap (im):
183| '''Draw the colormap.'''
184| py1 = im.dims[1]
185| py0 = py1 - 3
186| for ix,c in enumerate(ColorMap):
187| p0 = (ix, py0)
188| p1 = (ix, py1)
189| im.draw.line([p0, p1], fill=c)
190|
191|
192| def do_test (args=[]):
193| Log.info('test/parameters: %d' % len(args))
194| fname = args[0] if 0 < len(args) else path.join(ImgsPath,'color_test.png')
195| im = Img(ImageDims)
196| for y in range(im.dims[1]):
197| for x in range(im.dims[0]):
198|
199| xf = (float(x) / float(im.dims[0]))
200| xa = 360.0 * xf
201| xr = radians(xa)
202|
203| if y < 25:
204| c = (int(256.0 * sin(xr)), int(0), int(256.0 * sin(pi+xr)))
205| im.draw.point((x,y), fill=c)
206| continue
207| if y < 50:
208| c = (int(0), int(256.0 * cos(pi+xr)), int(0))
209| im.draw.point((x,y), fill=c)
210| continue
211| if y < 75:
212| c = (int(256.0 * sin(xr)), int(256.0 * cos(pi+xr)), int(256.0 * sin(pi+xr)))
213| im.draw.point((x,y), fill=c)
214| continue
215| if y < 100:
216| c = (int(256.0 * sin(xr)), int(256.0 * cos(xr)), int(256.0 * sin(3*xr)))
217| im.draw.point((x,y), fill=c)
218| continue
219| if y < 150:
220| im.draw.point((x,y), fill=fAngle2Color0(xa))
221| continue
222| if y < 200:
223| im.draw.point((x,y), fill=fAngle2Color1(xa))
224| continue
225| if y < 250:
226| im.draw.point((x,y), fill=fAngle2Color2(xa))
227| continue
228| if y < 300:
229| im.draw.point((x,y), fill=fAngle2Color3(xa))
230| continue
231| if y < 325:
232| im.draw.point((x,y), fill=fAngle2Gray1(xa))
233| continue
234| if y < 350:
235| im.draw.point((x,y), fill=fAngle2Gray2(xa))
236| continue
237| im.draw.point((x,y), fill=fAngle2Gray0(xa))
238| draw_colormap(im)
239|
240| im.save(fname)
241| im.show()
242| return 'Done.'
243|
244| def do_plasma1 (args=[]):
245| Log.info('plasma1/parameters: %d' % len(args))
246| fname = args[0] if 0 < len(args) else path.join(ImgsPath,'plasma-1.png')
247| im = Img(ImageDims)
248|
249| for y in range(im.dims[1]):
250| py = -0.5 + (float(y) / float(im.dims[1]))
251| for x in range(im.dims[0]):
252| px = -0.5 + (float(x) / float(im.dims[0]))
253|
254| v = float(-1)
255| v += cos(pi * 10 * px)
256| v += sin(pi * 10 * py)
257| v += sin(pi * 11 * (px+py))
258| v += cos(pi * sqrt(100 * ((px*px)+(py*py))))
259|
260| c = (int(0), int(255.0 * sin(pi*v)), int(255.0 * cos(pi*v)))
261|
262| im.draw.point((x,y), fill=c)
263|
264| im.save(fname)
265| im.show()
266| return 'Done.'
267|
268| def do_plasma2 (args=[]):
269| Log.info('plasma2/parameters: %d' % len(args))
270| fname = args[0] if 0 < len(args) else path.join(ImgsPath,'plasma-2.png')
271| im = Img(ImageDims)
272| for y in range(im.dims[1]):
273| ya = 360.0 * (float(y) / float(im.dims[1]))
274| for x in range(im.dims[0]):
275| xa = 360.0 * (float(x) / float(im.dims[0]))
276|
277| v = float(0)
278| v += 360.0 * sin(sqrt(100* (sin(radians(xa))**2 + sin(radians(ya))**2)))
279| v += 360.0 * cos(radians(5*xa))
280| v += 360.0 * sin(radians(7*ya))
281| im.draw.point((x,y), fill=fAngle2Color0(v))
282|
283| im.save(fname)
284| im.show()
285| return 'Done.'
286|
287| def do_plasma3 (args=[]):
288| Log.info('plasma3/parameters: %d' % len(args))
289| fname = args[0] if 0 < len(args) else path.join(ImgsPath,'plasma-3.png')
290| im = Img(ImageDims)
291|
292| obj = PlasmaCloud(im.dims, int(360))
293|
294| obj.copy_to_image(im, fAngle2Color2)
295|
296| im.save(fname)
297| im.show()
298| return 'Done.'
299|
300| def do_plasma4 (args=[]):
301| Log.info('plasma4/parameters: %d' % len(args))
302| fname = args[0] if 0 < len(args) else path.join(ImgsPath,'plasma-4.png')
303| param0 = int(15)
304| param1 = float(25)
305| param2 = float(13)
306| param3 = int(0)
307| param4 = float(9)
308|
309| fdrop = lambda: float(param1 - (((2.0*param1)+1.0) * random()))
310| ripples = [(fdrop(), fdrop()) for n in range(param0)]
311| Log.debug(ripples)
312|
313| im = Img(ImageDims)
314| for y in range(im.dims[1]):
315| py = -0.5 + (float(y) / float(im.dims[1]))
316| for x in range(im.dims[0]):
317| px = -0.5 + (float(x) / float(im.dims[0]))
318|
319| v = float(0)
320| for t in ripples:
321| tx = (px + t[0]) ** 2
322| ty = (py + t[1]) ** 2
323| v += param4 * sin(param2 * sqrt(param3 + tx + ty))
324| im.draw.point((x,y), fill=fAngle2Gray1(v))
325|
326| im.save(fname)
327| im.show()
328| return 'Done.'
329|
330| def do_plasma5 (args=[]):
331| Log.info('plasma5/parameters: %d' % len(args))
332| fname = args[0] if 0 < len(args) else path.join(ImgsPath,'plasma-5.png')
333| im = Img(ImageDims)
334| for y in range(im.dims[1]):
335| for x in range(im.dims[0]):
336|
337| v = 4.5 * ((1+int(x / 5)) * (1+int(y / 5)))
338| im.draw.point((x,y), fill=fAngle2Gray0(v))
339|
340| im.save(fname)
341| im.show()
342| return 'Done.'
343|
344| def do_plasma6 (args=[]):
345| Log.info('plasma6/parameters: %d' % len(args))
346| fname = args[0] if 0 < len(args) else path.join(ImgsPath,'plasma-6.png')
347| im = Img([200,200])
348| for y in range(0,200,2):
349| for x in range(0,200,2):
350|
351| v = 360 * random()
352| im.draw.point((x ,y ), fill=fAngle2Gray0(v))
353| im.draw.point((x ,y+1), fill=fAngle2Gray0(v))
354| im.draw.point((x+1,y+1), fill=fAngle2Gray0(v))
355| im.draw.point((x+1,y ), fill=fAngle2Gray0(v))
356|
357| im.save(fname)
358| im.show()
359| return 'Done.'
360|
361| def dispatch (cmd, args=[]):
362| Log.info('command: %s' % cmd)
363| Log.info('arguments: %d' % len(args))
364| if cmd == '1': return do_plasma1(args)
365| if cmd == '2': return do_plasma2(args)
366| if cmd == '3': return do_plasma3(args)
367| if cmd == '4': return do_plasma4(args)
368| if cmd == '5': return do_plasma5(args)
369| if cmd == '6': return do_plasma6(args)
370| if cmd == 'test': return do_test(args)
371| return 'Nothing to do.'
372|
373| if __name__ == '__main__':
374| Log.start(path.join(BasePath,'plasma.log'))
375| Log.level(trace())
376| Log.info('autorun: %s' % argv[0])
377| cmd = argv[1] if 1 < len(argv) else ''
378| etc = argv[2:]
379| try:
380| ColorMap = list(map(fAngle2Color1, list(range(360))))
381| Log.info(ColorMap)
382| Log.info()
383|
384| rtn = dispatch(cmd, etc)
385| Log.info(rtn)
386| except Exception as e:
387| Log.error(e)
388| raise
389| finally:
390| Log.end()
391|
392| '''eof'''
393|