为了我自己的乐趣,我正在尝试在 SASS 中实现 PDF 规范中的混合模式。
PDF规格:
http://www.adobe.com/content /dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
第 322 页是 Alpha 合成部分。
输入值采用 RGBA 格式,但现在我正在尝试实现 alpha 分量的混合。我该如何去做呢?根据我收集的数据,我的值应该在 0.0 和 1.0 之间,就这样了。但是从规格来看,您似乎应该为每个颜色通道进行混合?我是否只需将其平均即可返回到我的 alpha 分量的 RGBA 形式?
任何帮助都是值得的,我不介意阅读博客、书籍等来得到我的答案,因为这纯粹是一种智力练习。
预先感谢,
埃米尔
I'm trying to implement blend modes from the PDF specification, for my own pleasure, in SASS.
PDF Specification:
http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
Page 322 is the alpha compositing section.
The input values are in RGBA format, but now I'm trying to implement blending of the alpha component. How do I excatly go about doing it? From what I gather my values should be between 0.0 and 1.0, that's done. However from the specs it seems that you should blend for each color channel? Do I just average it out to get back to RGBA form for my alpha component?
Any help is appriciated, I don't mind reading blog, books etc. to get my answer, as this is purely an intellectual exercise.
Thanks in advance,
Emil
发布评论
评论(1)
SVG 规范有很多适用于各种混合模式的良好方程。是的,您确实必须为每个通道计算新的 alpha 和新的颜色。对于标准混合模式,alpha 的计算方式如下:
注意:我发现您认为 alpha 介于 0 和 1 之间,这很好。 CSS 中的 Alpha 值始终定义为 0 到 1 之间的浮点值;坚持这个惯例是件好事,因为它使计算变得非常容易。
它有助于将每个颜色通道“预乘”其 alpha;这些对于解释和使用常用公式更有帮助:
换句话说:
然后,对于简单的 alpha 合成(如覆盖描图纸,也称为 src-over) “http://keithp.com/%7Ekeithp/porterduff/” rel="noreferrer">Porter 和 Duff 的原始论文 和 SVG alpha 合成规范),您采用每个通道并进行计算:
最后一步是将每个最终颜色通道值“取消乘”最终的 alpha:
并以某种方式将其放入您的 mixin 中:
其他混合模式(乘法、差值、屏幕等)是稍微复杂的公式,但每个单独的概念模式是相同的:
如果您仍然对此感兴趣,我一直在 Stylus 中做同样的事情。您可以在这里查看我的进度: https://github.com/pdaoust/ stylus-helpers/blob/master/blend.styl 您也许可以使用它作为您自己的 Sass mixin 的起点。
我做的第一件事是将所有 R、G 和 B 值从 0 - 255 值转换为 0 - 1 浮点值以进行计算。我不知道这是否有必要,它确实需要将它们转换回 0 - 255 值。我感觉这是对的,Porter 和 Duff 在他们的原始论文中使用了 0 - 1 浮点值。
(我在使用某些合成模式时遇到了麻烦,这些模式产生的结果与 SVG 规范所描绘的预期结果截然不同。我怀疑该规范给出了错误的方程。如果有人了解 Porter/Duff 混合模式,我会告诉您非常感谢他们的帮助!)
The SVG spec has a lot of good equations for various blending modes. And yes, you do have to calculate both the new alpha and the new colour -- for each channel. For standard blending modes, the alpha is calculated this way:
Note: I see you're considering alpha to be between 0 and 1, which is good. Alpha values in CSS are always defined as float values from 0 to 1; it's good to stick with this convention, because it makes the calculations immensely easier.
It helps to 'premultiply' each colour channel by its alpha; these are more helpful for interpreting and using the usual formulae:
In other words:
Then, for plain-jane alpha compositing (like overlaying sheets of tracing paper, also known as
src-over
in Porter and Duff's original paper and the SVG alpha compositing spec), you take each channel and calculate it thus:The last step is to 'un-multiply' each final colour channel value by the final alpha:
and put it into your mixin somehow:
The other blending modes (multiply, difference, screen, etc) are slightly more complicated formulas, but the concept for every single mode is the same:
If you're still interested in this, I've been doing the very thing in Stylus. You can see my progress here: https://github.com/pdaoust/stylus-helpers/blob/master/blend.styl You might be able to use it as a starting point for your own Sass mixin.
The first thing I do is convert all the R, G, and B values from 0 - 255 values to 0 - 1 float values for the purposes of the calculations. I don't know if that's necessary, and it does require converting them back to 0 - 255 values. It felt right to me, and Porter and Duff worked in 0 - 1 float values in their original paper.
(I'm encountering trouble with some of the compositing modes, which produce wildly different results from the expected results that the SVG spec pictures. I suspect that the spec gives the wrong equations. If anyone knows about Porter/Duff blending modes, I'd be very grateful for their help!)