Skip to content

Commit

Permalink
Merge pull request #75 from d3/two
Browse files Browse the repository at this point in the history
two
  • Loading branch information
Fil committed Aug 19, 2020
2 parents e87dc62 + 0ecd740 commit 7de7354
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -37,6 +37,8 @@ In addition to the ubiquitous and machine-friendly [RGB](#rgb) and [HSL](#hsl) c

Cubehelix features monotonic lightness, while CIELAB and its polar form CIELCh<sub>ab</sub> are perceptually uniform.

## Extensions

For additional color spaces, see:

* [d3-cam16](https://github.com/d3/d3-cam16)
Expand All @@ -45,12 +47,16 @@ For additional color spaces, see:
* [d3-hcg](https://github.com/d3/d3-hcg)
* [d3-hsluv](https://github.com/petulla/d3-hsluv)

To measure color differences, see:

* [d3-color-difference](https://github.com/Evercoder/d3-color-difference)

## Installing

If you use NPM, `npm install d3-color`. Otherwise, download the [latest release](https://github.com/d3/d3-color/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-color.v1.min.js) or as part of [D3](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported:
If you use NPM, `npm install d3-color`. Otherwise, download the [latest release](https://github.com/d3/d3-color/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-color.v2.min.js) or as part of [D3](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported:

```html
<script src="https://d3js.org/d3-color.v1.min.js"></script>
<script src="https://d3js.org/d3-color.v2.min.js"></script>
<script>
var steelblue = d3.rgb("steelblue");
Expand Down
5 changes: 4 additions & 1 deletion package.json
@@ -1,6 +1,9 @@
{
"name": "d3-color",
"version": "1.4.1",
"version": "2.0.0-rc.1",
"publishConfig": {
"tag": "next"
},
"description": "Color spaces! RGB, HSL, Cubehelix, Lab and HCL (Lch).",
"keywords": [
"d3",
Expand Down
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
8 changes: 4 additions & 4 deletions src/lab.js
@@ -1,9 +1,9 @@
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
var K = 18,
const K = 18,
Xn = 0.96422,
Yn = 1,
Zn = 0.82521,
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 7de7354

Please sign in to comment.