合成与混合 – 探索混合模式背后的数学和直觉
Compositing and Blending – Exploring the math and intuition behind blend modes

原始链接: https://nik.digital/posts/compositing-blending

## 浏览器中的合成与混合:总结 浏览器通过分层元素并将它们组合在一起来渲染网页,这个过程称为**合成**。这涉及到确定每个图层如何与其背景交互,使用由**Porter-Duff 运算符**定义的运算。这些运算符根据像素在源图层和背景图层中的存在情况来确定像素的组合方式,提供了 12 种不同的合成方法。 **混合**是合成的关键部分,它发生在图层重叠的区域*内部*。混合模式,如“正片叠底”或“滤色”,是函数,它们通过组合源颜色和背景颜色来确定结果颜色。这些模式分为几类——提亮、变暗、对比度、反转和基于组件(HSL)——每种模式都会产生不同的视觉效果。 虽然浏览器通常使用 source-over 合成,但 CSS 属性(如 `mix-blend-mode` 和 `background-blend-mode`)允许开发者利用这些混合模式来实现创意效果。然而,颜色空间差异(如 sRGB 与 Display P3)可能导致跨浏览器出现不一致的情况。 理解合成和混合可以解锁各种效果的可能性,例如图像处理(双色调)、独特的边框以及视觉上有趣的文本处理——所有这些都可以仅使用 CSS 实现。W3C 规范和 Bartosz Ciechanowski 的文章等资源提供了对技术细节的更深入研究。

黑客新闻 新的 | 过去的 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 合成与混合 – 探索混合模式背后的数学和直觉 (nik.digital) 6 分,OuterVale 1 小时前 | 隐藏 | 过去的 | 收藏 | 讨论 帮助 考虑申请YC 2026年夏季项目!申请截止至5月4日 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请YC | 联系方式 搜索:
相关文章

原文

Your browser is a miracle!

Every pixel you see on this page is the result of stacking hundreds of elements on top of each other and deciding, millions of times, pixel by pixel, what the result should look like.

Two important steps in this process are compositing and blending, and in this post, we'll get to see what they are, how they work, and where they can be useful when building fun effects on the web.

What you'll learn:
  • What compositing is, and what it has to do with blend modes
  • What a blend mode is and how you can reason about what it will do
  • Which blend modes exist and which ones are commonly useful in real-world scenarios
  • How blend modes can be used in CSS, and problems that you might run into

Let's dive in!

Compositing

Before we can talk about what a blend mode is, we have to talk about compositing. Compositing is "the process of combining an element with its backdrop", according to the W3C specification. What does this mean?

The pixels you see on your screen while you're reading this were drawn by your browser. This webpage consists of many HTML elements making up many layers, and the browser draws each layer and then combines them. If we simplify a bit, it looks like this:

0 layers

The step of combining these layers with each other is called compositing. We'll call the layer that is drawn onto during compositing the backdrop, and the layer that is being drawn the source.

Let's look at how the browser might composite the input textbox of your favorite LLM tool, which consists of many different elements. We'll pretend that each element is a layer, but do note that in reality, layers are made up of multiple elements. Once a layer is painted, operations like transforming or fading it only require re-compositing, not re-painting, which saves time and increases performance.

For our general intro to compositing though, what exactly constitutes a layer in the browser is not of huge concern. Move the slider to pan through the compositing steps, and view the different compositing layers by clicking the small 3D button in the top corner.

In this example, we simply draw the current layer on top of the backdrop and call it a day. For a browser, this is sufficient. But compositing can be much more powerful: In different contexts, we can choose from many other ways to composite the source layer with the backdrop, like drawing it behind, or only drawing the overlap of both layers.

Let's look at the different operations which are possible. When we combine two images, each pixel falls into one of four regions, depending on whether this pixel exists in both the source and the backdrop, only one of them, or neither:

Source only

Backdrop only

Both

Neither

For each of these regions, we can choose what the composited pixel should be; for example, for the region in which only the source has visible pixels, we can either display these pixels in the composited image, or display nothing. The following table shows the available options:

RegionNameDescription
Source OnlyThe pixel is present in the source but not the backdrop.

2 Options: Choose between showing source or nothing

Backdrop OnlyThe pixel is present in the backdrop but not the source.

2 Options: Choose between showing backdrop or nothing

BothThe pixel is present in both the source and the backdrop.

3 Options: Choose between showing source, backdrop or nothing

NeitherThe pixel is not present in either the source or the backdrop.

1 Option: Show nothing (as there is nothing to composite)

The Porter-Duff Operators

If we count all the possible permutations, there are a total of 2×2×3×1=122 \times 2 \times 3 \times 1 = 12

They define, mathematically, how to combine two pixels during compositing. The formula to calculate the resulting color for a pixel when compositing two layers is given by:

Co=Fs×Cs+Fb×CbC_o = F_s \times \textcolor{#3b82f6}{C_s} + F_b \times \textcolor{#f59e0b}{C_b} \\
联系我们 contact @ memedata.com