Skip to content

Commit dff4583

Browse files
authored
fix ramp legend colorSpace (#2419)
* fix opacity legend colorSpace * promote colorSpace to RampLegendOptions * default to display-p3 * remove colorSpace option * remove defunct colorSpace option
1 parent 6ac9e7f commit dff4583

9 files changed

Lines changed: 11 additions & 49 deletions

src/legends.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import {select} from "d3";
21
import {createContext} from "./context.js";
32
import {legendRamp} from "./legends/ramp.js";
43
import {isSymbolColorLegend, legendSwatches, legendSymbols} from "./legends/swatches.js";
54
import {inherit, isScaleOptions} from "./options.js";
6-
import {getFilterId} from "./style.js";
75
import {normalizeScale} from "./scales.js";
86

97
const legendRegistry = new Map([
@@ -61,19 +59,11 @@ function legendOpacity({type, interpolate, ...scale}, {legend = true, color = "c
6159
if (!interpolate) throw new Error(`${type} opacity scales are not supported`);
6260
if (legend === true) legend = "ramp";
6361
if (`${legend}`.toLowerCase() !== "ramp") throw new Error(`${legend} opacity legends are not supported`);
64-
const node = legendColor({type, ...scale, interpolate: interpolateOpacity}, {legend, ...options});
65-
if (!node) return;
66-
const fid = getFilterId();
67-
const svg = select(node);
68-
svg.select("image").attr("filter", `url(#${fid})`);
69-
const filter = svg.append("filter").attr("id", fid);
70-
filter.append("feFlood").attr("flood-color", color);
71-
filter.append("feComposite").attr("in2", "SourceGraphic").attr("operator", "in");
72-
return node;
62+
return legendColor({type, ...scale, interpolate: interpolateOpacity(color)}, {legend, ...options});
7363
}
7464

75-
function interpolateOpacity(t) {
76-
return `rgba(0,0,0,${t})`;
65+
function interpolateOpacity(color) {
66+
return (t) => `color-mix(in srgb, transparent, ${color} ${(t * 100).toFixed(1)}%)`;
7767
}
7868

7969
export function createLegends(scales, context, options) {

src/legends/ramp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function legendRamp(color, options) {
9090
const canvas = context.document.createElement("canvas");
9191
canvas.width = n;
9292
canvas.height = 1;
93-
const context2 = canvas.getContext("2d");
93+
const context2 = canvas.getContext("2d", {colorSpace: "display-p3"}); // allow wide gamut
9494
for (let i = 0, j = n - 1; i < n; ++i) {
9595
context2.fillStyle = interpolator(i / j);
9696
context2.fillRect(i, 0, 1, 1);

test/output/opacityLegend.svg

Lines changed: 1 addition & 5 deletions
Loading

test/output/opacityLegendCSS4.svg

Lines changed: 1 addition & 5 deletions
Loading

test/output/opacityLegendColor.svg

Lines changed: 1 addition & 5 deletions
Loading

test/output/opacityLegendLinear.svg

Lines changed: 1 addition & 5 deletions
Loading

test/output/opacityLegendLog.svg

Lines changed: 1 addition & 5 deletions
Loading

test/output/opacityLegendRange.svg

Lines changed: 1 addition & 5 deletions
Loading

test/output/opacityLegendSqrt.svg

Lines changed: 1 addition & 5 deletions
Loading

0 commit comments

Comments
 (0)