aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tensorboard/components/tf_color_scale/colorScale.ts
blob: ff90d46aa249d240250854b0ec631834286ee651 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// Example usage:
// runs = ["train", "test", "test1", "test2"]
// ccs = new ColorScale();
// ccs.domain(runs);
// ccs.getColor("train");
// ccs.getColor("test1");
import * as d3 from 'd3';  // from //third_party/javascript/typings/d3_v4
import {palettes} from './palettes'


export class ColorScale {
  private palette: string[];
  private identifiers = d3.map();

  /**
   * Creates a color scale with optional custom palette.
   *  @param {string[]} [palette=palettes.googleColorBlind] - The color
   *                 palette you want as an Array of hex strings.
   */
  constructor(palette: string[] = palettes.googleColorBlindAssist) {
    this.palette = palette;
  }

  /**
   * Set the domain of strings.
   * @param {string[]} strings - An array of possible strings to use as the
   *                             domain for your scale.
   */
  public domain(strings: string[]): this {
    this.identifiers = d3.map();
    strings.forEach((s, i) => {
      this.identifiers.set(s, this.palette[i % this.palette.length]);
    });
    return this;
  }

  /**
   * Use the color scale to transform an element in the domain into a color.
   * @param {string} The input string to map to a color.
   * @return {string} The color corresponding to that input string.
   * @throws Will error if input string is not in the scale's domain.
   */
  public scale(s: string): string {
    if (!this.identifiers.has(s)) {
      throw new Error('String was not in the domain.');
    }
    return this.identifiers.get(s) as string;
  }
}

Polymer({
  is: 'tf-color-scale',
  properties: {
    runs: {
      type: Array,
    },
    outColorScale: {
      type: Object,
      readOnly: true,
      notify: true,
      value: function() {
        return new ColorScale();
      },
    },
  },
  observers: ['updateColorScale(runs.*)'],
  updateColorScale: function(runsChange) {
    this.outColorScale.domain(this.runs);
  },
});