plasma-orig.html
001| '''\
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| December 2013
001| '''
002|
003| from sys import argv, stdout, stderr, path as syspath
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 colors import *
009| from img import Img, Chart, XYChart, fBox, tAdd, tSub, dispatch
010|
011| Log = stdout
012|
013| ImageDims = (400, 400)
014| Jitter = 40
015|
016| fRed = lambda a: int(127 + (127.0 * cos(radians(a))))
017| fGreen = lambda a: int(127 + (127.0 * sin(radians(a*2.0))))
018| fBlue = lambda a: int(127 + (127.0 * cos(radians(a*2.0))))
019|
020| fAngle2Color = lambda a: (fRed(a), fGreen(a), fBlue(a))
021|
022| fCenter = lambda cs: int(float(sum(cs)) / float(len(cs)))
023|
024| fPlusMinus = lambda r: int(r - ((2*r) * random()))
025| fJitter = lambda v: fPlusMinus(min(v,Jitter))
026|
027| colormap = []
028|
029|
030|
031| def Test2 (im):
032| '''Fractint Plasma Cloud.'''
033|
034| (ulc,lrc,corners) = plasma_start(im)
035|
036|
037| plasma_plot(im, ulc, lrc, corners, 0)
038|
039|
040| draw_colormap(im)
041|
042| im.save('plasma-2.png')
043|
044|
045|
046| def plasma_plot (im, p_ulc, p_lrc, c_corners, depth):
047| '''\
047| Plasma Plot (Recursive). Given the upper-left and lower-right corner
047| coordinates, and color in all four corners, quarter the space and
047| generate the five new color points. Then call this function for each
047| of the quarters.
047| '''
048|
049| c_up = (fCenter([c_corners[0], c_corners[1]]) + fJitter(p_lrc[0] - p_ulc[0])) % 360
050| c_rt = (fCenter([c_corners[1], c_corners[2]]) + fJitter(p_lrc[1] - p_ulc[1])) % 360
051| c_dn = (fCenter([c_corners[2], c_corners[3]]) + fJitter(p_lrc[0] - p_ulc[0])) % 360
052| c_lt = (fCenter([c_corners[3], c_corners[0]]) + fJitter(p_lrc[1] - p_ulc[1])) % 360
053| c_ct = (fCenter(c_corners) + fJitter(p_lrc[0] - p_ulc[0])) % 360
054|
055|
056| tx = fCenter([p_ulc[0], p_lrc[0]])
057| ty = fCenter([p_ulc[1], p_lrc[1]])
058|
059|
060| p_lt = (p_ulc[0], ty)
061| p_rt = (p_lrc[0], ty)
062| p_up = (tx, p_ulc[1])
063| p_dn = (tx, p_lrc[1])
064| p_ct = (tx, ty)
065|
066|
067| if p_ulc[0] < tx < p_lrc[0]:
068| im.draw.point(p_up, fill=colormap[c_up])
069| im.draw.point(p_dn, fill=colormap[c_dn])
070|
071| if p_ulc[1] < ty < p_lrc[1]:
072| im.draw.point(p_rt, fill=colormap[c_rt])
073| im.draw.point(p_lt, fill=colormap[c_lt])
074|
075| if (p_ulc[0] < tx < p_lrc[0]) and (p_ulc[1] < ty < p_lrc[1]):
076| im.draw.point(p_ct, fill=colormap[c_ct])
077|
078|
079| if (1 < (tx - p_ulc[0])) or (1 < (ty - p_ulc[1])):
080| plasma_plot(im, p_ulc, p_ct , [c_corners[0], c_up, c_ct, c_lt], depth+1)
081| if (1 < (p_lrc[0] - tx)) or (1 < (ty - p_ulc[1])):
082| plasma_plot(im, p_up , p_rt , [c_up, c_corners[1], c_rt, c_ct], depth+1)
083| if (1 < (p_lrc[0] - tx)) or (1 < (p_lrc[1] - ty)):
084| plasma_plot(im, p_ct , p_lrc, [c_ct, c_rt, c_corners[2], c_dn], depth+1)
085| if (1 < (tx - p_ulc[0])) or (1 < (p_lrc[1] - ty)):
086| plasma_plot(im, p_lt , p_dn , [c_lt, c_ct, c_dn, c_corners[3]], depth+1)
087|
088|
089|
090| def plasma_start (im):
091| '''\
091| Initialize the Plasma Plot. Generate the first four corners and colors
091| and plot them. Return the ULC and LRC corner points and a list of the
091| four corner colors in a three-ple.
091| '''
092|
093| p_ulc = ( 0 , 0 )
094| p_urc = ( 0 , ImageDims[1])
095| p_lrc = (ImageDims[0], ImageDims[1])
096| p_llc = (ImageDims[0], 0 )
097|
098|
099| c_ulc = int(random() * 360)
100| c_urc = int(random() * 360)
101| c_lrc = int(random() * 360)
102| c_llc = int(random() * 360)
103|
104|
105| im.draw.point(p_ulc, fill=colormap[c_ulc])
106| im.draw.point(p_urc, fill=colormap[c_urc])
107| im.draw.point(p_lrc, fill=colormap[c_lrc])
108| im.draw.point(p_llc, fill=colormap[c_llc])
109|
110|
111| return (p_ulc, p_lrc, (c_ulc, c_urc, c_lrc, c_llc))
112|
113|
114|
115|
116| def draw_colormap (im):
117| '''Draw the colormap.'''
118| py1 = ImageDims[1]
119| py0 = py1 - 3
120| for ix,c in enumerate(colormap):
121| p0 = (ix, py0)
122| p1 = (ix, py1)
123| im.draw.line([p0, p1], fill=c)
124|
125|
126|
127| def main (*args):
128| Log = open('plasma.log', mode='w', encoding='utf-8')
129|
130| print(datetime.now(), file=Log)
131| print(file=Log)
132|
133| colormap = list(map(fAngle2Color, list(range(360))))
134| print(colormap, file=Log)
135| print(file=Log)
136|
137| try:
138|
139| im = Img(ImageDims)
140| Test2(im)
141| im.show()
142|
143| except:
144| raise
145| finally:
146| Log.close()
147|
148|
149| if __name__ == '__main__':
150| print(argv[0])
151| print()
152| main(*argv[1:])
153| print()
154|
155| '''eof'''
156|