We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How to convert colors to 8-bit
Page Index Toggle Pages: 1
How to convert colors to 8-bit (Read 339 times)
How to convert colors to 8-bit
Feb 22nd, 2009, 10:48am
 
I'm trying to convert the colors of whatever's captured by the camera into simplified 8-bit colors, so the end result would look similar to that of early Nintendo games. Could somehow help me out with the color-codes here?



//import processing.video.*;
//
//// Size of each cell in the grid
//int cellSize = 50;
//// Number of columns and rows in our system
//int cols, rows;
//// Variable for capture device
//Capture video;
//
//
//void setup() {
//  size(1024, 768, P2D);
//  frameRate(30);
//  cols = width / cellSize;
//  rows = height / cellSize;
//  colorMode(RGB, 255, 255, 255, 100);
//
//  // Uses the default video input, see the reference if this causes an error
//  video = new Capture(this, width, height, 12);
//  
//  background(0);
//}
//
//
//void draw() {
//  if (video.available()) {
//    video.read();
//    video.loadPixels();
//    
//    // Not bothering to clear background
//    // background(0);
//  
//    // Begin loop for columns
//    for (int i = 0; i < cols; i++) {
//      // Begin loop for rows
//      for (int j = 0; j < rows; j++) {
//      
//        // Where are we, pixel-wise?
//        int x = i*cellSize;
//        int y = j*cellSize;
//        int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image
//      
//        float r = red(video.pixels[loc]);
//        float g = green(video.pixels[loc]);
//        float b = blue(video.pixels[loc]);
//        // Make a new color with an alpha component
//        color c = color(r, g, b, 75);
//      
//        // Code for drawing a single rect
//        // Using translate in order for rotation to work properly
//        pushMatrix();
//        translate(x+cellSize/2, y+cellSize/2);
//        // Rotation formula based on brightness
//        rotate((0 * PI * brightness(c) / 255.0));
//        rectMode(CENTER);
//        fill(c);
//        noStroke();
//        // Rects are larger than the cell for some overlap
//        rect(0, 0, cellSize+6, cellSize+6);
//        popMatrix();
//      }
//    }
//  }
//}

Re: How to convert colors to 8-bit
Reply #1 - Feb 22nd, 2009, 7:53pm
 
the fancy name for this is quantisation

http://en.wikipedia.org/wiki/Color_quantization

and it can be quite tricky if you are adapting the palette to match the image and then mapping the image to the generated palette.

but generally it's a case of finding the nearest colour in the palette to the one in the image and using that.

i'd be tempted to use the 216 colour websafe palette and then just round each r, g and b value in each pixel in turn to a multiple of 51 (#33)

that said, even a 216 colour palette may be too precise to get you videogame style graphics.

http://en.wikipedia.org/wiki/List_of_palettes

um, we've had this question before. is it homework? 8)
Re: How to convert colors to 8-bit
Reply #2 - Feb 22nd, 2009, 7:58pm
 
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1223399852
Page Index Toggle Pages: 1