| 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 |
package dr.app.gui; |
| 27 |
|
| 28 |
import java.awt.*; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class ColorFunction { |
| 34 |
|
| 35 |
Color[] colors; |
| 36 |
float[] points; |
| 37 |
|
| 38 |
float[] rgba1 = new float[4]; |
| 39 |
float[] rgba2 = new float[4]; |
| 40 |
|
| 41 |
|
| 42 |
public ColorFunction(Color[] colors, float[] points) { |
| 43 |
this.colors = colors; |
| 44 |
this.points = points; |
| 45 |
|
| 46 |
if (points.length != colors.length) throw new IllegalArgumentException(); |
| 47 |
|
| 48 |
|
| 49 |
for (int i = 0; i < points.length - 1; i++) { |
| 50 |
if (points[i + 1] < points[i]) { |
| 51 |
throw new IllegalArgumentException(); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public Color getColor(float I) { |
| 57 |
|
| 58 |
for (int i = 0; i < points.length - 1; i++) { |
| 59 |
if (I >= points[i] && I <= points[i + 1]) { |
| 60 |
return interpolate(colors[i], colors[i + 1], I - points[i], points[i + 1] - points[i]); |
| 61 |
} |
| 62 |
} |
| 63 |
return Color.BLACK; |
| 64 |
} |
| 65 |
|
| 66 |
private Color interpolate(Color x, Color y, float s, float t) { |
| 67 |
|
| 68 |
rgba1 = x.getRGBComponents(rgba1); |
| 69 |
rgba2 = y.getRGBComponents(rgba2); |
| 70 |
float[] rgba3 = new float[4]; |
| 71 |
|
| 72 |
for (int i = 0; i < rgba1.length; i++) { |
| 73 |
rgba3[i] = (s * rgba2[i] + (t - s) * rgba1[i]) / t; |
| 74 |
} |
| 75 |
|
| 76 |
return new Color(rgba3[0], rgba3[1], rgba3[2], rgba3[3]); |
| 77 |
} |
| 78 |
|
| 79 |
public void setAlpha(float alpha) { |
| 80 |
|
| 81 |
for (int i = 0; i < colors.length; i++) { |
| 82 |
float[] rgba = colors[i].getRGBComponents(rgba1); |
| 83 |
colors[i] = new Color(rgba[0], rgba[1], rgba[2], alpha); |
| 84 |
} |
| 85 |
} |
| 86 |
} |