Changeset 23189 in osm for applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/visualisation/FloatPropertyColorScheme.java
- Timestamp:
- 2010-09-15T18:53:09+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/visualisation/FloatPropertyColorScheme.java
r18130 r23189 22 22 public class FloatPropertyColorScheme implements ColorScheme { 23 23 24 25 26 24 private final Class<? extends RoadPropertyType<Float>> propertyClass; 25 private final Map<Float, Color> colorMap; 26 private final Color defaultColor; 27 27 28 29 30 31 32 33 34 35 36 37 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; 38 38 39 40 41 42 39 this.propertyClass = propertyClass; 40 this.colorMap = new HashMap<Float, Color>(colorMap); 41 this.defaultColor = defaultColor; 42 } 43 43 44 45 44 public Color getSegmentColor(Segment segment) { 45 assert segment != null; 46 46 47 48 49 50 51 52 53 54 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 } 57 57 58 59 60 61 62 63 58 if (propertyValue != null) { 59 return getColorForValue(propertyValue); 60 } else { 61 return defaultColor; 62 } 63 } 64 64 65 65 public Color getNodeColor(GraphNode node) { 66 66 67 67 List<Color> segmentColors = new ArrayList<Color>(); 68 68 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 }85 69 86 if (segmentColors.size() > 0) {87 return averageColor(segmentColors);88 } else {89 return Color.WHITE;90 }91 70 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 } 93 85 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 } 101 91 102 if (colorMap.containsKey(value)) { 92 } 103 93 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; 105 101 106 } else{102 if (colorMap.containsKey(value)) { 107 103 108 LinkedList<Float> valuesWithDefinedColor = new LinkedList<Float>(colorMap.keySet()); 109 Collections.sort(valuesWithDefinedColor); 104 return colorMap.get(value); 110 105 111 if (value <= valuesWithDefinedColor.getFirst()){106 } else { 112 107 113 return colorMap.get(valuesWithDefinedColor.getFirst()); 108 LinkedList<Float> valuesWithDefinedColor = new LinkedList<Float>(colorMap.keySet()); 109 Collections.sort(valuesWithDefinedColor); 114 110 115 } elseif (value>= valuesWithDefinedColor.getLast()) {111 if (value <= valuesWithDefinedColor.getFirst()) { 116 112 117 Last());113 return colorMap.get(valuesWithDefinedColor.getFirst()); 118 114 119 } else{115 } else if (value >= valuesWithDefinedColor.getLast()) { 120 116 121 /* interpolate */ 117 return colorMap.get(valuesWithDefinedColor.getLast()); 122 118 123 Float lowerValue = valuesWithDefinedColor.getFirst(); 124 Float higherValue = null; 119 } else { 125 120 126 for (Float v : valuesWithDefinedColor) { 127 if (v >= value) { 128 higherValue = v; 129 break; 130 } 131 lowerValue = v; 132 } 121 /* interpolate */ 133 122 134 assert lowerValue != null && higherValue != null; 123 Float lowerValue = valuesWithDefinedColor.getFirst(); 124 Float higherValue = null; 135 125 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 } 138 133 139 float weightHigherColor = (value -lowerValue) / (higherValue- lowerValue);134 assert lowerValue != null && higherValue != null; 140 135 141 return weightedAverageColor(lowerColor, higherColor, weightHigherColor); 136 Color lowerColor = colorMap.get(lowerValue); 137 Color higherColor = colorMap.get(higherValue); 142 138 143 } 139 float weightHigherColor = (value - lowerValue) / (higherValue - lowerValue); 144 140 145 } 141 return weightedAverageColor(lowerColor, higherColor, weightHigherColor); 146 142 147 143 } 148 144 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 } 157 146 158 float weightPerColor = 1.0f / colors.size(); 147 } 159 148 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; 161 157 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(); 168 159 169 return average; 170 } 160 Color average = new Color(0,0,0); 171 161 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 } 183 168 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 } 189 189 190 190 }
Note:
See TracChangeset
for help on using the changeset viewer.