Debian Codegraph @

src/dr/geo/color/ChannelColorScheme.java

raw symbols SCIP
1 /*
2 * ChannelColorScheme.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 import java.util.ArrayList;
30
31 /**
32 * Created by msuchard on 4/18/14.
33 */
34
35 public interface ChannelColorScheme {
36 Color getColor(java.util.List<Double> input, java.util.List<Double> min, java.util.List<Double> max);
37
38
39 static class MultipleChannelColorScheme implements ChannelColorScheme {
40 private final ColorScheme[] schemes;
41
42 MultipleChannelColorScheme(ColorScheme[] schemes) {
43 this.schemes = schemes;
44 }
45
46 public Color getColor(java.util.List<Double> input, java.util.List<Double> min, java.util.List<Double> max) {
47 java.util.List<Color> colors = new ArrayList<Color>();
48
49 final int channels = schemes.length; // assumes the same length as input, min, max
50 for (int i = 0; i < channels; ++i) {
51 colors.add(schemes[i].getColor(input.get(i), min.get(i), max.get(i)));
52 }
53
54 return blend(colors);
55 }
56
57 private static Color blend(java.util.List<Color> colors) {
58 double totalAlpha = 0.0;
59 for (Color color : colors) {
60 totalAlpha += color.getAlpha();
61 }
62
63 double r = 0.0;
64 double g = 0.0;
65 double b = 0.0;
66 double a = 0.0;
67 for (Color color : colors) {
68 double weight = color.getAlpha();
69 r += weight * color.getRed();
70 g += weight * color.getGreen();
71 b += weight * color.getBlue();
72 a = Math.max(a, weight);
73 }
74 r /= totalAlpha;
75 g /= totalAlpha;
76 b /= totalAlpha;
77
78 return new Color((int) r, (int) g, (int) b, (int) a);
79 }
80 }
81
82 public static final ChannelColorScheme CHANNEL_RED = new MultipleChannelColorScheme(
83 new ColorScheme[]{ColorScheme.WHITE_RED}
84 );
85
86 public static final ChannelColorScheme CHANNEL_RED_BLUE = new MultipleChannelColorScheme(
87 new ColorScheme[]{ColorScheme.WHITE_RED, ColorScheme.WHITE_BLUE}
88 );
89
90 }