• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.traceview;
18 
19 import java.util.HashMap;
20 
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.GC;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.graphics.RGB;
26 import org.eclipse.swt.widgets.Display;
27 
28 public class ColorController {
29     private static final int[] systemColors = { SWT.COLOR_BLUE, SWT.COLOR_RED,
30         SWT.COLOR_GREEN, SWT.COLOR_CYAN, SWT.COLOR_MAGENTA, SWT.COLOR_DARK_BLUE,
31         SWT.COLOR_DARK_RED, SWT.COLOR_DARK_GREEN, SWT.COLOR_DARK_YELLOW,
32         SWT.COLOR_DARK_CYAN, SWT.COLOR_DARK_MAGENTA, SWT.COLOR_BLACK };
33 
34     private static RGB[] rgbColors = { new RGB(90, 90, 255), // blue
35             new RGB(0, 240, 0), // green
36             new RGB(255, 0, 0), // red
37             new RGB(0, 255, 255), // cyan
38             new RGB(255, 80, 255), // magenta
39             new RGB(200, 200, 0), // yellow
40             new RGB(40, 0, 200), // dark blue
41             new RGB(150, 255, 150), // light green
42             new RGB(150, 0, 0), // dark red
43             new RGB(30, 150, 150), // dark cyan
44             new RGB(200, 200, 255), // light blue
45             new RGB(0, 120, 0), // dark green
46             new RGB(255, 150, 150), // light red
47             new RGB(140, 80, 140), // dark magenta
48             new RGB(150, 100, 50), // brown
49             new RGB(70, 70, 70), // dark grey
50     };
51 
52     private static HashMap<Integer, Color> colorCache = new HashMap<Integer, Color>();
53     private static HashMap<Integer, Image> imageCache = new HashMap<Integer, Image>();
54 
ColorController()55     public ColorController() {
56     }
57 
requestColor(Display display, RGB rgb)58     public static Color requestColor(Display display, RGB rgb) {
59         return requestColor(display, rgb.red, rgb.green, rgb.blue);
60     }
61 
requestColorSquare(Display display, RGB rgb)62     public static Image requestColorSquare(Display display, RGB rgb) {
63         return requestColorSquare(display, rgb.red, rgb.green, rgb.blue);
64     }
65 
requestColor(Display display, int red, int green, int blue)66     public static Color requestColor(Display display, int red, int green, int blue) {
67         int key = (red << 16) | (green << 8) | blue;
68         Color color = colorCache.get(key);
69         if (color == null) {
70             color = new Color(display, red, green, blue);
71             colorCache.put(key, color);
72         }
73         return color;
74     }
75 
requestColorSquare(Display display, int red, int green, int blue)76     public static Image requestColorSquare(Display display, int red, int green, int blue) {
77         int key = (red << 16) | (green << 8) | blue;
78         Image image = imageCache.get(key);
79         if (image == null) {
80             image = new Image(display, 8, 14);
81             GC gc = new GC(image);
82             Color color = requestColor(display, red, green, blue);
83             gc.setBackground(color);
84             gc.fillRectangle(image.getBounds());
85             gc.dispose();
86             imageCache.put(key, image);
87         }
88         return image;
89     }
90 
assignMethodColors(Display display, MethodData[] methods)91     public static void assignMethodColors(Display display, MethodData[] methods) {
92         int nextColorIndex = 0;
93         for (MethodData md : methods) {
94             RGB rgb = rgbColors[nextColorIndex];
95             if (++nextColorIndex == rgbColors.length)
96                 nextColorIndex = 0;
97             Color color = requestColor(display, rgb);
98             Image image = requestColorSquare(display, rgb);
99             md.setColor(color);
100             md.setImage(image);
101 
102             // Compute and set a faded color
103             int fadedRed = 150 + rgb.red / 4;
104             int fadedGreen = 150 + rgb.green / 4;
105             int fadedBlue = 150 + rgb.blue / 4;
106             RGB faded = new RGB(fadedRed, fadedGreen, fadedBlue);
107             color = requestColor(display, faded);
108             image = requestColorSquare(display, faded);
109             md.setFadedColor(color);
110             md.setFadedImage(image);
111         }
112     }
113 }
114