comparison visualizer/Filter/src/com/sun/hotspot/igv/filter/GradientColorFilter.java @ 4512:015fb895586b

Moved visualizer to new directory.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 07 Feb 2012 22:41:09 +0100
parents
children
comparison
equal deleted inserted replaced
4511:6cb549627941 4512:015fb895586b
1 /*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 package com.sun.hotspot.igv.filter;
25
26 import com.sun.hotspot.igv.graph.Diagram;
27 import com.sun.hotspot.igv.graph.Figure;
28 import java.awt.*;
29 import java.awt.geom.AffineTransform;
30 import java.awt.image.Raster;
31 import java.util.List;
32
33 /**
34 * Filter that colors nodes using a customizable color gradient, based on how
35 * a numeric property is located in a specified interval.
36 *
37 * @author Peter Hofer
38 */
39 public class GradientColorFilter extends AbstractFilter {
40
41 public static final String LINEAR = "LINEAR";
42 public static final String LOGARITHMIC = "LOGARITHMIC";
43
44 private String propertyName = "probability";
45 private float minValue = 0;
46 private float maxValue = 500;
47 private float[] fractions = {0, 0.5f, 1};
48 private Color[] colors = {Color.BLUE, Color.YELLOW, Color.RED};
49 private int shadeCount = 8;
50 private String mode = LINEAR;
51
52 @Override
53 public String getName() {
54 return "Gradient Color Filter";
55 }
56
57 @Override
58 public void apply(Diagram d) {
59 boolean logarithmic = mode.equalsIgnoreCase(LOGARITHMIC);
60 if (!logarithmic && !mode.equalsIgnoreCase(LINEAR)) {
61 throw new RuntimeException("Unknown mode: " + mode);
62 }
63
64 Rectangle bounds = new Rectangle(shadeCount, 1);
65 LinearGradientPaint lgp = new LinearGradientPaint(bounds.x, bounds.y, bounds.width, bounds.y, fractions, colors);
66 PaintContext context = lgp.createContext(null, bounds, bounds.getBounds2D(), AffineTransform.getTranslateInstance(0, 0), new RenderingHints(null));
67 Raster raster = context.getRaster(bounds.x, bounds.y, bounds.width, bounds.height);
68 int[] rgb = raster.getPixels(bounds.x, bounds.y, bounds.width, bounds.height, (int[]) null);
69 Color[] shades = new Color[rgb.length / 3];
70 for (int i = 0; i < shades.length; ++i) {
71 shades[i] = new Color(rgb[i * 3], rgb[i * 3 + 1], rgb[i * 3 + 2]);
72 }
73
74 List<Figure> figures = d.getFigures();
75 for (Figure f : figures) {
76 String property = f.getProperties().get(propertyName);
77 if (property != null) {
78 try {
79 float value = Float.parseFloat(property);
80
81 Color nodeColor;
82 if (value <= minValue) {
83 nodeColor = colors[0];
84 } else if (value >= maxValue) {
85 nodeColor = colors[colors.length - 1];
86 } else {
87 double normalized = value - minValue;
88 double interval = maxValue - minValue;
89 int index;
90 // Use Math.ceil() to make values above zero distinguishable from zero
91 if (logarithmic) {
92 index = (int) Math.ceil(shades.length * Math.log(1 + normalized) / Math.log(1 + interval));
93 } else {
94 index = (int) Math.ceil(shades.length * normalized / interval);
95 }
96 nodeColor = shades[index];
97 }
98 f.setColor(nodeColor);
99 } catch (Exception e) {
100 e.printStackTrace();
101 }
102 }
103 }
104 }
105
106 public String getPropertyName() {
107 return propertyName;
108 }
109
110 public void setPropertyName(String propertyName) {
111 this.propertyName = propertyName;
112 }
113
114 public float getMinValue() {
115 return minValue;
116 }
117
118 public void setMinValue(float minValue) {
119 this.minValue = minValue;
120 }
121
122 public float getMaxValue() {
123 return maxValue;
124 }
125
126 public void setMaxValue(float maxValue) {
127 this.maxValue = maxValue;
128 }
129
130 public float[] getFractions() {
131 return fractions;
132 }
133
134 public void setFractions(float[] fractions) {
135 this.fractions = fractions;
136 }
137
138 public Color[] getColors() {
139 return colors;
140 }
141
142 public void setColors(Color[] colors) {
143 this.colors = colors;
144 }
145
146 public int getShadeCount() {
147 return shadeCount;
148 }
149
150 public void setShadeCount(int shadeCount) {
151 this.shadeCount = shadeCount;
152 }
153
154 public String getMode() {
155 return mode;
156 }
157
158 public void setMode(String mode) {
159 this.mode = mode;
160 }
161 }