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.
IndexProcessing DevelopmentLibraries,  Tool Development › color gradient change support for rasters
Page Index Toggle Pages: 1
color gradient change support for rasters? (Read 1362 times)
color gradient change support for rasters?
Jan 12th, 2009, 9:52am
 
Hi, I am new to processing, I am trying to figure out which libraries of processing i should look into to accomplish this short set of tasks:

- have an image that uses colors between dark purple to pink
- read in the pixel colors data of a raster image
- change the image such that dark purple becomes red and pink becomes green, and all the other in between colors change accordingly and export the image.  In other words i would like to change the gradient color scheme of an image

While im sure this is likely possible, just would like to get a feel as to if this is an involved task at all - im not sure if processing is the best tool for this.

Thanks for any help
Re: color gradient change support for rasters?
Reply #1 - Jan 12th, 2009, 2:31pm
 
Is it the best tool? Not sure... But it can do it.
I found the idea classical (basically that's palette substitution) but interesting to implement.
Your description left some room for interpretation... I chose to read "brightness drives color change".
My implementation can be seen as a framework, leaving room for experimentations.
Code:
PImage testImage;
int minHue = 256, maxHue = 0;
int minSat = 256, maxSat = 0;
int minBri = 256, maxBri = 0;

void setup()
{
 size(600, 400);
 smooth();
 noLoop();
 colorMode(HSB);

 testImage = loadImage("Image_in_red.png");

 testImage.loadPixels();
 analyzeImage(testImage);

 background(230);

 image(testImage, 0, 0);

 shiftColors(testImage);
 testImage.updatePixels();

 image(testImage, testImage.width, 0);
}

void analyzeImage(PImage someImage)
{
 // Pre-condition: loadPixels have been done
 for (int i = 0; i < someImage.pixels.length; i++)
 {
   color c = someImage.pixels[i];

   int hue = int(hue(c));
   minHue = min(minHue, hue);
   maxHue = max(maxHue, hue);

   int saturation= int(saturation(c));
   minSat = min(minSat, saturation);
   maxSat = max(maxSat, saturation);

   int brightness= int(brightness(c));
   minBri = min(minBri, brightness);
   maxBri = max(maxBri, brightness);
 }

 println("Hue - Min: " + minHue + ", Max: " + maxHue);
 println("Sat - Min: " + minSat + ", Max: " + maxSat);
 println("Bri - Min: " + minBri + ", Max: " + maxBri);
}

void shiftColors(PImage someImage)
{
 // Pre-condition: loadPixels have been done
 for (int i = 0; i < someImage.pixels.length; i++)
 {
   color oc = someImage.pixels[i];  // Eliminate alpha
   int hue = int(map(brightness(oc), minBri, maxBri,
       0,  // hue = 0 => red
       85));  // 85 => green
   // Keep original saturation and brightness (can be mapped too)
   color nc = color(hue,
       saturation(oc),
       brightness(oc));
   someImage.pixels[i] = nc;
 }
}
Page Index Toggle Pages: 1