| 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.geo.color; |
| 27 |
|
| 28 |
import java.awt.*; |
| 29 |
|
| 30 |
|
| 31 |
|
| 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; |
| 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 |
} |