问题描述
我想实现从PDF规范的混合模式,为我自己的快乐,在上海社会科学院。
I'm trying to implement blend modes from the PDF specification, for my own pleasure, in SASS.
PDF规格:的
第322 alpha组合部分。
Page 322 is the alpha compositing section.
输入的值是RGBA格式,但现在我想实现alpha分量的混合。我怎么excatly去这样做呢?从我收集我的价值观应该是0.0和1.0之间,这样做了。不过从规格看来,你应该融入每个颜色通道?难道我只是平均出来,回到RGBA形式为我的alpha通道?
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?
任何帮助是appriciated,我不介意阅读博客,书籍等,让我的答案,因为这纯粹是一种智力活动。
Any help is appriciated, I don't mind reading blog, books etc. to get my answer, as this is purely an intellectual exercise.
在此先感谢,
周华健
推荐答案
SVG规范有很多的不同的混合模式很好的方程式。是的,你必须计算的两个的新阿尔法的和的新的颜色 - 每个通道。对于标准的混合模式,阿尔法的计算方法是这样的:
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:
alpha_final = alpha_bg + alpha_fg - alpha_bg * alpha_fg
注意:我看到你在考虑阿尔法为0和1之间,这是很好的。在CSS中的Alpha值总是被定义为浮点值从0到1;这是很好的坚持这一惯例,因为它使计算非常容易。
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.
它有助于'premultiply'其阿尔法每个颜色通道;这些都为跨preting更多的帮助,并使用通常的公式:
It helps to 'premultiply' each colour channel by its alpha; these are more helpful for interpreting and using the usual formulae:
colour_bg_a = colour_bg * alpha_bg
在换句话说:
red_bg_a = red_bg * alpha_bg
green_bg_a = green_bg * alpha_bg
blue_bg_a = blue_bg * alpha_bg
然后,对于纯简的alpha合成(如覆盖描图纸的床单,也被称为 SRC-超过
中的和 SVG alpha合成规范),你把每个通道计算的话是这样的:
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:
colour_final_a = colour_fg_a + colour_bg_a * (1 - alpha_fg)
的最后一步是要由最终的α'未乘'每一最终颜色通道值:
The last step is to 'un-multiply' each final colour channel value by the final alpha:
colour_final = colour_final_a / alpha_final
,并把它变成你的mixin以某种方式:
and put it into your mixin somehow:
rgba(red_final, green_final, blue_final, alpha_final)
的其他混合模式(乘,差,屏幕等)是稍微更复杂的公式,但概念为每一个模式是相同的:
The other blending modes (multiply, difference, screen, etc) are slightly more complicated formulas, but the concept for every single mode is the same:
- 分离的R,G,B和的前景色和背景色既是一个值
- 计算新的,最终颜色的α与上述式
- pre-乘法的R,他们的alpha值G,B值
- 计算新的,最终的R,G,B值(插入共混此处模式式)
- 将最终的α 取消乘以最终的R,G,B值
- 剪辑最终的R,G和B值,使它们在0和255(必需的一些模式中,但不是所有的)
- 把颜色再聚首!
- Separate the R, G, B, and A values of both the foreground and background colours
- Calculate the alpha of the new, final colour with the above formula
- Pre-multiply all the R, G, and B values by their alpha value
- Calculate the new, final R, G, and B values (insert blending mode formula here)
- Un-multiply the final R, G, and B values by the final alpha
- Clip the final R, G, and B values so that they are between 0 and 255 (necessary for some modes, but not all)
- Put the colour back together again!
如果你仍然有兴趣在此,我一直在做的非常的事情笔。你可以在这里看到我的进步:https://github.com/pdaoust/stylus-helpers/blob/master/blend.styl你也许可以用它作为一个起点为您自己的Sass的mixin。
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.
我做的第一件事就是从0转换所有的R,G,B值 - 255值0 - 1浮点值计算的目的。说不上来,如果这是必要的,它确实需要将它们转换回到0 - 255的值。这感觉我的权利,和波特和达夫曾在0 - 在原来的纸1的浮点值
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. Dunno 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.
(我遇到的一些合成模式,即产生与预期的结果是,SVG规范的照片。我怀疑该规范给出了错误的公式完全不同的结果麻烦。如果有人知道关于波特/达夫混合模式,我会非常感激他们的帮助!)
(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!)
这篇关于阿尔法合成算法(混合模式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!