简体   繁体   English

使用 SVG 进行加色混合(加色混合)

[英]Using SVG for additive color mixing (additive blending)

Coming from a graphic design background I know how to cheat to create an effect of additive color .来自平面设计背景,我知道如何作弊来创造加色效果。 The same basic solution is proposed in another post on here . 此处的另一篇文章中提出了相同的基本解决方案。

The post above refers to transparent .png files but the concept is the same.上面的帖子指的是透明的.png文件,但概念是相同的。 The basic effect I'd like to create is like the one pictured here .我想要创建的基本效果就像这里的图片

I'd love to do it in SVG so that it can scale and so that when I am talking about a given topic (let's just say the topic is 'green') I can enlarge that portion of the graphic and the overlapping areas would still display correctly.我很想在 SVG 中做它,这样它可以缩放,这样当我谈论一个给定的主题时(假设主题是“绿色”)我可以放大图形的那部分,重叠区域仍然会正确显示。

These posts seem to get pretty close:这些帖子似乎非常接近:

But none of the above deal with SVG so let me give it a whirl.但以上都没有涉及 SVG,所以让我试一试。

Right now you can do this with SVG filters .现在您可以使用SVG 过滤器来做到这一点。 Since your interest lies in mixing colors, you might be interested in researching the following filter primitives: feBlend , feComposite , feColorMatrix and feComponentTransfer .由于您的兴趣在于混合颜色,因此您可能有兴趣研究以下滤镜原语: feBlendfeCompositefeColorMatrixfeComponentTransfer

If you want to learn the easy way (Inkscape) see this blogpost .如果您想学习简单的方法 (Inkscape),请参阅此博文 I can also recommend reading the Inkscape book and in particular how to do custom filters .我也可以推荐阅读Inkscape 的书,特别是如何做自定义过滤器 Here's one page showing feBlend with similar results as in your basic effect example. 这是显示 feBlend 的一页,其结果与您的基本效果示例相似。

Update: here's the relevant svg file from the inkscape book, it should look like the image below in browsers that support svg filters (and the BackgroundImage filter input, note that Gecko doesn't support BackgroundImage and enable-background ).更新: 这是inkscape 书中的相关svg 文件,在支持svg 过滤器(以及BackgroundImage过滤器输入,注意Gecko 不支持BackgroundImageenable-background浏览器)中,它应该如下图所示。feBlend 变体

This version replaces an earlier version that wasn't truly cross-browser.此版本取代了并非真正跨浏览器的早期版本。 I realized that I didn't need separate shapes for the various circles, I could clone, reposition and recolor the original shape within the filter.我意识到我不需要为不同的圆圈制作单独的形状,我可以在过滤器中克隆、重新定位和重新着色原始形状。

 <svg x="0px" y="0px" width="600px" height="600px" viewbox="0 0 600 600"> <defs> <filter id="B4" x="-150%" width="400%" y="-150%" height="400%"> <feOffset in="SourceGraphic" result="pre-red" dx="-70" dy="-120"/> <feColorMatrix in="pre-red" type="matrix" result="red" values="0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0"/> <feOffset in="SourceGraphic" result="pre-blue" dx="70" dy="-120"/> <feColorMatrix in="pre-blue" type="matrix" result="blue" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0"/> <feBlend mode="screen" in="red" in2="blue" result="main"/> <feBlend mode="screen" in="main" in2="SourceGraphic"/> </filter> </defs> <circle filter="url(#B4)" cx="200" cy="250" r="100" fill="#00FF00" /> </svg>

From https://drafts.fxtf.org/compositing-1/#mix-blend-mode , Example 2:来自https://drafts.fxtf.org/compositing-1/#mix-blend-mode ,示例 2:

circle { mix-blend-mode: screen; }

<svg>
    <circle cx="40" cy="40" r="40" fill="red"/>
    <circle cx="80" cy="40" r="40" fill="lime"/>
    <circle cx="60" cy="80" r="40" fill="blue"/>
</svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM