Extracting Color Values

I edited this question since originally I was using the Hype framework to colorize stuff, but then found a perfect example of what I'm trying to do on the Processing site itself. This takes an image, builds out a grid just as I want, and colors things. I try printing "c" since I believe that to be the color but I'm not recognizing the format. Since it's in black and white, I'd love to see it in the range of 0 to 255.

1.) My goal is to print out the color that it's using to build the grid...just once.

Here's a snippet of the first few values I get:

6447715-3684409-2960686-3487030-2631721-3750202

I'm using this exact code from the website (https://processing.org/tutorials/pixels/) but I'll post the code here:

PImage img;       // The source image
int cellsize = 34; // Dimensions of each cell in the grid
int cols, rows;   // Number of columns and rows in our system

void setup() {
  size(900, 900, P3D); 
  img  = loadImage("black_and_white.jpg"); // Load the image
  cols = width/cellsize;             // Calculate # of columns
  rows = height/cellsize;            // Calculate # of rows
}

void draw() {
  background(0);
  loadPixels();
  // Begin loop for columns
  for ( int i = 0; i < cols;i++) {
    // Begin loop for rows
    for ( int j = 0; j < rows;j++) {
      int x = i*cellsize + cellsize/2; // x position
      int y = j*cellsize + cellsize/2; // y position
      int loc = x + y*width;           // Pixel array location
      color c = img.pixels[loc];       // Grab the color

      // Calculate a z position as a function of mouseX and pixel brightness
      float z = (mouseX/(float)width) * brightness(img.pixels[loc]) - 200.0;
      // Translate to the location, set fill and stroke, and draw the rect
      pushMatrix();
      translate(x,y,z);
      fill(c);
      noStroke();
      rectMode(CENTER);
      rect(0,0,cellsize,cellsize);
      print(c);
      popMatrix();
    }
  }
}
Tagged:

Answers

Sign In or Register to comment.