Skip to content

Commit

Permalink
normalize "degrees" and "radians" for deg2rad conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Jun 2, 2020
1 parent 693572b commit c1b93f1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/cubehelix.js
@@ -1,6 +1,6 @@
import define, {extend} from "./define.js";
import {Color, rgbConvert, Rgb, darker, brighter} from "./color.js";
import {deg2rad, rad2deg} from "./math.js";
import {degrees, radians} from "./math.js";

var A = -0.14861,
B = +1.78277,
Expand All @@ -21,7 +21,7 @@ function cubehelixConvert(o) {
bl = b - l,
k = (E * (g - l) - C * bl) / D,
s = Math.sqrt(k * k + bl * bl) / (E * l * (1 - l)), // NaN if l=0 or l=1
h = s ? Math.atan2(k, bl) * rad2deg - 120 : NaN;
h = s ? Math.atan2(k, bl) * degrees - 120 : NaN;
return new Cubehelix(h < 0 ? h + 360 : h, s, l, o.opacity);
}

Expand All @@ -46,7 +46,7 @@ define(Cubehelix, cubehelix, extend(Color, {
return new Cubehelix(this.h, this.s, this.l * k, this.opacity);
},
rgb: function() {
var h = isNaN(this.h) ? 0 : (this.h + 120) * deg2rad,
var h = isNaN(this.h) ? 0 : (this.h + 120) * radians,
l = +this.l,
a = isNaN(this.s) ? 0 : this.s * l * (1 - l),
cosh = Math.cos(h),
Expand Down
6 changes: 3 additions & 3 deletions src/lab.js
@@ -1,6 +1,6 @@
import define, {extend} from "./define.js";
import {Color, rgbConvert, Rgb} from "./color.js";
import {deg2rad, rad2deg} from "./math.js";
import {degrees, radians} from "./math.js";

// https://observablehq.com/@mbostock/lab-and-rgb
const K = 18,
Expand Down Expand Up @@ -85,7 +85,7 @@ function hclConvert(o) {
if (o instanceof Hcl) return new Hcl(o.h, o.c, o.l, o.opacity);
if (!(o instanceof Lab)) o = labConvert(o);
if (o.a === 0 && o.b === 0) return new Hcl(NaN, 0 < o.l && o.l < 100 ? 0 : NaN, o.l, o.opacity);
var h = Math.atan2(o.b, o.a) * rad2deg;
var h = Math.atan2(o.b, o.a) * degrees;
return new Hcl(h < 0 ? h + 360 : h, Math.sqrt(o.a * o.a + o.b * o.b), o.l, o.opacity);
}

Expand All @@ -106,7 +106,7 @@ export function Hcl(h, c, l, opacity) {

function hcl2lab(o) {
if (isNaN(o.h)) return new Lab(o.l, 0, 0, o.opacity);
var h = o.h * deg2rad;
var h = o.h * radians;
return new Lab(o.l, Math.cos(h) * o.c, Math.sin(h) * o.c, o.opacity);
}

Expand Down
4 changes: 2 additions & 2 deletions src/math.js
@@ -1,2 +1,2 @@
export var deg2rad = Math.PI / 180;
export var rad2deg = 180 / Math.PI;
export const radians = Math.PI / 180;
export const degrees = 180 / Math.PI;

0 comments on commit c1b93f1

Please sign in to comment.