Skip to content

Commit 743ffc9

Browse files
committed
support opacity in literal colors
1 parent d84d5bf commit 743ffc9

4 files changed

Lines changed: 416 additions & 413 deletions

File tree

src/marks/raster.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ export class Raster extends AbstractRaster {
137137
const context2d = canvas.getContext("2d", {colorSpace: this.colorSpace});
138138
const image = context2d.createImageData(w, h);
139139
const imageData = image.data;
140-
let {r, g, b} = colorBytes(this.fill) ?? {r: 0, g: 0, b: 0};
141-
let a = (this.fillOpacity ?? 1) * 255;
140+
const fo = this.fillOpacity ?? 1;
141+
let {r, g, b, opacity: co = 1} = colorBytes(this.fill) ?? {r: 0, g: 0, b: 0};
142+
let a = co * fo * 255;
142143
for (let i = 0; i < n; ++i) {
143144
const j = i << 2;
144145
if (F) {
@@ -147,9 +148,10 @@ export class Raster extends AbstractRaster {
147148
imageData[j + 3] = 0;
148149
continue;
149150
}
150-
({r, g, b} = colorBytes(fi));
151+
({r, g, b, opacity: co = 1} = colorBytes(fi));
152+
if (!FO) a = co * fo * 255;
151153
}
152-
if (FO) a = FO[i + offset] * 255;
154+
if (FO) a = co * FO[i + offset] * 255;
153155
imageData[j + 0] = r;
154156
imageData[j + 1] = g;
155157
imageData[j + 2] = b;
@@ -520,9 +522,10 @@ export function converter(colorSpace) {
520522
context.fillStyle = c;
521523
context.clearRect(0, 0, 1, 1);
522524
context.fillRect(0, 0, 1, 1);
523-
const [r, g, b] = context.getImageData(0, 0, 1, 1).data;
524-
if (mem.size < 256) mem.set(c, {r, g, b});
525-
return {r, g, b};
525+
const [r, g, b, a] = context.getImageData(0, 0, 1, 1).data;
526+
const color = {r, g, b, opacity: a / 255};
527+
if (mem.size < 256) mem.set(c, color);
528+
return color;
526529
};
527530
let p;
528531
return colorSpace === "srgb" ? (c) => (isNaN((p = rgb(c)).opacity) ? canvasConverter(c) : p) : canvasConverter;

0 commit comments

Comments
 (0)