3 Copyright (c) 2014 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
7 <link rel="import" href="/tvcm.html">
12 * @fileoverview Provides color scheme related functions.
14 tvcm.exportTo('tvcm.ui', function() {
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 * Gets the raw color palette, where entries are still objects.
118 function getRawColorPalette() {
123 * @return {Number} The value to add to a color ID to get its highlighted
124 * colro ID. E.g. 7 + getPaletteHighlightIdBoost() yields a brightened from
127 function getColorPaletteHighlightIdBoost() {
128 return highlightIdBoost;
132 * @param {String} name The color name.
133 * @return {Number} The color ID for the given color name.
135 function getColorIdByName(name) {
136 if (name == 'iowait')
137 return numRegularColorIds;
138 if (name == 'running')
139 return numRegularColorIds + 1;
140 if (name == 'runnable')
141 return numRegularColorIds + 2;
142 if (name == 'sleeping')
143 return numRegularColorIds + 3;
144 if (name == 'UNKNOWN')
145 return numRegularColorIds + 4;
146 throw new Error('Unrecognized color ') + name;
149 // Previously computed string color IDs. They are based on a stable hash, so
150 // it is safe to save them throughout the program time.
151 var stringColorIdCache = {};
154 * @return {Number} A color ID that is stably associated to the provided via
155 * the getStringHash method. The color ID will be chosen from the regular
156 * ID space only, e.g. no reserved ID will be used.
158 function getStringColorId(string) {
159 if (stringColorIdCache[string] === undefined) {
160 var hash = getStringHash(string);
161 stringColorIdCache[string] = hash % numRegularColorIds;
163 return stringColorIdCache[string];
167 colorToRGBString: colorToRGBString,
168 colorToRGBAString: colorToRGBAString,
169 getRawColorPalette: getRawColorPalette,
170 getColorPalette: getColorPalette,
172 numRegularColorIds: numRegularColorIds,
173 highlightIdBoost: highlightIdBoost
175 getColorPaletteHighlightIdBoost: getColorPaletteHighlightIdBoost,
176 getColorIdByName: getColorIdByName,
177 getStringHash: getStringHash,
178 getStringColorId: getStringColorId