-
Notifications
You must be signed in to change notification settings - Fork 0
/
duotone.pde
76 lines (52 loc) · 1.82 KB
/
duotone.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// duotone [with alpha values] - image processing
// david medina, 2015
color c1, c2;
int colorRange=256;
float[] alpha = new float[colorRange];
void setup() {
size(800, 600, P2D);
colorMode(RGB, 255);
background(255,255,255);
c1 = color(50, 0, 0, 255); //main
c2 = color(0, 0, 0, 10); //transparent
PImage pic= loadImage("http://grama.co/wp-content/uploads/2017/01/PP03-1024x768.jpg");
PImage bck=loadImage("http://grama.co/wp-content/uploads/2017/01/P1050132-768x512.jpg");
bck = duotone(bck, c1, c2);
image(bck, 0, 50);
c1 = color(0, 50, 0, 255); //main
c2 = color(0, 0, 0, 10); //transparent
pic = duotone(pic, c1, c2);
image(pic, 0, 0);
}
PImage duotone(PImage img, color color1, color color2) {
img.resize(width, 0);
img.filter(GRAY);
float a = (alpha(color2)-alpha(color1))/colorRange;
color gradient[] = new color[colorRange];
// Creates a gradient of 255 colors between color1 and color2 and alpha gradient
for (int d=0; d < colorRange-1; d++) {
float ratio= float(d)*1.0/colorRange;
gradient[d]=lerpColor(color1, color2, ratio);
alpha[d]=alpha(color1) + int(d*a);
}
color tc=0;
float br=0;
//auxiliar image for alpha pixels - mode ARGB
PImage aux2=createImage(img.width, img.height, ARGB);
aux2.loadPixels();
img.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
br = constrain(brightness(img.pixels[loc]),0,colorRange-1);
tc=gradient[int(br)];
color pix= color(red(tc), green(tc), blue(tc), alpha[int(br)]);
// Set the pixel to a gradient with the level of brightness
aux2.pixels[loc] = img.pixels[loc] = pix;
}
}
img.updatePixels();
aux2.updatePixels();
return(aux2);
}