OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 /** | 6 /** |
7 * @fileoverview Helpers for validating parameters to chrome-search:// iframes. | 7 * @fileoverview Helpers for validating parameters to chrome-search:// iframes. |
8 */ | 8 */ |
9 | 9 |
10 | 10 |
11 /** | 11 /** |
12 * Converts an RGB color number to a hex color string if valid. | 12 * Converts an RGB color number to a hex color string if valid. |
13 * @param {number} color A 6-digit hex RGB color code as a number. | 13 * @param {number} color A 6-digit hex RGB color code as a number. |
14 * @return {?string} A CSS representation of the color or null if invalid. | 14 * @return {?string} A CSS representation of the color or null if invalid. |
15 */ | 15 */ |
16 function convertToHexColor(color) { | 16 function convertToHexColor(color) { |
17 // Color must be a number, finite, with no fractional part, in the correct | 17 // Color must be a number, finite, with no fractional part, in the correct |
18 // range for an RGB hex color. | 18 // range for an RGB hex color. |
19 if (isFinite(color) && Math.floor(color) == color && | 19 if (isFinite(color) && Math.floor(color) == color && color >= 0 && |
20 color >= 0 && color <= 0xffffff) { | 20 color <= 0xffffff) { |
21 var hexColor = color.toString(16); | 21 var hexColor = color.toString(16); |
22 // Pads with initial zeros and # (e.g. for 'ff' yields '#0000ff'). | 22 // Pads with initial zeros and # (e.g. for 'ff' yields '#0000ff'). |
23 return '#000000'.substr(0, 7 - hexColor.length) + hexColor; | 23 return '#000000'.substr(0, 7 - hexColor.length) + hexColor; |
24 } | 24 } |
25 return null; | 25 return null; |
26 } | 26 } |
27 | 27 |
28 | 28 |
29 /** | 29 /** |
30 * Validates a RGBA color component. It must be a number between 0 and 255. | 30 * Validates a RGBA color component. It must be a number between 0 and 255. |
(...skipping 10 matching lines...) Expand all Loading... |
41 * @param {Array<number>} rgbaColor Array of rgba color components. | 41 * @param {Array<number>} rgbaColor Array of rgba color components. |
42 * @return {?string} CSS color in RGBA format or null if invalid. | 42 * @return {?string} CSS color in RGBA format or null if invalid. |
43 */ | 43 */ |
44 function convertArrayToRGBAColor(rgbaColor) { | 44 function convertArrayToRGBAColor(rgbaColor) { |
45 // Array must contain 4 valid components. | 45 // Array must contain 4 valid components. |
46 if (rgbaColor instanceof Array && rgbaColor.length === 4 && | 46 if (rgbaColor instanceof Array && rgbaColor.length === 4 && |
47 isValidRBGAComponent(rgbaColor[0]) && | 47 isValidRBGAComponent(rgbaColor[0]) && |
48 isValidRBGAComponent(rgbaColor[1]) && | 48 isValidRBGAComponent(rgbaColor[1]) && |
49 isValidRBGAComponent(rgbaColor[2]) && | 49 isValidRBGAComponent(rgbaColor[2]) && |
50 isValidRBGAComponent(rgbaColor[3])) { | 50 isValidRBGAComponent(rgbaColor[3])) { |
51 return 'rgba(' + | 51 return 'rgba(' + rgbaColor[0] + ',' + rgbaColor[1] + ',' + rgbaColor[2] + |
52 rgbaColor[0] + ',' + | 52 ',' + rgbaColor[3] / 255 + ')'; |
53 rgbaColor[1] + ',' + | |
54 rgbaColor[2] + ',' + | |
55 rgbaColor[3] / 255 + ')'; | |
56 } | 53 } |
57 return null; | 54 return null; |
58 } | 55 } |
OLD | NEW |