source: node_modules/d3-color/src/cubehelix.js

Last change on this file was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 1.8 KB
Line 
1import define, {extend} from "./define.js";
2import {Color, rgbConvert, Rgb, darker, brighter} from "./color.js";
3import {degrees, radians} from "./math.js";
4
5var A = -0.14861,
6 B = +1.78277,
7 C = -0.29227,
8 D = -0.90649,
9 E = +1.97294,
10 ED = E * D,
11 EB = E * B,
12 BC_DA = B * C - D * A;
13
14function cubehelixConvert(o) {
15 if (o instanceof Cubehelix) return new Cubehelix(o.h, o.s, o.l, o.opacity);
16 if (!(o instanceof Rgb)) o = rgbConvert(o);
17 var r = o.r / 255,
18 g = o.g / 255,
19 b = o.b / 255,
20 l = (BC_DA * b + ED * r - EB * g) / (BC_DA + ED - EB),
21 bl = b - l,
22 k = (E * (g - l) - C * bl) / D,
23 s = Math.sqrt(k * k + bl * bl) / (E * l * (1 - l)), // NaN if l=0 or l=1
24 h = s ? Math.atan2(k, bl) * degrees - 120 : NaN;
25 return new Cubehelix(h < 0 ? h + 360 : h, s, l, o.opacity);
26}
27
28export default function cubehelix(h, s, l, opacity) {
29 return arguments.length === 1 ? cubehelixConvert(h) : new Cubehelix(h, s, l, opacity == null ? 1 : opacity);
30}
31
32export function Cubehelix(h, s, l, opacity) {
33 this.h = +h;
34 this.s = +s;
35 this.l = +l;
36 this.opacity = +opacity;
37}
38
39define(Cubehelix, cubehelix, extend(Color, {
40 brighter(k) {
41 k = k == null ? brighter : Math.pow(brighter, k);
42 return new Cubehelix(this.h, this.s, this.l * k, this.opacity);
43 },
44 darker(k) {
45 k = k == null ? darker : Math.pow(darker, k);
46 return new Cubehelix(this.h, this.s, this.l * k, this.opacity);
47 },
48 rgb() {
49 var h = isNaN(this.h) ? 0 : (this.h + 120) * radians,
50 l = +this.l,
51 a = isNaN(this.s) ? 0 : this.s * l * (1 - l),
52 cosh = Math.cos(h),
53 sinh = Math.sin(h);
54 return new Rgb(
55 255 * (l + a * (A * cosh + B * sinh)),
56 255 * (l + a * (C * cosh + D * sinh)),
57 255 * (l + a * (E * cosh)),
58 this.opacity
59 );
60 }
61}));
Note: See TracBrowser for help on using the repository browser.