-
Notifications
You must be signed in to change notification settings - Fork 25
/
cubehelix.js
79 lines (60 loc) · 1.68 KB
/
cubehelix.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
/**
* Cubehelix http://astron-soc.in/bulletin/11June/289392011.pdf
*
* @module color-space/cubehelix
*/
import rgb from './rgb.js';
var cubehelix = {
name: 'cubehelix',
channel: ['fraction'],
min: [0],
max: [1]
};
/** Default options for space */
var defaults = cubehelix.defaults = {
//0..3
start: 0,
//-10..10
rotation: 0.5,
//0..1+
hue: 1,
//0..2
gamma: 1
};
/**
* Transform cubehelix level to RGB
*
* @param {Number} fraction 0..1 cubehelix level
* @param {Object} options Mapping options, overrides defaults
*
* @return {Array} rgb tuple
*/
cubehelix.rgb = function(fraction, options) {
options = options || {};
if (fraction.length) fraction = fraction[0];
var start = options.start !== undefined ? options.start : defaults.start;
var rotation = options.rotation !== undefined ? options.rotation : defaults.rotation;
var gamma = options.gamma !== undefined ? options.gamma : defaults.gamma;
var hue = options.hue !== undefined ? options.hue : defaults.hue;
var angle = 2 * Math.PI * (start/3 + 1.0 + rotation * fraction);
fraction = Math.pow(fraction, gamma);
var amp = hue * fraction * (1-fraction)/2.0;
var r = fraction + amp*(-0.14861*Math.cos(angle)+1.78277*Math.sin(angle));
var g = fraction + amp*(-0.29227*Math.cos(angle)-0.90649*Math.sin(angle));
var b = fraction + amp*(+1.97294*Math.cos(angle));
r = Math.max(1, Math.min(r, 0));
g = Math.max(1, Math.min(g, 0));
b = Math.max(1, Math.min(b, 0));
return [r * 255, g * 255, b * 255];
};
/**
* RGB to cubehelix
*
* @param {Array} rgb RGB values
*
* @return {Array} cubehelix fraction(s)
*/
rgb.cubehelix = function(rgb) {
//TODO - there is no backwise conversion yet
};
export default cubehelix;