Skip to content

Commit

Permalink
raster colorSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Aug 21, 2024
1 parent e50254d commit 38cf4a8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/marks/raster.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,12 @@ If **width** is specified, **x1** defaults to 0 and **x2** defaults to **width**

The following raster-specific constant options are supported:

* **colorSpace** - the [color space](https://developer.mozilla.org/en-US/docs/Web/API/ImageData/colorSpace) <VersionBadge pr="2143" />
* **interpolate** - the [spatial interpolation method](#spatial-interpolators)
* **imageRendering** - the [image-rendering attribute](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/image-rendering); defaults to *auto* (bilinear)
* **blur** - a non-negative pixel radius for smoothing; defaults to 0

The **imageRendering** option may be set to *pixelated* for a sharper image. The **interpolate** option is ignored when **fill** or **fillOpacity** is a function of *x* and *y*.
The **colorSpace** may be set to *display-p3* for the Display P3 color space. The **imageRendering** option may be set to *pixelated* for a sharper image. The **interpolate** option is ignored when **fill** or **fillOpacity** is a function of *x* and *y*.

## raster(*data*, *options*) {#raster}

Expand Down
8 changes: 8 additions & 0 deletions src/marks/raster.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ export interface RasterOptions extends Omit<MarkOptions, "fill" | "fillOpacity">
*/
imageRendering?: string;

/**
* The [color space][1] of the backing canvas. Defaults to *srgb*; set to
* *display-p3* for the Display P3 color space.
*
* [1]: https://developer.mozilla.org/en-US/docs/Web/API/ImageData/colorSpace
*/
colorSpace?: "srgb" | "display-p3" | string;

/**
* The fill, typically bound to the *color* scale. Can be specified as a
* constant, a channel based on the sample *data*, or as a function *f*(*x*,
Expand Down
4 changes: 3 additions & 1 deletion src/marks/raster.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class AbstractRaster extends Mark {
y1 = y == null ? 0 : undefined,
x2 = x == null ? width : undefined,
y2 = y == null ? height : undefined,
colorSpace = "srgb",
pixelSize = defaults.pixelSize,
blur = 0,
interpolate
Expand Down Expand Up @@ -79,6 +80,7 @@ export class AbstractRaster extends Mark {
this.pixelSize = number(pixelSize, "pixelSize");
this.blur = number(blur, "blur");
this.interpolate = x == null || y == null ? null : maybeInterpolate(interpolate); // interpolation requires x & y
this.colorSpace = String(colorSpace);
}
}

Expand Down Expand Up @@ -129,7 +131,7 @@ export class Raster extends AbstractRaster {
const canvas = document.createElement("canvas");
canvas.width = w;
canvas.height = h;
const context2d = canvas.getContext("2d");
const context2d = canvas.getContext("2d", {colorSpace: this.colorSpace});
const image = context2d.createImageData(w, h);
const imageData = image.data;
let {r, g, b} = rgb(this.fill) ?? {r: 0, g: 0, b: 0};
Expand Down

0 comments on commit 38cf4a8

Please sign in to comment.