Repository navigation
fix(🎨): honor the source alpha in plusLighter and plusDarker - #4337
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
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]).
There was a problem hiding this comment.
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:
- The translucent-destination test, with the expected value for the CSS definition: two
rgba(255, 255, 255, 0.5)fills withplusLighterand no opaque backdrop should read[255, 255, 255, 255](alpha 0.5 + 0.5). - Optional: since the shader is now identical to
SkBlendMode::kPlus, mappingplusLightertokPlus(nativeConvertor.h/applyBlendModeand webJsiSkPaint.ts) would avoid the runtime blender. Happy either way.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@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. |
|
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, co = min(1, αs + αb) − min(1, αs(1 − Cs) + αb(1 − Cb)) The fix is one line in 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]. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@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. |
Thanks for merging this! I use React Native Skia every day, so I'm really happy to have contributed to it. 🥹 |
|
🎉 This PR is included in version 3.8.1 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Review follow-up
After the latest review:
plusDarkernow adds the alphas as well,min(1, sa + da), like CSSplus-darker(defined as what WebKit does in [css-compositing-2] Remove or fix definition of plus-darker w3c/fxtf-drafts#447) and Core Graphics. It is one line on native and web, plus theBlendMode.tscomment.plusDarkercase: 40% green over 40% blue with nothing behind reads[0, 128, 128, 204], where the union alpha gives[0, 96, 96, 163].main(fix(🔤): apply TextStyle fontVariations on native #4338).Oslo at opacity
t, then Zurich at1 - twithplusDarker, 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 att = 0.5. This change matches Core Graphics within 1/255 in every frame. Rendered with CanvasKit, which runs the same SkSL as native.After the first review:
plusLighteris drawn withkPluson native and web, and its runtime blender is gone. The result is the same.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'sSkColor4f::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.Oslo at opacity
t, then Zurich at1 - twithplusLighter, over a transparent canvas (the checkerboard). Left to right: CSSplus-lighterin Chrome, 3.8.0, the union alphasa + da - sa * dafrom the first review, and this PR. CSS and this PR stay opaque through the whole fade; the other two dip to alpha 0.75 att = 0.5. This PR matches Chrome within 2/255 in every frame. Rendered with CanvasKit, which drawsplusLighterwith the samePlusmode as native.Before / after
Alpha 0 → 1 → 0. Left to right: SwiftUI, 3.8.0, this PR (top
plusLighter, bottomplusDarker).Problem
plusLighterandplusDarkerignore the alpha of what is drawn. Over an opaque backdrop, a white glow at 5% opacity is as bright withplusLighteras an opaque one, so it can't fade out. A black shape withplusDarkerturns the backdrop black even at alpha 0.Measured on an iPhone 15 Pro (iOS 26.5) against SwiftUI's
.plusLighter/.plusDarkeron the same device. The source is white over a 51 grey forplusLighter, and black over a 153 grey forplusDarker:plusLighter, SwiftUIplusLighter, 3.8.0plusLighter, this PRplusDarker, SwiftUIplusDarker, 3.8.0plusDarker, this PRThe ±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,plusDarkerand "blend mode alpha".Root cause
The blenders in
cpp/api/CustomBlendModes.h, mirrored insrc/skia/web/JsiSkPaint.ts, un-premultiply both colors before combining them:Dividing by
src.aturns a 5% white into full white. Over an opaque backdropoutAlphais 1, so the alpha never comes back. At alpha 0,plusDarkercomputesmax(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. CSSplus-lighterand the iOS modes theBlendModedocs compare these to work on premultiplied colors.Solution
Blend the premultiplied colors directly:
plusLighteris CSSplus-lighter:min(1, dst + src)on every premultiplied channel, alpha included, so a cross-fade (A attplus B at1 - t) stays fully opaque on a transparent background. That is Skia'sSkBlendMode::kPlus, so it is now drawn with the built-in mode on native and web instead of a runtime blender. Graphite keepskPlusclamped to [0, 1] on formats that don't clamp themselves, such as F16, by blending in the shader (ContextUtils.cpp).plusDarkerkeeps its runtime blender, with the same SkSL on native and web. It adds the alphas too, as CSSplus-darker([css-compositing-2] Remove or fix definition of plus-darker w3c/fxtf-drafts#447) and Core Graphics do: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
plusDarkerover transparent pixels now draws the source instead of black. The comments in both files and inBlendMode.tsgive the formulas with their alpha.Tests
Paint.spec.tsxreads back five pixels:plusLighterover#333333: 153, and 40% black withplusDarkerover#999999: 51. SwiftUI gives the same at 0.4.plusDarkerover#999999: the backdrop stays 153plusLighterand nothing behind them: the alphas add up to 1,[255, 255, 255, 255]plusDarkerover 40% blue and nothing behind them:[0, 128, 128, 204]On
mainit fails (plusLighterreturns 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:web41 passed.Paint.specandParagraphMethods.specpass 26 of 26 on an Android 15 emulator (API 35, software GPU, as in CI). Before this last change,Paint.specalso passed 16 of 16 on a Galaxy Z Fold6 (Android 16), where it fails withmain'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.xhas the same blenders; I can open a backport.Base
39d14f7🤖 Generated with Claude Code