Debian Codegraph @

src/dr/geo/color/ColorScheme.java

raw symbols SCIP
1 /*
2 * ColorScheme.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.geo.color;
27
28 import java.awt.*;
29
30 /**
31 * Created by msuchard on 4/18/14.
32 */
33 public interface ColorScheme {
34
35 Color getColor(double input, double min, double max);
36
37 public static final ColorScheme HEATMAP = new ColorScheme() {
38 double getRampValue(double input, double min, double max) {
39 double end = 1.0 / 6.0;
40 double start = 0.0;
41 return (input - min) / (max - min) * (end - start);
42 }
43
44 public Color getColor(double input, double min, double max) {
45 float hue = (float) getRampValue(input, min, max);
46 float saturation = 0.85f;
47 float alpha = 1.0f;
48 return Color.getHSBColor(hue, saturation, alpha);
49 }
50 };
51
52 interface Ramp {
53 int mix(double ratio, int c1, int c2);
54 }
55
56 public static final Ramp LINEAR = new Ramp() {
57 public int mix(double ratio, int c1, int c2) {
58 return (int) (c1 * ratio + c2 * (1.0 - ratio));
59 }
60 };
61
62 abstract class RampedColorScheme implements ColorScheme {
63
64 protected abstract Color getMaxColor();
65
66 protected abstract Color getMinColor();
67
68 private final Ramp ramp;
69
70 private RampedColorScheme(Ramp ramp) {
71 this.ramp = ramp;
72 }
73
74 public Color getColor(double input, double min, double max) {
75 double value = (input - min) / (max - min);
76 int red = ramp.mix(value, getMaxColor().getRed(), getMinColor().getRed());
77 int green = ramp.mix(value, getMaxColor().getGreen(), getMinColor().getGreen());
78 int blue = ramp.mix(value, getMaxColor().getBlue(), getMinColor().getBlue());
79 return new Color(red, green, blue);
80 }
81 }
82
83 abstract class TransparentColorScheme implements ColorScheme {
84
85 TransparentColorScheme(ColorScheme scheme) {
86 this.scheme = scheme;
87 }
88
89 protected abstract double getTransparentValue();
90
91 protected Color getBaseTransparentColor() {
92 return Color.WHITE;
93 }
94
95 public Color getColor(double input, double min, double max) {
96 if (input == getTransparentValue()) {
97 int rgb = getBaseTransparentColor().getRGB();
98 rgb = 0x00FFFFFF & rgb; // make complete transparent
99 return new Color(rgb, true);
100 } else {
101 return scheme.getColor(input, min, max);
102 }
103 }
104
105 private final ColorScheme scheme;
106 }
107
108 public static final ColorScheme WHITE_RED = new RampedColorScheme(LINEAR) {
109 protected Color getMaxColor() {
110 return Color.RED;
111 }
112
113 protected Color getMinColor() {
114 return Color.WHITE;
115 }
116 };
117
118 public static final ColorScheme WHITE_BLUE = new RampedColorScheme(LINEAR) {
119 protected Color getMaxColor() {
120 return Color.BLUE;
121 }
122
123 protected Color getMinColor() {
124 return Color.WHITE;
125 }
126 };
127
128 public static final ColorScheme TRANPARENT_WHITE_RED = new TransparentColorScheme(WHITE_RED) {
129
130 protected double getTransparentValue() {
131 return Double.NaN;
132 }
133 };
134
135 public static final ColorScheme TRANPARENT_HEATMAP = new TransparentColorScheme(HEATMAP) {
136
137 protected double getTransparentValue() {
138 return Double.NaN;
139 }
140 };
141
142 public static final ColorScheme TRANPARENT0_HEATMAP = new TransparentColorScheme(HEATMAP) {
143
144 protected double getTransparentValue() {
145 return 0.0;
146 }
147 };
148
149 public static final ColorScheme TRANPARENT_HEATMAP2 = new TransparentColorScheme(
150 new RampedColorScheme(LINEAR) {
151
152 protected Color getMaxColor() {
153 return Color.YELLOW;
154 }
155
156 protected Color getMinColor() {
157 return Color.RED;
158 }
159 }
160 ) {
161
162 protected double getTransparentValue() {
163 return Double.NaN;
164 }
165 };
166 }