Skip to content

fix(🎨): honor the source alpha in plusLighter and plusDarker - #4337

Merged
wcandillon merged 5 commits into
wcandillon:mainfrom
LESANF:fix/plus-blend-source-alpha
Oct 27, 2026
Merged

wcandillon merged 5 commits into
wcandillon:mainfrom
LESANF:fix/plus-blend-source-alpha

Conversation

@LESANF

@LESANF LESANF commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review follow-up

After the latest review:

Oslo at opacity t, then Zurich at 1 - t with plusDarker, over a transparent canvas (the checkerboard). Left to right: Core Graphics, 3.8.0, the union alpha this PR had, and this change. Core Graphics and this change stay opaque through the whole fade; the other two dip to alpha 0.75 at t = 0.5. This change matches Core Graphics within 1/255 in every frame. Rendered with CanvasKit, which runs the same SkSL as native.

crossfade-darker

After the first review:

  • plusLighter is drawn with kPlus on native and web, and its runtime blender is gone. The result is the same.
  • The test has the translucent-destination case, with 0.6 instead of 0.5. Native's color conversion (SkColorSetARGB(a * 255, …)) truncates 0.5 to 127/255, so two 50% whites add up to 254 on a Galaxy Z Fold6, while CanvasKit, like Skia's SkColor4f::toSkColor(), rounds to 128/255 and gets 255. 0.6 is 153/255 exactly everywhere. I can send a separate PR that rounds in the native conversion, after which 0.5 works too.
  • The comments give both formulas with their alpha.

Oslo at opacity t, then Zurich at 1 - t with plusLighter, over a transparent canvas (the checkerboard). Left to right: CSS plus-lighter in Chrome, 3.8.0, the union alpha sa + da - sa * da from the first review, and this PR. CSS and this PR stay opaque through the whole fade; the other two dip to alpha 0.75 at t = 0.5. This PR matches Chrome within 2/255 in every frame. Rendered with CanvasKit, which draws plusLighter with the same Plus mode as native.

crossfade

Before / after

Alpha 0 → 1 → 0. Left to right: SwiftUI, 3.8.0, this PR (top plusLighter, bottom plusDarker).

fade-sweep fade-chart

Problem

plusLighter and plusDarker ignore the alpha of what is drawn. Over an opaque backdrop, a white glow at 5% opacity is as bright with plusLighter as an opaque one, so it can't fade out. A black shape with plusDarker turns the backdrop black even at alpha 0.

Measured on an iPhone 15 Pro (iOS 26.5) against SwiftUI's .plusLighter / .plusDarker on the same device. The source is white over a 51 grey for plusLighter, and black over a 153 grey for plusDarker:

source alpha 0 0.05 0.25 0.5 0.75 1
plusLighter, SwiftUI 51 64 114 178 242 255
plusLighter, 3.8.0 51 255 255 255 255 255
plusLighter, this PR 51 63 114 178 242 255
plusDarker, SwiftUI 153 140 89 25 0 0
plusDarker, 3.8.0 0 0 0 0 0 0
plusDarker, this PR 153 141 90 26 0 0

The ±1 left comes from the native color conversion, which truncates the alpha to 8 bits (SkColorSetARGB(a * 255, …)): 0.05 becomes 12/255.

No issue reports it; I searched the tracker for plusLighter, plusDarker and "blend mode alpha".

Root cause

The blenders in cpp/api/CustomBlendModes.h, mirrored in src/skia/web/JsiSkPaint.ts, un-premultiply both colors before combining them:

vec3 srcUnpremul = src.a > 0.0 ? src.rgb / src.a : vec3(0.0);
vec3 dstUnpremul = dst.a > 0.0 ? dst.rgb / dst.a : vec3(0.0);
vec3 blended = min(vec3(1.0), srcUnpremul + dstUnpremul); // plusDarker: max(0, src + dst - 1)
return vec4(blended * outAlpha, outAlpha);

Dividing by src.a turns a 5% white into full white. Over an opaque backdrop outAlpha is 1, so the alpha never comes back. At alpha 0, plusDarker computes max(0, 0 + dst - 1), which is black. Both also composite the alpha like source-over (sa + da - sa * da), while CSS and Core Graphics add it. CSS plus-lighter and the iOS modes the BlendMode docs compare these to work on premultiplied colors.

Solution

Blend the premultiplied colors directly:

  • plusLighter is CSS plus-lighter: min(1, dst + src) on every premultiplied channel, alpha included, so a cross-fade (A at t plus B at 1 - t) stays fully opaque on a transparent background. That is Skia's SkBlendMode::kPlus, so it is now drawn with the built-in mode on native and web instead of a runtime blender. Graphite keeps kPlus clamped to [0, 1] on formats that don't clamp themselves, such as F16, by blending in the shader (ContextUtils.cpp).
  • plusDarker keeps its runtime blender, with the same SkSL on native and web. It adds the alphas too, as CSS plus-darker ([css-compositing-2] Remove or fix definition of plus-darker w3c/fxtf-drafts#447) and Core Graphics do:
float outAlpha = min(1.0, src.a + dst.a);
vec3 c = max(vec3(0.0), outAlpha - ((dst.a - dst.rgb) + (src.a - src.rgb)));
return vec4(c, outAlpha);

An opaque source over an opaque backdrop gives the same result as before, so the existing snapshots pass unchanged. A transparent source leaves the backdrop untouched, and plusDarker over transparent pixels now draws the source instead of black. The comments in both files and in BlendMode.ts give the formulas with their alpha.

Tests

Paint.spec.tsx reads back five pixels:

  • 40% white with plusLighter over #333333: 153, and 40% black with plusDarker over #999999: 51. SwiftUI gives the same at 0.4.
  • a transparent black with plusDarker over #999999: the backdrop stays 153
  • two 60% whites with plusLighter and nothing behind them: the alphas add up to 1, [255, 255, 255, 255]
  • 40% green with plusDarker over 40% blue and nothing behind them: [0, 128, 128, 204]

On main it fails (plusLighter returns 255 for the first one); here it passes.

Verification

On this branch, up to date with main: yarn lint · yarn tsc · cpplint · yarn test (jest 895 passed) · yarn test-cpp · yarn build · yarn test:web 41 passed. Paint.spec and ParagraphMethods.spec pass 26 of 26 on an Android 15 emulator (API 35, software GPU, as in CI). Before this last change, Paint.spec also passed 16 of 16 on a Galaxy Z Fold6 (Android 16), where it fails with main's native code.

Not run: iOS e2e. The iPhone numbers above come from a Release build of an Expo SDK 58 app with an earlier version of this change patched into 3.8.0. It differs only over a translucent backdrop, which the table doesn't use.

Related

#3654 added both modes. 2.x has the same blenders; I can open a backport.

Base

39d14f7

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@wcandillon wcandillon left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the diagnosis is right and plusDarker looks good.

For plusLighter, I'd like to keep the result alpha as sa + da - sa * da rather than min(1, sa + da). That alpha was intentional: additive glows drawn inside a layer or on a transparent canvas shouldn't saturate to full opacity, and it is also what keeps plusLighter distinct from plus (kPlus). This version keeps your fix for the source alpha, gives the same values over an opaque backdrop (so your SwiftUI table and the new test still hold), and matches the alpha you already use for plusDarker:

vec4 main(vec4 src, vec4 dst) {
    float outAlpha = src.a + dst.a - src.a * dst.a;
    return vec4(min(src.rgb + dst.rgb, vec3(outAlpha)), outAlpha);
}

(on both native in CustomBlendModes.h and web in JsiSkPaint.ts, and the comments should mention the alpha.)

Could you also add a case to the test with a translucent destination? For example two rgba(255, 255, 255, 0.5) fills with plusLighter and no opaque backdrop. Since readPixel reads unpremultiplied values, it should give [255, 255, 255, 191] (with min(1, sa + da) it would be [255, 255, 255, 255]).

@wcandillon
wcandillon self-requested a review October 27, 2026 08:50

@wcandillon wcandillon left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: please ignore my request to keep the sa + da - sa * da alpha for plusLighter. We want plusLighter to match the CSS plus-lighter definition (Compositing Level 2), which is also what Core Graphics and SwiftUI use: min(1, S + D) on every premultiplied channel, alpha included. Your current shader already does exactly that, so no change is needed there.

The additive alpha matters: it is what lets a cross-fade (A at t plus B at 1 - t) reach full opacity on a transparent background, which is the main reason plus-lighter exists in CSS. The union alpha I suggested would bring back the opacity dip.

Two things that would still be nice:

  1. The translucent-destination test, with the expected value for the CSS definition: two rgba(255, 255, 255, 0.5) fills with plusLighter and no opaque backdrop should read [255, 255, 255, 255] (alpha 0.5 + 0.5).
  2. Optional: since the shader is now identical to SkBlendMode::kPlus, mapping plusLighter to kPlus (native Convertor.h / applyBlendMode and web JsiSkPaint.ts) would avoid the runtime blender. Happy either way.

LESANF and others added 2 commits October 27, 2026 17:56
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LESANF

LESANF commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@wcandillon Thanks for the review!

I made the changes you suggested and summarized them in a Review follow-up section at the top of the description.

I'd appreciate another look when you have time.

@LESANF
LESANF requested a review from wcandillon October 27, 2026 09:58
@wcandillon

Copy link
Copy Markdown
Owner

Thanks for the follow-up, plusLighter and the kPlus mapping look good.

One more change on plusDarker. Its output alpha is still the source-over union, sa + da - sa * da. The CSS definition and Core Graphics use the same additive alpha as plus-lighter, min(1, sa + da). The published spec text for plus-darker is broken (it yields values up to 2), and the CSSWG resolved in w3c/fxtf-drafts#447 to define it as what WebKit does:

co = min(1, αs + αb) − min(1, αs(1 − Cs) + αb(1 − Cb))
αo = min(1, αs + αb)

The fix is one line in CustomBlendModes.h and JsiSkPaint.ts:

vec4 main(vec4 src, vec4 dst) {
    float outAlpha = min(1.0, src.a + dst.a);
    vec3 c = max(vec3(0.0), outAlpha - ((dst.a - dst.rgb) + (src.a - src.rgb)));
    return vec4(c, outAlpha);
}

(no need to clamp the darkness term separately, the max(0, ...) already covers it), plus the alpha in the BlendMode.ts comment.

Could you also add a translucent-over-translucent plusDarker case to the test? rgba(0, 255, 0, 0.4) with plusDarker over rgba(0, 0, 255, 0.4) and no opaque backdrop should read [0, 128, 128, 204] (0.4 is 102/255 exactly, so the native truncation you found does not get in the way). With the union alpha it reads [0, 96, 96, 163].

LESANF and others added 2 commits October 27, 2026 22:45
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LESANF

LESANF commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

@wcandillon I've made the changes you suggested.

The details are in the Review follow-up at the top of the description.

Let me know if I missed anything.

@wcandillon wcandillon left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great Thank You 💚

@LESANF

LESANF commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

This is great Thank You 💚

Thanks for merging this! I use React Native Skia every day, so I'm really happy to have contributed to it. 🥹

@wcandillon
wcandillon merged commit 20ae1f1 into wcandillon:main Oct 27, 2026
9 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 3.8.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants