Duotone Image Processing

edited December 2016 in Questions about Code

I'm making this function for converting images to duotone. I'm close but I can't get the gradient (from color1 to color2 right). Also I would like to do this from color1 to transparent/alpha 0, if somebody has any idea. Thanks a LOT for any help!

A related script: http://codepen.io/72lions/pen/jPzLJX

// duotone script

color c1, c2;

void setup() {  
  size(800, 800, P2D);
  colorMode(HSB, 255);

  c1 = color(240, 14, 46);
  c2 = color(25, 37, 80);

  PImage pic=loadImage("<a href="http://jquery-custom-scrollbar.rocketmind.pl/images/lena.png" target="_blank" rel="nofollow">http://jquery-custom-scrollbar.rocketmind.pl/images/lena.png</a>;");

  image(duotone(pic, c1, c2), 0, 0);
}


PImage duotone(PImage img, color color1, color color2) {

  img.resize(width, 0);
  img.filter(GRAY);

  color gradient[] = new color[256];

  // Creates a gradient of 255 colors between color1 and color2
  for (int d=0; d < 255; d++) {    
    float ratio= float(d)*0.00392156862745;
    gradient[d]=lerpColor(color1,color2,ratio);
  }

  loadPixels();
  for (int y = 0; y < img.height; y++) {
    for (int x = 0; x < img.width; x++) {
      int loc = x + y*img.width;

      // get the brightness
      float br = brightness(img.pixels[loc]);

      // The luminosity from 0 to 255 --this is necessary?
      float luminosity = max(0, min(254, round((br * 254))));

      // Set the pixel to a gradient with the level of brightness
      img.pixels[loc] = gradient[int(br)];   

      //img.pixels[loc] = gradient[int(luminosity)];  // this won't work
    }
  }

  updatePixels();  
  return(img);
}

Answers

This discussion has been closed.