| 1 | import define, {extend} from "./define.js";
|
|---|
| 2 | import {Color, rgbConvert, Rgb, darker, brighter} from "./color.js";
|
|---|
| 3 | import {degrees, radians} from "./math.js";
|
|---|
| 4 |
|
|---|
| 5 | var 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 |
|
|---|
| 14 | function 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 |
|
|---|
| 28 | export 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 |
|
|---|
| 32 | export function Cubehelix(h, s, l, opacity) {
|
|---|
| 33 | this.h = +h;
|
|---|
| 34 | this.s = +s;
|
|---|
| 35 | this.l = +l;
|
|---|
| 36 | this.opacity = +opacity;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | define(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 | }));
|
|---|