1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 tvcm.require('tracing.trace_model.event');
10 * @fileoverview Provides color scheme related functions.
12 tvcm.exportTo('tracing', function() {
14 var SelectionState = tracing.trace_model.SelectionState;
16 // The color palette is split in half, with the upper
17 // half of the palette being the "highlighted" verison
18 // of the base color. So, color 7's highlighted form is
19 // 7 + (palette.length / 2).
21 // These bright versions of colors are automatically generated
22 // from the base colors.
24 // Within the color palette, there are "regular" colors,
25 // which can be used for random color selection, and
26 // reserved colors, which are used when specific colors
27 // need to be used, e.g. where red is desired.
29 {r: 138, g: 113, b: 152},
30 {r: 175, g: 112, b: 133},
31 {r: 127, g: 135, b: 225},
32 {r: 93, g: 81, b: 137},
33 {r: 116, g: 143, b: 119},
34 {r: 178, g: 214, b: 122},
35 {r: 87, g: 109, b: 147},
36 {r: 119, g: 155, b: 95},
37 {r: 114, g: 180, b: 160},
38 {r: 132, g: 85, b: 103},
39 {r: 157, g: 210, b: 150},
40 {r: 148, g: 94, b: 86},
41 {r: 164, g: 108, b: 138},
42 {r: 139, g: 191, b: 150},
43 {r: 110, g: 99, b: 145},
44 {r: 80, g: 129, b: 109},
45 {r: 125, g: 140, b: 149},
46 {r: 93, g: 124, b: 132},
47 {r: 140, g: 85, b: 140},
48 {r: 104, g: 163, b: 162},
49 {r: 132, g: 141, b: 178},
50 {r: 131, g: 105, b: 147},
51 {r: 135, g: 183, b: 98},
52 {r: 152, g: 134, b: 177},
53 {r: 141, g: 188, b: 141},
54 {r: 133, g: 160, b: 210},
55 {r: 126, g: 186, b: 148},
56 {r: 112, g: 198, b: 205},
57 {r: 180, g: 122, b: 195},
58 {r: 203, g: 144, b: 152},
60 {r: 182, g: 125, b: 143},
61 {r: 126, g: 200, b: 148},
62 {r: 133, g: 160, b: 210},
63 {r: 240, g: 240, b: 240},
64 {r: 199, g: 155, b: 125}];
66 // Make sure this number tracks the number of reserved entries in the
68 var numReservedColorIds = 5;
70 function brighten(c) {
72 if (c.r >= 240 && c.g >= 240 && c.b >= 240)
77 return {r: Math.min(255, c.r + Math.floor(c.r * k)),
78 g: Math.min(255, c.g + Math.floor(c.g * k)),
79 b: Math.min(255, c.b + Math.floor(c.b * k))};
81 function colorToRGBString(c) {
82 return 'rgb(' + c.r + ',' + c.g + ',' + c.b + ')';
84 function colorToRGBAString(c, a) {
85 return 'rgba(' + c.r + ',' + c.g + ',' + c.b + ',' + a + ')';
89 * The number of color IDs that getStringColorId can choose from.
91 var numRegularColorIds = paletteBase.length - numReservedColorIds;
92 var highlightIdBoost = paletteBase.length;
94 var paletteRaw = paletteBase.concat(paletteBase.map(brighten));
95 var palette = paletteRaw.map(colorToRGBString);
97 * Computes a simplistic hashcode of the provide name. Used to chose colors
99 * @param {string} name The string to hash.
101 function getStringHash(name) {
103 for (var i = 0; i < name.length; ++i)
104 hash = (hash + 37 * hash + 11 * name.charCodeAt(i)) % 0xFFFFFFFF;
109 * Gets the color palette.
111 function getColorPalette() {
116 * @return {Number} The value to add to a color ID to get its highlighted
117 * colro ID. E.g. 7 + getPaletteHighlightIdBoost() yields a brightened from
120 function getColorPaletteHighlightIdBoost() {
121 return highlightIdBoost;
125 * @param {String} name The color name.
126 * @return {Number} The color ID for the given color name.
128 function getColorIdByName(name) {
129 if (name == 'iowait')
130 return numRegularColorIds;
131 if (name == 'running')
132 return numRegularColorIds + 1;
133 if (name == 'runnable')
134 return numRegularColorIds + 2;
135 if (name == 'sleeping')
136 return numRegularColorIds + 3;
137 if (name == 'UNKNOWN')
138 return numRegularColorIds + 4;
139 throw new Error('Unrecognized color ') + name;
142 // Previously computed string color IDs. They are based on a stable hash, so
143 // it is safe to save them throughout the program time.
144 var stringColorIdCache = {};
147 * @return {Number} A color ID that is stably associated to the provided via
148 * the getStringHash method. The color ID will be chosen from the regular
149 * ID space only, e.g. no reserved ID will be used.
151 function getStringColorId(string) {
152 if (stringColorIdCache[string] === undefined) {
153 var hash = getStringHash(string);
154 stringColorIdCache[string] = hash % numRegularColorIds;
156 return stringColorIdCache[string];
160 * Provides methods to get view values for events.
162 var EventPresenter = {
163 getAlpha_: function(event) {
164 if (event.selectionState === SelectionState.DIMMED)
169 getColorIdOffset_: function(event) {
170 if (event.selectionState === SelectionState.SELECTED)
171 return highlightIdBoost;
175 getTextColor: function(event) {
176 if (event.selectionState === SelectionState.DIMMED)
177 return 'rgb(60,60,60)';
181 getSliceColorId: function(slice) {
182 return slice.colorId + this.getColorIdOffset_(slice);
185 getSliceAlpha: function(slice, async) {
186 var alpha = this.getAlpha_(slice);
192 getInstantSliceColor: function(instant) {
193 var colorId = instant.colorId + this.getColorIdOffset_(instant);
194 return colorToRGBAString(paletteRaw[colorId], this.getAlpha_(instant));
197 getObjectInstanceColor: function(instance) {
198 var colorId = instance.colorId + this.getColorIdOffset_(instance);
199 return colorToRGBAString(paletteRaw[colorId], 0.25);
202 getObjectSnapshotColor: function(snapshot) {
204 snapshot.objectInstance.colorId + this.getColorIdOffset_(snapshot);
205 return palette[colorId];
208 getCounterSeriesColor: function(colorId, selectionState) {
209 return colorToRGBAString(
211 this.getAlpha_({selectionState: selectionState}));
214 getBarSnapshotColor: function(snapshot, offset) {
216 (snapshot.objectInstance.colorId + offset) % numRegularColorIds;
217 colorId += this.getColorIdOffset_(snapshot);
218 return colorToRGBAString(paletteRaw[colorId], this.getAlpha_(snapshot));
223 getColorPalette: getColorPalette,
224 getColorPaletteHighlightIdBoost: getColorPaletteHighlightIdBoost,
225 getColorIdByName: getColorIdByName,
226 getStringHash: getStringHash,
227 getStringColorId: getStringColorId,
228 EventPresenter: EventPresenter