| <!DOCTYPE html> |
| <!-- |
| Copyright 2015 The Chromium Authors. All rights reserved. |
| Use of this source code is governed by a BSD-style license that can be |
| found in the LICENSE file. |
| --> |
| |
| <script> |
| 'use strict'; |
| |
| /** |
| * Does a deep copy of the object. |
| * @param {*} obj Object to copy. |
| * @return {*} A copy of the object. |
| */ |
| function deepCopy(obj) { |
| return JSON.parse(JSON.stringify(obj)); |
| } |
| |
| /** |
| * JavaScript for graphing performance data. |
| */ |
| var graph = (function() { |
| |
| /** |
| * Returns a hex string color from hash code of a string. This a popular |
| * implementation of generating hash code and spread over rgb color range |
| * http://stackoverflow.com/questions/3426404. |
| * @param {string} str string to get color code for. |
| * @return {string} A rgb color code string. |
| */ |
| var stringToColor = function(str) { |
| var hash = hashCode_(str); |
| var red = hash & 0xff; |
| var green = (hash >> 8) & 0xff; |
| var blue = (hash >> 16) & 0xff; |
| return 'rgb(' + red + ',' + green + ',' + blue + ')'; |
| }; |
| |
| /** |
| * Gets a hash code for a string. |
| * This is used by 'stringToColor' to generate a unique hash code |
| * so that each string will always map to the same color. |
| * @param {string} str A string to hash. |
| * @return {number} An integer number. |
| */ |
| var hashCode_ = function(str) { |
| var hash = 0; |
| for (var i = 0; i < str.length; i++) { |
| hash = hash * 31 + str.charCodeAt(i); |
| // Convert to 32-bit integer. |
| hash = hash & hash; |
| } |
| return hash; |
| }; |
| |
| return { |
| stringToColor: stringToColor |
| }; |
| })(); |
| |
| </script> |