Debian Codegraph @

src/dr/app/gui/ColorFunction.java

raw symbols SCIP
1 /*
2 * ColorFunction.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.gui;
27
28 import java.awt.*;
29
30 /**
31 * @author Alexei Drummond
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 //if (points[0] != 0.0) throw new IllegalArgumentException();
48 //if (points[points.length - 1] != 1.0) throw new IllegalArgumentException();
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 }