get paid to paste

c64 palette swap

// Processing code to 

PImage test;

// C64 16-color palette converted to hex
int[] colors = {0x000000, 0xFFFFFF, 0x68372B, 0x70A4B2, 0x6F3D86, 
                0x588D43, 0x352979, 0xB8C76F, 0x6F4F25, 0x433900,
                0x9A6759, 0x444444, 0x6C6C6C, 0x9AD284, 0x6C5EB5,
                0x959595};
                

void setup() {
  size(720, 478);
  test = loadImage("test.jpg");

}


void draw() {
    test.loadPixels();
    image(test, width/2, 0);
    drawRects();
}

int compare( int colour1, int colour2 ) 
{

  int currR = (color1 >> 16) & 0xFF; 
  int currG = (color1 >> 8) & 0xFF;
  int currB = color1 & 0xFF;

  int currR2 = (color2 >> 16) & 0xFF; 
  int currG2 = (color2 >> 8) & 0xFF;
  int currB2 = colour2 & 0xFF;

  int distance  = 0;
  distance += Math.pow(currR - currR2, 2);
  distance += Math.pow(currG - currG2, 2);
  distance += Math.pow(currB - currB2, 2);
  return distance ;
}


int  getClosestColor(color color1)  {

  int colorTo = 0;
  int oldDist = 0;

  for ( int i = 0;i < 15; i++ ) {
    int colorTarget  = colors[i];
    int distance  = compare(color1, colorTarget);
    if ( i == 0 ) {
      oldDist = distance;
      colorTo = colorTarget;
    } 
    if ( distance < oldDist ) {
      colorTo = colorTarget;
      oldDist = distance;
    }

  }
  return colorTo;
}


void drawRects()  {

  
  int res = 1;

  for (int  i  = 0;i < 320; i = i + res) {
    for (int j  = 0;j < height; j = j + res) {


      color currColor =  test.pixels[j*test.width + i];
      currColor = getClosestColor(currColor);

      int currR = (currColor >> 16) & 0xFF; 
      int currG = (currColor >> 8) & 0xFF;
      int currB = currColor & 0xFF;
      
      color mapFill = color(currR, currG, currB);
      strokeWeight(1);
      stroke(mapFill);
     
      fill(currR, currG, currB);
      rect(i, j, res, res);
    }
  }
}

Pasted: Nov 30, 2011, 3:15:29 pm
Views: 6