# All files / src Gradient.ts

94.66% Statements 142/150
90.54% Branches 67/74
84.61% Functions 11/13
96.47% Lines 137/142

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304

    19x
    15x
    15x
    15x

    4x
    4x

    4x
    4x

    4x
    1x
    3x

    2x
    2x
    2x
    1x

    1x

    1x

    9x

    9x
    9x
    9x
    9x
    9x
    9x

    2x
    2x
    7x
    1x
    1x

    3x
    2x

    1x

    5x
    5x
    4x
    4x

    1x
    1x

    5x

    5x
    5x
    5x
    5x

    5x
    5x
    2x
    1x
    1x

    5x
    3x
    3x
    3x
    2x

    1x
    1x
    1x

    1x

    8x

    8x
    8x
    8x
    8x
    8x

    2x
    2x
    6x

    6x
    6x
    5x
    5x

    1x
    1x

    6x

    6x
    6x
    6x
    6x

    6x
    6x
    6x

    6x

    3x
    3x
    3x
    3x

    1x
    1x
    1x
    2x

    1x
    1x
    1x

    1x
    1x
    1x

    3x
    2x

    1x

    4x

    4x
    4x
    4x
    4x
    4x

    2x
    2x

    4x

    2x
    2x
    1x
    1x

    1x
    1x

    2x
    2x
    2x
    2x
    2x

    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x

    3x
    2x

    1x

    18x

    18x
    18x
    18x
    18x
    18x
    18x

     export abstract class GradientType {
      gradient?: string;
      abstract valueToHex(value: number, range: number): number;
      abstract range(): [number, number] | null;
    }

    export function normalizeValue(
      lo: number,
      hi: number,
      val: number
    ): { lo: number; hi: number; val: number } {
      if (hi >= lo) {
        if (val < lo) val = lo;
        if (val > hi) val = hi;
        return { lo: lo, hi: hi, val: val };
      } else {
        if (val > lo) val = lo;
        if (val < hi) val = hi;
        //flip the meaning of val, lo, hi
        val = lo - val + hi;
        return { lo: hi, hi: lo, val: val };
      }
    }

    //return a Gradient object, even if what is specified is descriptive
    export function getGradient(grad) {
      if (grad instanceof GradientType) {
        return grad;
      } else if (
        grad.gradient !== undefined &&
        builtinGradients[grad.gradient]
      ) {
        let min = grad.min === undefined ? -1 : grad.min;
        let max = grad.max === undefined ? 1 : grad.max;
        if (grad.mid === undefined) {
          return new builtinGradients[grad.gradient](min, max);
        } else {
          return new builtinGradients[grad.gradient](min, max, grad.mid);
        }
      }
      return grad;
    }

    /**
     * Color scheme red to white to blue, for charges
     * Reverse gradients are supported when min>max so that the colors are displayed in reverse order.
     * @subcategory Gradients
     */
    export class RWB extends GradientType {
      gradient = "RWB";
      min: number;
      max: number;
      mid?: number;
      mult: number;
      constructor(min?: number | [number, number], max?: number, mid?: number) {
        super();
        this.mult = 1.0;
        this.mid = mid;
        this.min = min as number;
        this.max = max as number;
        if (typeof max == "undefined" && Array.isArray(min) && min.length >= 2) {
          //we were passed a single range
          this.max = min[1];
          this.min = min[0];
        } else if (!!min && !!max && !Array.isArray(min)) {
          this.min = min;
          this.max = max;
        }
      }

      //return range used for color mapping, null if none set
      range() {
        if (typeof this.min != "undefined" && typeof this.max != "undefined") {
          return [this.min, this.max] as [number, number];
        }
        return null;
      }

      //map value to hex color, range is provided
      valueToHex(val, range) {
        var lo, hi;
        val = this.mult * val; //reverse if necessary
        if (range) {
          lo = range[0];
          hi = range[1];
        } else {
          lo = this.min;
          hi = this.max;
        }

        Iif (val === undefined) return 0xffffff;

        var norm = normalizeValue(lo, hi, val);
        lo = norm.lo;
        hi = norm.hi;
        val = norm.val;

        var middle = (hi + lo) / 2;
        if (range && typeof range[2] != "undefined") middle = range[2];
        else if (typeof this.mid != "undefined")
          middle = this.mid; //allow user to specify midpoint
        else middle = (lo + hi) / 2;
        var scale, color;

        //scale bottom from red to white
        if (val < middle) {
          scale = Math.floor(255 * Math.sqrt((val - lo) / (middle - lo)));
          color = 0xff0000 + 0x100 * scale + scale;
          return color;
        } else if (val > middle) {
          //form white to blue
          scale = Math.floor(255 * Math.sqrt(1 - (val - middle) / (hi - middle)));
          color = 0x10000 * scale + 0x100 * scale + 0xff;
          return color;
        } else {
          //val == middle
          return 0xffffff;
        }
      }
    }

    /**
     * rainbow gradient, but without purple to match jmol
     * Reverse gradients are supported when min>max so that the colors are displayed in reverse order.
     * @subcategory Gradients
     */
    export class ROYGB extends GradientType {
      gradient = "ROYGB";
      mult: number;
      max: number;
      min: number;
      constructor(min, max) {
        super();
        this.mult = 1.0;
        this.min = min;
        this.max = max;
        if (typeof max == "undefined" && Array.isArray(min) && min.length >= 2) {
          //we were passed a single range
          this.max = min[1];
          this.min = min[0];
        } else Iif (!!min && !!max && !Array.isArray(min)) {
          this.min = min;
          this.max = max;
        }
      };
      //map value to hex color, range is provided
      valueToHex(val, range) {
        var lo, hi;
        val = this.mult * val;
        if (range) {
          lo = range[0];
          hi = range[1];
        } else {
          lo = this.min;
          hi = this.max;
        }

        Iif (typeof val == "undefined") return 0xffffff;

        var norm = normalizeValue(lo, hi, val);
        lo = norm.lo;
        hi = norm.hi;
        val = norm.val;

        var mid = (lo + hi) / 2;
        var q1 = (lo + mid) / 2;
        var q3 = (mid + hi) / 2;

        var scale, color;
        if (val < q1) {
          //scale green up, red up, blue down
          scale = Math.floor(255 * Math.sqrt((val - lo) / (q1 - lo)));
          color = 0xff0000 + 0x100 * scale + 0;
          return color;
        } else if (val < mid) {
          //scale red down, green up, blue down
          scale = Math.floor(255 * Math.sqrt(1 - (val - q1) / (mid - q1)));
          color = 0x010000 * scale + 0xff00 + 0x0;
          return color;
        } else if (val < q3) {
          //scale blue up, red down, green up
          scale = Math.floor(255 * Math.sqrt((val - mid) / (q3 - mid)));
          color = 0x000000 + 0xff00 + 0x1 * scale;
          return color;
        } else {
          //scale green down, blue up, red down
          scale = Math.floor(255 * Math.sqrt(1 - (val - q3) / (hi - q3)));
          color = 0x000000 + 0x0100 * scale + 0xff;
          return color;
        }
      };

      //return range used for color mapping, null if none set
      range() {
        if (typeof this.min != "undefined" && typeof this.max != "undefined") {
          return [this.min, this.max] as [number, number];
        }
        return null;
      };
    }
    /**
     * rainbow gradient with constant saturation, all the way to purple!
     * Reverse gradients are supported when min>max so that the colors are displayed in reverse order.
      * @subcategory Gradients
     *
     * @example $.get('data/1fas.pqr', function(data){
          viewer.addModel(data, "pqr");
          $.get("data/1fas.cube",function(volumedata){
              viewer.addSurface($3Dmol.SurfaceType.VDW, {
                  opacity:0.85,
                  voldata: new $3Dmol.VolumeData(volumedata, "cube"),
                  volscheme: new $3Dmol.Gradient.Sinebow(2,0,1)
              },{});

          viewer.render();
          });
          viewer.zoomTo();
      });
     */
    export class Sinebow extends GradientType {
      gradient = "Sinebow";
      mult: number;
      max: number;
      min: number;
      constructor(min, max) {
        super();
        this.mult = 1.0;
        this.min = min;
        this.max = max;
        if (typeof max == "undefined" && Array.isArray(min) && min.length >= 2) {
          //we were passed a single range
          this.max = min[1];
          this.min = min[0];
        }
        Iif (max < min) {
          //reverse the order
          this.mult = -1.0;
          this.min *= -1.0;
          this.max *= -1.0;
        }
      };

      //map value to hex color, range is provided
      valueToHex(val, range) {
        var lo, hi;
        val = this.mult * val;
        if (range) {
          lo = range[0];
          hi = range[1];
        } else {
          lo = this.min;
          hi = this.max;
        }

        Iif (typeof val == "undefined") return 0xffffff;
        var norm = Gradient.normalizeValue(lo, hi, val);
        lo = norm.lo;
        hi = norm.hi;
        val = norm.val;

        var scale = (val - lo) / (hi - lo);
        var h = (5 * scale) / 6.0 + 0.5;
        var r = Math.sin(Math.PI * h);
        r *= r * 255;
        var g = Math.sin(Math.PI * (h + 1 / 3.0));
        g *= g * 255;
        var b = Math.sin(Math.PI * (h + 2 / 3.0));
        b *= b * 255;

        return (
          0x10000 * Math.floor(r) + 0x100 * Math.floor(b) + 0x1 * Math.floor(g)
        );
      };

      //return range used for color mapping, null if none set
      range() {
        if (typeof this.min != "undefined" && typeof this.max != "undefined") {
          return [this.min, this.max] as [number, number];
        }
        return null;
      };
    }

    //map from names to gradient constructors
    export const builtinGradients = {
      rwb: RWB,
      RWB: RWB,
      roygb: ROYGB,
      ROYGB: ROYGB,
      sinebow: Sinebow,
    };

    export class Gradient extends GradientType {
      static RWB = RWB;
      static ROYGB = ROYGB;
      static Sinebow = Sinebow;
      static builtinGradients = builtinGradients;
      static normalizeValue = normalizeValue;
      static getGradient = getGradient;
      // @ts-ignore
      valueToHex(_value: number, _range: number): number { }
      // @ts-ignore
      range(): [number, number] { }
    }