Skip to content

Commit

Permalink
fix(🎨): consolidate color processing in the scenegraph (#2891)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Jan 12, 2025
1 parent 6bc0508 commit 939ad0b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
18 changes: 17 additions & 1 deletion packages/skia/src/dom/nodes/datatypes/Gradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ export const transformOrigin = (origin: Vector, transform: Transforms3d) => {
];
};

export const processColor = (
Skia: Skia,
color: number | string | Float32Array | number[]
) => {
"worklet";
if (typeof color === "string" || typeof color === "number") {
return Skia.Color(color);
} else if (Array.isArray(color) || color instanceof Float32Array) {
return color instanceof Float32Array ? color : new Float32Array(color);
} else {
throw new Error(
`Invalid color type: ${typeof color}. Expected number, string, or array.`
);
}
};

export const processGradientProps = (
Skia: Skia,
{ colors, positions, mode, flags, ...transform }: GradientProps
Expand All @@ -24,7 +40,7 @@ export const processGradientProps = (
const localMatrix = Skia.Matrix();
processTransformProps(localMatrix, transform);
return {
colors: colors.map((color) => Skia.Color(color)),
colors: colors.map((color) => processColor(Skia, color)),
positions: positions ?? null,
mode: TileMode[enumKey(mode ?? "clamp")],
flags,
Expand Down
4 changes: 2 additions & 2 deletions packages/skia/src/sksg/Recorder/commands/Box.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deflate, inflate } from "../../../dom/nodes";
import { deflate, inflate, processColor } from "../../../dom/nodes";
import type { BoxProps, BoxShadowProps } from "../../../dom/types";
import { BlurStyle, ClipOp, isRRect } from "../../../skia/types";
import type { Command } from "../Core";
Expand Down Expand Up @@ -31,7 +31,7 @@ export const drawBox = (ctx: DrawingContext, command: BoxCommand) => {
.map((shadow) => {
const { color = "black", blur, spread = 0, dx = 0, dy = 0 } = shadow;
const lPaint = Skia.Paint();
lPaint.setColor(Skia.Color(color));
lPaint.setColor(processColor(Skia, color));
lPaint.setAlphaf(paint.getAlphaf() * opacity);
lPaint.setMaskFilter(
Skia.MaskFilter.MakeBlur(BlurStyle.Normal, blur, true)
Expand Down
4 changes: 2 additions & 2 deletions packages/skia/src/sksg/Recorder/commands/ColorFilters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { enumKey } from "../../../dom/nodes";
import { enumKey, processColor } from "../../../dom/nodes";
import type {
BlendColorFilterProps,
LerpColorFilterProps,
Expand Down Expand Up @@ -59,7 +59,7 @@ export const pushColorFilter = (
if (isColorFilter(command, NodeType.BlendColorFilter)) {
const { props } = command;
const { mode } = props;
const color = ctx.Skia.Color(props.color);
const color = processColor(ctx.Skia, props.color);
cf = ctx.Skia.ColorFilter.MakeBlend(color, BlendMode[enumKey(mode)]);
} else if (isColorFilter(command, NodeType.MatrixColorFilter)) {
const { matrix } = command.props;
Expand Down
9 changes: 5 additions & 4 deletions packages/skia/src/sksg/Recorder/commands/Drawing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
inflate,
NodeType,
processCircle,
processColor,
processPath,
processRect,
processRRect,
Expand Down Expand Up @@ -87,7 +88,7 @@ export const drawBox = (
.map((shadow) => {
const { color = "black", blur, spread = 0, dx = 0, dy = 0 } = shadow;
const lPaint = Skia.Paint();
lPaint.setColor(Skia.Color(color));
lPaint.setColor(processColor(Skia, color));
lPaint.setAlphaf(paint.getAlphaf() * opacity);
lPaint.setMaskFilter(
Skia.MaskFilter.MakeBlur(BlurStyle.Normal, blur, true)
Expand All @@ -105,7 +106,7 @@ export const drawBox = (
canvas.save();
canvas.clipRRect(box, ClipOp.Intersect, false);
const lPaint = Skia.Paint();
lPaint.setColor(Skia.Color(color));
lPaint.setColor(processColor(Skia, color));
lPaint.setAlphaf(paint.getAlphaf() * opacity);

lPaint.setMaskFilter(
Expand Down Expand Up @@ -170,7 +171,7 @@ export const drawVertices = (ctx: DrawingContext, props: VerticesProps) => {
vertexMode,
props.vertices,
textures,
colors ? colors.map((c) => ctx.Skia.Color(c)) : undefined,
colors ? colors.map((c) => processColor(ctx.Skia, c)) : undefined,
indices
);
const defaultBlendMode = colors ? BlendMode.DstOver : BlendMode.SrcOver;
Expand Down Expand Up @@ -258,7 +259,7 @@ export const drawPatch = (ctx: DrawingContext, props: PatchProps) => {
patch[0].c1,
];
const colors = props.colors
? props.colors.map((c) => ctx.Skia.Color(c))
? props.colors.map((c) => processColor(ctx.Skia, c))
: undefined;
ctx.canvas.drawPatch(points, colors, texture, mode, ctx.paint);
};
Expand Down
4 changes: 2 additions & 2 deletions packages/skia/src/sksg/Recorder/commands/ImageFilters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { enumKey, processRadius } from "../../../dom/nodes";
import { enumKey, processColor, processRadius } from "../../../dom/nodes";
import type {
BlendImageFilterProps,
BlurImageFilterProps,
Expand Down Expand Up @@ -113,7 +113,7 @@ const declareDropShadowImageFilter = (
) => {
"worklet";
const { dx, dy, blur, shadowOnly, color: cl, inner } = props;
const color = ctx.Skia.Color(cl);
const color = processColor(ctx.Skia, cl);
let factory;
if (inner) {
factory = MakeInnerShadow.bind(null, ctx.Skia, shadowOnly);
Expand Down
18 changes: 1 addition & 17 deletions packages/skia/src/sksg/Recorder/commands/Paint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { enumKey } from "../../../dom/nodes";
import { enumKey, processColor } from "../../../dom/nodes";
import type { PaintProps } from "../../../dom/types";
import {
BlendMode,
Expand All @@ -8,22 +8,6 @@ import {
} from "../../../skia/types";
import type { SkPaint, Skia } from "../../../skia/types";

export const processColor = (
Skia: Skia,
color: number | string | Float32Array | number[]
) => {
"worklet";
if (typeof color === "string" || typeof color === "number") {
return Skia.Color(color);
} else if (Array.isArray(color) || color instanceof Float32Array) {
return color instanceof Float32Array ? color : new Float32Array(color);
} else {
throw new Error(
`Invalid color type: ${typeof color}. Expected number, string, or array.`
);
}
};

export const setPaintProperties = (
Skia: Skia,
paint: SkPaint,
Expand Down
3 changes: 2 additions & 1 deletion packages/skia/src/sksg/Recorder/commands/Shaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
enumKey,
fitRects,
getRect,
processColor,
processGradientProps,
processTransformProps,
rect2rect,
Expand Down Expand Up @@ -49,7 +50,7 @@ const declareShader = (ctx: DrawingContext, props: ShaderProps) => {
const declareColorShader = (ctx: DrawingContext, props: ColorProps) => {
"worklet";
const { color } = props;
const shader = ctx.Skia.Shader.MakeColor(ctx.Skia.Color(color));
const shader = ctx.Skia.Shader.MakeColor(processColor(ctx.Skia, color));
ctx.shaders.push(shader);
};

Expand Down

0 comments on commit 939ad0b

Please sign in to comment.