-
Notifications
You must be signed in to change notification settings - Fork 39
/
LastFMColorPicker.java
53 lines (42 loc) · 1.48 KB
/
LastFMColorPicker.java
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
import processing.core.*;
/**
* LastFMColorPicker
* Loads in an image and uses it as a two-dimensional gradient
* Supply two [0,1) numbers and get the color of the gradient at that point
*
* @author Lee Byron
* @author Martin Wattenberg
*/
public class LastFMColorPicker implements ColorPicker {
public PImage source;
public LastFMColorPicker(PApplet parent, String src) {
source = parent.loadImage(src);
}
public String getName() {
return "Listening History Color Scheme";
}
public void colorize(Layer[] layers) {
// find the largest layer to use as a normalizer
float maxSum = 0;
for (int i=0; i<layers.length; i++) {
maxSum = (float) Math.max(maxSum, layers[i].sum);
}
// find the color for each layer
for (int i = 0; i < layers.length; i++) {
float normalizedOnset = (float)layers[i].onset / layers[i].size.length;
float normalizedSum = layers[i].sum / maxSum;
float shapedSum = (float)(1.0 - Math.sqrt(normalizedSum));
layers[i].rgb = get(normalizedOnset, shapedSum);
}
}
protected int get(float g1, float g2) {
// get pixel coordinate based on provided parameters
int x = PApplet.floor(g1 * source.width);
int y = PApplet.floor(g2 * source.height);
// ensure that the pixel is within bounds.
x = PApplet.constrain(x, 0, source.width - 1);
y = PApplet.constrain(y, 0, source.height - 1);
// return the color at the requested pixel
return source.pixels[x + y * source.width];
}
}