如何避免 SkiaSharp 中的 "adding up" 不透明度

How to avoid "adding up" of opacities in SkiaSharp

我目前正在创建一个 Xamarin.Forms 应用程序。我的一个页面使用 SkiaSharp 允许用户以文本标记样式(即低不透明度的黄色画笔)突出显示图像的一部分。

相关的 SKPaint 对象是这样定义的:

var strokePaint = new SKPaint()
{
    Color = Color.FromRgba(255, 255, 0, 100).ToSKColor(),
    Style = SKPaintStyle.Stroke,
    StrokeWidth = StrokeWidth
};

到目前为止一切正常,但令我困扰的是当我有多个重叠路径时不透明度 "increases",直到某个时候底层图片不再可见。

我该怎么做才能避免这种重叠?我正在考虑将所有路径合并为一条路径,但这似乎行不通,因为允许用户在笔画之间更改 StrokeWidth,而我没有看到任何绘制宽度不同的路径的方法。

希望大家对我有所帮助。任何想法表示赞赏!

I'm not super familiar with Skia, but I took a look at the documentation for SKPaint, and it looks like it has a BlendMode property. Based on how similar things work in other systems, that should control how colors are combined. You might have to try different values to get the effect you are looking for. Dst, or Modulate look like good candidates. – Bradley Uffner

感谢布拉德利的回答!我使用 Darken 混合模式并将不透明度设置为 255,这在突出显示文本时产生了非常好的效果(只有较暗的颜色可见,因此浅色背景上的深色文本在我的标记背景上变成了深色文本颜色)。