/
Gradient.ts
405 lines (366 loc) · 10.8 KB
/
Gradient.ts
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import { CC, Color } from "./colors";
export abstract class GradientType {
gradient?: string;
abstract valueToHex(value: number, range?: number[]): number;
abstract range(): 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): GradientType {
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) {
if (grad.colors === undefined) {
return new builtinGradients[grad.gradient](min, max);
} else {
return new builtinGradients[grad.gradient](min, max, grad.colors);
}
} 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;
}
if (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 if (!!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;
}
if (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];
}
if (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;
}
if (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;
};
}
/**
* Custom linear gradient using user supplied colors.
* Reverse gradients are supported when min>max so that the colors are displayed in reverse order.
* Midpoints are not supported - color map should be specified to get desired middle color.
*
* @param {number} min
* @param {number} max
* @param {Array} colors Array of colors that will be linearly interpolated between from min to max values.
* @subcategory Gradients
*
* @example
$3Dmol.get('../test_structs/af.pdb', function(data){
viewer.addModel(data);
viewer.setStyle({cartoon:{colorscheme:{prop: 'b', gradient:'linear', min: 70, max: 100, colors: ["blue","yellow","green"]}}});
viewer.zoomTo();
viewer.render();
});
*/
export class CustomLinear extends GradientType {
gradient = "linear";
min: number;
max: number;
colors = new Array<Color>();
constructor(min: any, max: any, colors?: any) {
super();
var carr: Array<any>;
if (Array.isArray(min) && min.length >= 2) {
//we were passed a single range
this.max = min[1] as number;
this.min = min[0] as number;
carr = max;
} else {
this.min = min as number;
this.max = max as number;
carr = colors;
}
//convert colors
if (carr) {
for (let c of carr) {
this.colors.push(CC.color(c));
}
} else {
this.colors.push(CC.color(0));
}
}
//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;
if (range) {
lo = range[0];
hi = range[1];
} else {
lo = this.min;
hi = this.max;
}
if (val === undefined) return 0xffffff;
var norm = normalizeValue(lo, hi, val);
lo = norm.lo;
hi = norm.hi;
val = norm.val;
let nsteps = this.colors.length;
let stepsize = (hi - lo) / nsteps;
let startpos = Math.min(Math.floor((val - lo) / stepsize), nsteps - 1);
let endpos = Math.min(startpos + 1, nsteps - 1);
let frac = (val - lo - (startpos * stepsize)) / stepsize;
let startcol = this.colors[startpos];
let endcol = this.colors[endpos];
let col = new Color(startcol.r + frac * (endcol.r - startcol.r),
startcol.g + frac * (endcol.g - startcol.g),
startcol.b + frac * (endcol.b - startcol.b));
return col.getHex();
}
}
//map from names to gradient constructors
export const builtinGradients = {
rwb: RWB,
RWB: RWB,
roygb: ROYGB,
ROYGB: ROYGB,
sinebow: Sinebow,
linear: CustomLinear
};
export class Gradient extends GradientType {
static RWB = RWB;
static ROYGB = ROYGB;
static Sinebow = Sinebow;
static CustomLinear = CustomLinear;
static builtinGradients = builtinGradients;
static normalizeValue = normalizeValue;
static getGradient = getGradient;
valueToHex(_value: number, _range?: number[]): number { return 0; }
range(): [number, number] | null { return null; }
}