Ignore:
Timestamp:
2010-09-15T18:53:09+02:00 (15 years ago)
Author:
stoecker
Message:

remove tabs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/visualisation/FloatPropertyColorScheme.java

    r18130 r23189  
    2222public class FloatPropertyColorScheme implements ColorScheme {
    2323
    24         private final Class<? extends RoadPropertyType<Float>> propertyClass;
    25         private final Map<Float, Color> colorMap;
    26         private final Color defaultColor;
     24    private final Class<? extends RoadPropertyType<Float>> propertyClass;
     25    private final Map<Float, Color> colorMap;
     26    private final Color defaultColor;
    2727
    28         /**
    29         * @param propertyClass  property type to get values for; != null
    30         * @param colorMap       map from some values to colors.
    31         *                       Colors for all other values are interpolated.
    32         *                       This map will be copied and not used directly later. != null
    33         * @param defaultColor   color that is used when the property is not available; != null
    34         */
    35         public FloatPropertyColorScheme(Class<? extends RoadPropertyType<Float>> propertyClass,
    36                         Map<Float, Color> colorMap, Color defaultColor) {
    37                 assert propertyClass != null && colorMap != null && defaultColor != null;
     28    /**
     29    * @param propertyClass  property type to get values for; != null
     30    * @param colorMap       map from some values to colors.
     31    *                       Colors for all other values are interpolated.
     32    *                       This map will be copied and not used directly later. != null
     33    * @param defaultColor   color that is used when the property is not available; != null
     34    */
     35    public FloatPropertyColorScheme(Class<? extends RoadPropertyType<Float>> propertyClass,
     36            Map<Float, Color> colorMap, Color defaultColor) {
     37        assert propertyClass != null && colorMap != null && defaultColor != null;
    3838
    39                 this.propertyClass = propertyClass;
    40                 this.colorMap = new HashMap<Float, Color>(colorMap);
    41                 this.defaultColor = defaultColor;
    42         }
     39        this.propertyClass = propertyClass;
     40        this.colorMap = new HashMap<Float, Color>(colorMap);
     41        this.defaultColor = defaultColor;
     42    }
    4343
    44         public Color getSegmentColor(Segment segment) {
    45                 assert segment != null;
     44    public Color getSegmentColor(Segment segment) {
     45        assert segment != null;
    4646
    47                 Float propertyValue = null;
    48                 Collection<RoadPropertyType<?>> availableProperties = segment.getAvailableProperties();
    49                 for (RoadPropertyType<?> property : availableProperties) {
    50                         if (propertyClass.isInstance(property)) {
    51                                 @SuppressWarnings("unchecked") //has been checked using isInstance
    52                                 RoadPropertyType<Float> floatProperty = (RoadPropertyType<Float>)property;
    53                                 propertyValue = segment.getPropertyValue(floatProperty);
    54                                 break;
    55                         }
    56                 }
     47        Float propertyValue = null;
     48        Collection<RoadPropertyType<?>> availableProperties = segment.getAvailableProperties();
     49        for (RoadPropertyType<?> property : availableProperties) {
     50            if (propertyClass.isInstance(property)) {
     51                @SuppressWarnings("unchecked") //has been checked using isInstance
     52                RoadPropertyType<Float> floatProperty = (RoadPropertyType<Float>)property;
     53                propertyValue = segment.getPropertyValue(floatProperty);
     54                break;
     55            }
     56        }
    5757
    58                 if (propertyValue != null) {
    59                         return getColorForValue(propertyValue);
    60                 } else {
    61                         return defaultColor;
    62                 }
    63         }
     58        if (propertyValue != null) {
     59            return getColorForValue(propertyValue);
     60        } else {
     61            return defaultColor;
     62        }
     63    }
    6464
    65         public Color getNodeColor(GraphNode node) {
     65    public Color getNodeColor(GraphNode node) {
    6666
    67                 List<Color> segmentColors = new ArrayList<Color>();
     67        List<Color> segmentColors = new ArrayList<Color>();
    6868
    69                
    70                
    71                 for (GraphEdge edge : node.getInboundEdges()) {
    72                         List<Segment> edgeSegments = edge.getPropertyValue(GraphEdgeSegments.PROPERTY);
    73                         if (edgeSegments.size() > 0) {
    74                                 Segment firstSegment = edgeSegments.get(0);
    75                                 segmentColors.add(getSegmentColor(firstSegment));
    76                         }
    77                 }
    78                 for (GraphEdge edge : node.getOutboundEdges()) {
    79                         List<Segment> edgeSegments = edge.getPropertyValue(GraphEdgeSegments.PROPERTY);
    80                         if (edgeSegments.size() > 0) {
    81                                 Segment lastSegment = edgeSegments.get(edgeSegments.size()-1);
    82                                 segmentColors.add(getSegmentColor(lastSegment));
    83                         }
    84                 }
    8569
    86                 if (segmentColors.size() > 0) {
    87                         return averageColor(segmentColors);
    88                 } else {
    89                         return Color.WHITE;
    90                 }
    9170
    92         }
     71        for (GraphEdge edge : node.getInboundEdges()) {
     72            List<Segment> edgeSegments = edge.getPropertyValue(GraphEdgeSegments.PROPERTY);
     73            if (edgeSegments.size() > 0) {
     74                Segment firstSegment = edgeSegments.get(0);
     75                segmentColors.add(getSegmentColor(firstSegment));
     76            }
     77        }
     78        for (GraphEdge edge : node.getOutboundEdges()) {
     79            List<Segment> edgeSegments = edge.getPropertyValue(GraphEdgeSegments.PROPERTY);
     80            if (edgeSegments.size() > 0) {
     81                Segment lastSegment = edgeSegments.get(edgeSegments.size()-1);
     82                segmentColors.add(getSegmentColor(lastSegment));
     83            }
     84        }
    9385
    94         /**
    95          * returns the color for a value
    96          * @param value  value to get color for; != null
    97          * @return       color; != null
    98          */
    99         protected Color getColorForValue(Float value) {
    100                 assert value != null;
     86        if (segmentColors.size() > 0) {
     87            return averageColor(segmentColors);
     88        } else {
     89            return Color.WHITE;
     90        }
    10191
    102                 if (colorMap.containsKey(value)) {
     92    }
    10393
    104                         return colorMap.get(value);
     94    /**
     95     * returns the color for a value
     96     * @param value  value to get color for; != null
     97     * @return       color; != null
     98     */
     99    protected Color getColorForValue(Float value) {
     100        assert value != null;
    105101
    106                 } else {
     102        if (colorMap.containsKey(value)) {
    107103
    108                         LinkedList<Float> valuesWithDefinedColor = new LinkedList<Float>(colorMap.keySet());
    109                         Collections.sort(valuesWithDefinedColor);
     104            return colorMap.get(value);
    110105
    111                         if (value <= valuesWithDefinedColor.getFirst()) {
     106        } else {
    112107
    113                                 return colorMap.get(valuesWithDefinedColor.getFirst());
     108            LinkedList<Float> valuesWithDefinedColor = new LinkedList<Float>(colorMap.keySet());
     109            Collections.sort(valuesWithDefinedColor);
    114110
    115                         } else if (value >= valuesWithDefinedColor.getLast()) {
     111            if (value <= valuesWithDefinedColor.getFirst()) {
    116112
    117                                 return colorMap.get(valuesWithDefinedColor.getLast());
     113                return colorMap.get(valuesWithDefinedColor.getFirst());
    118114
    119                         } else {
     115            } else if (value >= valuesWithDefinedColor.getLast()) {
    120116
    121                                 /* interpolate */
     117                return colorMap.get(valuesWithDefinedColor.getLast());
    122118
    123                                 Float lowerValue = valuesWithDefinedColor.getFirst();
    124                                 Float higherValue = null;
     119            } else {
    125120
    126                                 for (Float v : valuesWithDefinedColor) {
    127                                         if (v >= value) {
    128                                                 higherValue = v;
    129                                                 break;
    130                                         }
    131                                         lowerValue = v;
    132                                 }
     121                /* interpolate */
    133122
    134                                 assert lowerValue != null && higherValue != null;
     123                Float lowerValue = valuesWithDefinedColor.getFirst();
     124                Float higherValue = null;
    135125
    136                                 Color lowerColor = colorMap.get(lowerValue);
    137                                 Color higherColor = colorMap.get(higherValue);
     126                for (Float v : valuesWithDefinedColor) {
     127                    if (v >= value) {
     128                        higherValue = v;
     129                        break;
     130                    }
     131                    lowerValue = v;
     132                }
    138133
    139                                 float weightHigherColor = (value - lowerValue) / (higherValue - lowerValue);
     134                assert lowerValue != null && higherValue != null;
    140135
    141                                 return weightedAverageColor(lowerColor, higherColor, weightHigherColor);
     136                Color lowerColor = colorMap.get(lowerValue);
     137                Color higherColor = colorMap.get(higherValue);
    142138
    143                         }
     139                float weightHigherColor = (value - lowerValue) / (higherValue - lowerValue);
    144140
    145                 }
     141                return weightedAverageColor(lowerColor, higherColor, weightHigherColor);
    146142
    147         }
     143            }
    148144
    149         /**
    150          * returns an average of all colors that have been passed as parameter
    151          *
    152          * @param colors  colors to calculate average from; not empty or null
    153          * @return        average color; != null
    154          */
    155         private static Color averageColor(List<Color> colors) {
    156                 assert colors != null && colors.size() > 0;
     145        }
    157146
    158                 float weightPerColor = 1.0f / colors.size();
     147    }
    159148
    160                 Color average = new Color(0,0,0);
     149    /**
     150     * returns an average of all colors that have been passed as parameter
     151     *
     152     * @param colors  colors to calculate average from; not empty or null
     153     * @return        average color; != null
     154     */
     155    private static Color averageColor(List<Color> colors) {
     156        assert colors != null && colors.size() > 0;
    161157
    162                 for (Color color : colors) {
    163                         average = new Color(
    164                                         Math.min(Math.round(average.getRed() + weightPerColor*color.getRed()), 255),
    165                                         Math.min(Math.round(average.getGreen() + weightPerColor*color.getGreen()), 255),
    166                                         Math.min(Math.round(average.getBlue() + weightPerColor*color.getBlue()), 255));
    167                 }
     158        float weightPerColor = 1.0f / colors.size();
    168159
    169                 return average;
    170         }
     160        Color average = new Color(0,0,0);
    171161
    172         /**
    173          * returns a weighted average of two colors
    174          *
    175          * @param color1        first color for the average; != null
    176          * @param color2        second color for the average; != null
    177          * @param weightColor2  weight of color2; must be in [0..1]
    178          * @return              average color; != null
    179          */
    180         private static Color weightedAverageColor(Color color1, Color color2, float weightColor2) {
    181                 assert color1 != null && color2 != null;
    182                 assert 0 <= weightColor2 && weightColor2 <= 1;
     162        for (Color color : colors) {
     163            average = new Color(
     164                    Math.min(Math.round(average.getRed() + weightPerColor*color.getRed()), 255),
     165                    Math.min(Math.round(average.getGreen() + weightPerColor*color.getGreen()), 255),
     166                    Math.min(Math.round(average.getBlue() + weightPerColor*color.getBlue()), 255));
     167        }
    183168
    184                 return new Color(
    185                                 Math.round((1 - weightColor2) * color1.getRed() + weightColor2 * color2.getRed()),
    186                                 Math.round((1 - weightColor2) * color1.getGreen() + weightColor2 * color2.getGreen()),
    187                                 Math.round((1 - weightColor2) * color1.getBlue() + weightColor2 * color2.getBlue()));
    188         }
     169        return average;
     170    }
     171
     172    /**
     173     * returns a weighted average of two colors
     174     *
     175     * @param color1        first color for the average; != null
     176     * @param color2        second color for the average; != null
     177     * @param weightColor2  weight of color2; must be in [0..1]
     178     * @return              average color; != null
     179     */
     180    private static Color weightedAverageColor(Color color1, Color color2, float weightColor2) {
     181        assert color1 != null && color2 != null;
     182        assert 0 <= weightColor2 && weightColor2 <= 1;
     183
     184        return new Color(
     185                Math.round((1 - weightColor2) * color1.getRed() + weightColor2 * color2.getRed()),
     186                Math.round((1 - weightColor2) * color1.getGreen() + weightColor2 * color2.getGreen()),
     187                Math.round((1 - weightColor2) * color1.getBlue() + weightColor2 * color2.getBlue()));
     188    }
    189189
    190190}
Note: See TracChangeset for help on using the changeset viewer.