function dist2(a, b, c, d) { var dx = c-a, dy = d-b; return dx*dx + dy*dy; } function cos2(x) { var c = Math.cos(x); return c*c; } var off1 = 0; var m = Math.PI*32; var off2 = .25; return function(x, y) { function wave(a, b) { return cos2(m*dist2(x, y, a, b)); } var r1 = wave(0, 0); var r2 = wave(1, 1); var r3 = wave(off2, 1-off2); var r4 = wave(1-off2, off2); var g1 = wave(.5, off1); var g2 = wave(.5, 1 - off1); var g3 = wave(off1, .5); var g4 = wave(1 - off1, .5); var b1 = wave(0, 1); var b2 = wave(1, 0); var b3 = wave(off2, off2); var b4 = wave(1-off2, 1-off2); return [ .25*(r1+r2+r3+r4), .25*(g1+g2+g3+g4), .25*(b1+b2+b3+b4) ]; };