Home | History | Annotate | Download | only in src
      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  */
     10 base.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 
     61   // Make sure this number tracks the number of reserved entries in the
     62   // palette.
     63   var numReservedColorIds = 4;
     64 
     65   function brighten(c) {
     66     var k;
     67     if (c.r >= 240 && c.g >= 240 && c.b >= 240)
     68       k = -0.20;
     69     else
     70       k = 0.45;
     71 
     72     return {r: Math.min(255, c.r + Math.floor(c.r * k)),
     73       g: Math.min(255, c.g + Math.floor(c.g * k)),
     74       b: Math.min(255, c.b + Math.floor(c.b * k))};
     75   }
     76   function colorToString(c) {
     77     return 'rgb(' + c.r + ',' + c.g + ',' + c.b + ')';
     78   }
     79 
     80   /**
     81    * The number of color IDs that getStringColorId can choose from.
     82    */
     83   var numRegularColorIds = paletteBase.length - numReservedColorIds;
     84   var highlightIdBoost = paletteBase.length;
     85 
     86   var palette = paletteBase.concat(paletteBase.map(brighten)).
     87       map(colorToString);
     88   /**
     89    * Computes a simplistic hashcode of the provide name. Used to chose colors
     90    * for slices.
     91    * @param {string} name The string to hash.
     92    */
     93   function getStringHash(name) {
     94     var hash = 0;
     95     for (var i = 0; i < name.length; ++i)
     96       hash = (hash + 37 * hash + 11 * name.charCodeAt(i)) % 0xFFFFFFFF;
     97     return hash;
     98   }
     99 
    100   /**
    101    * Gets the color palette.
    102    */
    103   function getColorPalette() {
    104     return palette;
    105   }
    106 
    107   /**
    108    * @return {Number} The value to add to a color ID to get its highlighted
    109    * colro ID. E.g. 7 + getPaletteHighlightIdBoost() yields a brightened from
    110    * of 7's base color.
    111    */
    112   function getColorPaletteHighlightIdBoost() {
    113     return highlightIdBoost;
    114   }
    115 
    116   /**
    117    * @param {String} name The color name.
    118    * @return {Number} The color ID for the given color name.
    119    */
    120   function getColorIdByName(name) {
    121     if (name == 'iowait')
    122       return numRegularColorIds;
    123     if (name == 'running')
    124       return numRegularColorIds + 1;
    125     if (name == 'runnable')
    126       return numRegularColorIds + 2;
    127     if (name == 'sleeping')
    128       return numRegularColorIds + 3;
    129     throw new Error('Unrecognized color ') + name;
    130   }
    131 
    132   // Previously computed string color IDs. They are based on a stable hash, so
    133   // it is safe to save them throughout the program time.
    134   var stringColorIdCache = {};
    135 
    136   /**
    137    * @return {Number} A color ID that is stably associated to the provided via
    138    * the getStringHash method. The color ID will be chosen from the regular
    139    * ID space only, e.g. no reserved ID will be used.
    140    */
    141   function getStringColorId(string) {
    142     if (stringColorIdCache[string] === undefined) {
    143       var hash = getStringHash(string);
    144       stringColorIdCache[string] = hash % numRegularColorIds;
    145     }
    146     return stringColorIdCache[string];
    147   }
    148 
    149   return {
    150     getColorPalette: getColorPalette,
    151     getColorPaletteHighlightIdBoost: getColorPaletteHighlightIdBoost,
    152     getColorIdByName: getColorIdByName,
    153     getStringHash: getStringHash,
    154     getStringColorId: getStringColorId
    155   };
    156 });
    157