Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/legends.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {select} from "d3";
import {createContext} from "./context.js";
import {legendRamp} from "./legends/ramp.js";
import {isSymbolColorLegend, legendSwatches, legendSymbols} from "./legends/swatches.js";
import {inherit, isScaleOptions} from "./options.js";
import {getFilterId} from "./style.js";
import {normalizeScale} from "./scales.js";

const legendRegistry = new Map([
Expand Down Expand Up @@ -61,19 +59,11 @@ function legendOpacity({type, interpolate, ...scale}, {legend = true, color = "c
if (!interpolate) throw new Error(`${type} opacity scales are not supported`);
if (legend === true) legend = "ramp";
if (`${legend}`.toLowerCase() !== "ramp") throw new Error(`${legend} opacity legends are not supported`);
const node = legendColor({type, ...scale, interpolate: interpolateOpacity}, {legend, ...options});
if (!node) return;
const fid = getFilterId();
const svg = select(node);
svg.select("image").attr("filter", `url(#${fid})`);
const filter = svg.append("filter").attr("id", fid);
filter.append("feFlood").attr("flood-color", color);
filter.append("feComposite").attr("in2", "SourceGraphic").attr("operator", "in");
return node;
return legendColor({type, ...scale, interpolate: interpolateOpacity(color)}, {legend, ...options});
}

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

export function createLegends(scales, context, options) {
Expand Down
2 changes: 1 addition & 1 deletion src/legends/ramp.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function legendRamp(color, options) {
const canvas = context.document.createElement("canvas");
canvas.width = n;
canvas.height = 1;
const context2 = canvas.getContext("2d");
const context2 = canvas.getContext("2d", {colorSpace: "display-p3"}); // allow wide gamut
for (let i = 0, j = n - 1; i < n; ++i) {
context2.fillStyle = interpolator(i / j);
context2.fillRect(i, 0, 1, 1);
Expand Down
6 changes: 1 addition & 5 deletions test/output/opacityLegend.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 1 addition & 5 deletions test/output/opacityLegendCSS4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 1 addition & 5 deletions test/output/opacityLegendColor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 1 addition & 5 deletions test/output/opacityLegendLinear.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 1 addition & 5 deletions test/output/opacityLegendLog.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 1 addition & 5 deletions test/output/opacityLegendRange.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 1 addition & 5 deletions test/output/opacityLegendSqrt.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.