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. 4 5'use strict'; 6 7/** 8 * @fileoverview Provides color scheme related functions. 9 */ 10base.exportTo('tracing', function() { 11 12 // The color palette is split in half, with the upper 13 // half of the palette being the "highlighted" verison 14 // of the base color. So, color 7's highlighted form is 15 // 7 + (palette.length / 2). 16 // 17 // These bright versions of colors are automatically generated 18 // from the base colors. 19 // 20 // Within the color palette, there are "regular" colors, 21 // which can be used for random color selection, and 22 // reserved colors, which are used when specific colors 23 // need to be used, e.g. where red is desired. 24 var paletteBase = [ 25 {r: 138, g: 113, b: 152}, 26 {r: 175, g: 112, b: 133}, 27 {r: 127, g: 135, b: 225}, 28 {r: 93, g: 81, b: 137}, 29 {r: 116, g: 143, b: 119}, 30 {r: 178, g: 214, b: 122}, 31 {r: 87, g: 109, b: 147}, 32 {r: 119, g: 155, b: 95}, 33 {r: 114, g: 180, b: 160}, 34 {r: 132, g: 85, b: 103}, 35 {r: 157, g: 210, b: 150}, 36 {r: 148, g: 94, b: 86}, 37 {r: 164, g: 108, b: 138}, 38 {r: 139, g: 191, b: 150}, 39 {r: 110, g: 99, b: 145}, 40 {r: 80, g: 129, b: 109}, 41 {r: 125, g: 140, b: 149}, 42 {r: 93, g: 124, b: 132}, 43 {r: 140, g: 85, b: 140}, 44 {r: 104, g: 163, b: 162}, 45 {r: 132, g: 141, b: 178}, 46 {r: 131, g: 105, b: 147}, 47 {r: 135, g: 183, b: 98}, 48 {r: 152, g: 134, b: 177}, 49 {r: 141, g: 188, b: 141}, 50 {r: 133, g: 160, b: 210}, 51 {r: 126, g: 186, b: 148}, 52 {r: 112, g: 198, b: 205}, 53 {r: 180, g: 122, b: 195}, 54 {r: 203, g: 144, b: 152}, 55 // Reserved Entires 56 {r: 182, g: 125, b: 143}, 57 {r: 126, g: 200, b: 148}, 58 {r: 133, g: 160, b: 210}, 59 {r: 240, g: 240, b: 240}, 60 {r: 199, g: 155, b: 125}]; 61 62 // Make sure this number tracks the number of reserved entries in the 63 // palette. 64 var numReservedColorIds = 5; 65 66 function brighten(c) { 67 var k; 68 if (c.r >= 240 && c.g >= 240 && c.b >= 240) 69 k = -0.20; 70 else 71 k = 0.45; 72 73 return {r: Math.min(255, c.r + Math.floor(c.r * k)), 74 g: Math.min(255, c.g + Math.floor(c.g * k)), 75 b: Math.min(255, c.b + Math.floor(c.b * k))}; 76 } 77 function colorToString(c) { 78 return 'rgb(' + c.r + ',' + c.g + ',' + c.b + ')'; 79 } 80 81 /** 82 * The number of color IDs that getStringColorId can choose from. 83 */ 84 var numRegularColorIds = paletteBase.length - numReservedColorIds; 85 var highlightIdBoost = paletteBase.length; 86 87 var palette = paletteBase.concat(paletteBase.map(brighten)). 88 map(colorToString); 89 /** 90 * Computes a simplistic hashcode of the provide name. Used to chose colors 91 * for slices. 92 * @param {string} name The string to hash. 93 */ 94 function getStringHash(name) { 95 var hash = 0; 96 for (var i = 0; i < name.length; ++i) 97 hash = (hash + 37 * hash + 11 * name.charCodeAt(i)) % 0xFFFFFFFF; 98 return hash; 99 } 100 101 /** 102 * Gets the color palette. 103 */ 104 function getColorPalette() { 105 return palette; 106 } 107 108 /** 109 * @return {Number} The value to add to a color ID to get its highlighted 110 * colro ID. E.g. 7 + getPaletteHighlightIdBoost() yields a brightened from 111 * of 7's base color. 112 */ 113 function getColorPaletteHighlightIdBoost() { 114 return highlightIdBoost; 115 } 116 117 /** 118 * @param {String} name The color name. 119 * @return {Number} The color ID for the given color name. 120 */ 121 function getColorIdByName(name) { 122 if (name == 'iowait') 123 return numRegularColorIds; 124 if (name == 'running') 125 return numRegularColorIds + 1; 126 if (name == 'runnable') 127 return numRegularColorIds + 2; 128 if (name == 'sleeping') 129 return numRegularColorIds + 3; 130 if (name == 'UNKNOWN') 131 return numRegularColorIds + 4; 132 throw new Error('Unrecognized color ') + name; 133 } 134 135 // Previously computed string color IDs. They are based on a stable hash, so 136 // it is safe to save them throughout the program time. 137 var stringColorIdCache = {}; 138 139 /** 140 * @return {Number} A color ID that is stably associated to the provided via 141 * the getStringHash method. The color ID will be chosen from the regular 142 * ID space only, e.g. no reserved ID will be used. 143 */ 144 function getStringColorId(string) { 145 if (stringColorIdCache[string] === undefined) { 146 var hash = getStringHash(string); 147 stringColorIdCache[string] = hash % numRegularColorIds; 148 } 149 return stringColorIdCache[string]; 150 } 151 152 return { 153 getColorPalette: getColorPalette, 154 getColorPaletteHighlightIdBoost: getColorPaletteHighlightIdBoost, 155 getColorIdByName: getColorIdByName, 156 getStringHash: getStringHash, 157 getStringColorId: getStringColorId 158 }; 159}); 160