forked from nicgirault/circosJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.js
112 lines (107 loc) · 2.49 KB
/
colors.js
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
import isFunction from 'lodash/isFunction'
import {scaleLog, scaleSequential} from 'd3-scale'
import {
interpolateBrBG,
interpolatePRGn,
interpolatePiYG,
interpolatePuOr,
interpolateRdBu,
interpolateRdGy,
interpolateRdYlBu,
interpolateRdYlGn,
interpolateSpectral,
interpolateBlues,
interpolateGreens,
interpolateGreys,
interpolateOranges,
interpolatePurples,
interpolateReds,
interpolateBuGn,
interpolateBuPu,
interpolateGnBu,
interpolateOrRd,
interpolatePuBuGn,
interpolatePuBu,
interpolatePuRd,
interpolateRdPu,
interpolateYlGnBu,
interpolateYlGn,
interpolateYlOrBr,
interpolateYlOrRd
} from 'd3-scale-chromatic'
const palettes = {
BrBG: interpolateBrBG,
PRGn: interpolatePRGn,
PiYG: interpolatePiYG,
PuOr: interpolatePuOr,
RdBu: interpolateRdBu,
RdGy: interpolateRdGy,
RdYlBu: interpolateRdYlBu,
RdYlGn: interpolateRdYlGn,
Spectral: interpolateSpectral,
Blues: interpolateBlues,
Greens: interpolateGreens,
Greys: interpolateGreys,
Oranges: interpolateOranges,
Purples: interpolatePurples,
Reds: interpolateReds,
BuGn: interpolateBuGn,
BuPu: interpolateBuPu,
GnBu: interpolateGnBu,
OrRd: interpolateOrRd,
PuBuGn: interpolatePuBuGn,
PuBu: interpolatePuBu,
PuRd: interpolatePuRd,
RdPu: interpolateRdPu,
YlGnBu: interpolateYlGnBu,
YlGn: interpolateYlGn,
YlOrBr: interpolateYlOrBr,
YlOrRd: interpolateYlOrRd
}
export function buildColorValue (
color,
min = null,
max = null,
logScale = false,
logScaleBase = Math.E
) {
if (isFunction(color)) {
return color
}
const reverse = color[0] === '-'
const paletteName = color[0] === '-' ? color.slice(1) : color
if (palettes[paletteName]) {
const scale = buildColorScale(
palettes[paletteName], min, max, reverse, logScale, logScaleBase
)
return (d) => {
return scale(d.value)
}
}
return color
}
const buildColorScale = (
interpolator,
min,
max,
reverse = false,
logScale = false,
logScaleBase = Math.E
) => {
if (logScale && min * max <= 0) {
console.warn(`As log(0) = -∞, a log scale domain must be
strictly-positive or strictly-negative. logscale ignored`
)
}
if (logScale && min * max > 0) {
const scale = scaleLog()
.base(logScaleBase)
.domain(reverse ? [max, min] : [min, max])
.range([0, 1])
return scaleSequential((t) => {
return interpolator(scale(t))
}).domain([0, 1])
}
return scaleSequential(interpolator)
.domain(reverse ? [max, min] : [min, max])
}