summaryrefslogtreecommitdiffstats
path: root/devtools/client/memory/components/tree-map/color-coarse-type.js
blob: 5f033ea26c5c7640dd8c5c7f1f602f54b48f8cf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/**
 * Color the boxes in the treemap
 */

const TYPES = [ "objects", "other", "strings", "scripts" ];

// The factors determine how much the hue shifts
const TYPE_FACTOR = TYPES.length * 3;
const DEPTH_FACTOR = -10;
const H = 0.5;
const S = 0.6;
const L = 0.9;

/**
 * Recursively find the index of the coarse type of a node
 *
 * @param  {Object} node
 *         d3 treemap
 * @return {Integer}
 *         index
 */
function findCoarseTypeIndex(node) {
  let index = TYPES.indexOf(node.name);

  if (node.parent) {
    return index === -1 ? findCoarseTypeIndex(node.parent) : index;
  }

  return TYPES.indexOf("other");
}

/**
 * Decide a color value for depth to be used in the HSL computation
 *
 * @param  {Object} node
 * @return {Number}
 */
function depthColorFactor(node) {
  return Math.min(1, node.depth / DEPTH_FACTOR);
}

/**
 * Decide a color value for type to be used in the HSL computation
 *
 * @param  {Object} node
 * @return {Number}
 */
function typeColorFactor(node) {
  return findCoarseTypeIndex(node) / TYPE_FACTOR;
}

/**
 * Color a node
 *
 * @param  {Object} node
 * @return {Array} HSL values ranged 0-1
 */
module.exports = function colorCoarseType(node) {
  let h = Math.min(1, H + typeColorFactor(node));
  let s = Math.min(1, S);
  let l = Math.min(1, L + depthColorFactor(node));

  return [h, s, l];
};