allColors2.frink

Download or view allColors2.frink in plain text format


/** program to make an image with each pixel in a different color.  Now what?
*/


stepsize = 4

colors = new array
multifor color = [new range[0, 255, stepsize], new range[0, 255, stepsize], new range[0, 255, stepsize]]
{
   colors.push[color]
}

colors.shuffle[]

side = ceil[sqrt[length[colors]]]
println["side is $side"]
img = new image[side, side]

//dirs = [[-1,0], [0, -1]]
//dirs = [[-1,0], [1,0], [0, -1], [0,1]]
dirs = [[-1,0], [1,0], [0, -1], [0,1], [-1,-1], [1,1], [-1,1], [1,-1]]

idx = 0
for [r,g,b] = colors
{
   img.setPixelInt[idx div side, idx mod side, r,g,b]
   idx = idx + 1
}

img.show[]
img.write["allColors2.png"]

g = new graphics
win = g.show[]

for t = 0 to 40 side
{
   s = (t mod 2) + 1
   for y = 0 to side-1 step s
   {
      for x=0 to side-1 step s
      {
         [r1,g1,b1] = img.getPixelInt[x,y]
         bestd = undef
         bestdiff = million
         for d = dirs
         {
            [dx, dy] = mul[d, s]
            [r2,g2,b2] = img.getPixelInt[(x+dx) mod side, (y+dy) mod side]
            diff = (r1-r2)^2 + (g1-g2)^2 + (b1-b2)^2
            if diff < bestdiff
            {
               bestd = d
               bestdiff = diff
            }
         }

         [dx, dy] = mul[bestd, s]
         x1 = (x + dx) mod side
         y1 = (y + dy) mod side
         
         temp = img.getPixelAsColor[x,y]
         img.setPixel[x,y,img.getPixelAsColor[x1,y1]]
         img.setPixel[x1,y1, temp]
      }
   }
   g = new graphics
   g.antialiased[false]
   g.add[img]
   win.replaceGraphics[g]
}

img.show[]
img.write["allColors2.png"]

//img2 = img.gaussianBlur[3]
//img2.show[]



Download or view allColors2.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 20293 days, 4 hours, 44 minutes ago.