comparison src/app/spectrogram/ColourMap.ts @ 343:8bfd9586c78a

Move some functions out of waveform and into appropriately named modules.
author Lucas Thompson <dev@lucas.im>
date Tue, 23 May 2017 10:41:36 +0100
parents
children
comparison
equal deleted inserted replaced
342:b5f2ee789fb3 343:8bfd9586c78a
1 /**
2 * Created by lucast on 23/05/2017.
3 */
4
5 export function interpolatingMapper(hexColours) {
6 const colours = hexColours.map(n => {
7 const i = parseInt(n, 16);
8 return [ ((i >> 16) & 255) / 255.0,
9 ((i >> 8) & 255) / 255.0,
10 ((i) & 255) / 255.0 ];
11 });
12 const last = colours.length - 1;
13 return (value => {
14 const m = value * last;
15 if (m >= last) {
16 return colours[last];
17 }
18 if (m <= 0) {
19 return colours[0];
20 }
21 const base = Math.floor(m);
22 const prop0 = base + 1.0 - m;
23 const prop1 = m - base;
24 const c0 = colours[base];
25 const c1 = colours[base + 1];
26 return [ c0[0] * prop0 + c1[0] * prop1,
27 c0[1] * prop0 + c1[1] * prop1,
28 c0[2] * prop0 + c1[2] * prop1 ];
29 });
30 }
31
32 export function iceMapper() {
33 const hexColours = [
34 // Based on ColorBrewer ylGnBu
35 'ffffff', 'ffff00', 'f7fcf0', 'e0f3db', 'ccebc5', 'a8ddb5',
36 '7bccc4', '4eb3d3', '2b8cbe', '0868ac', '084081', '042040'
37 ];
38 hexColours.reverse();
39 return interpolatingMapper(hexColours);
40 }
41
42 export function greenMapper() {
43 const blue = 0.6666;
44 const pieslice = 0.3333;
45 return (value => {
46 const h = blue - value * 2.0 * pieslice;
47 const s = 0.5 + value / 2.0;
48 const v = value;
49 return this.hsv2rgb(h, s, v);
50 });
51 }
52
53 export function sunsetMapper() {
54 return (value => {
55 const r = (value - 0.24) * 2.38;
56 const g = (value - 0.64) * 2.777;
57 let b = (3.6 * value);
58 if (value > 0.277) {
59 b = 2.0 - b;
60 }
61 return [ r, g, b ];
62 });
63 }
64
65 export function hsv2rgb(h, s, v) { // all values in range [0, 1]
66 const i = Math.floor(h * 6);
67 const f = h * 6 - i;
68 const p = v * (1 - s);
69 const q = v * (1 - f * s);
70 const t = v * (1 - (1 - f) * s);
71 let r = 0, g = 0, b = 0;
72 switch (i % 6) {
73 case 0: r = v; g = t; b = p; break;
74 case 1: r = q; g = v; b = p; break;
75 case 2: r = p; g = v; b = t; break;
76 case 3: r = p; g = q; b = v; break;
77 case 4: r = t; g = p; b = v; break;
78 case 5: r = v; g = p; b = q; break;
79 }
80 return [ r, g, b ];
81 }