简体   繁体   English

同时使用 CSS 灰度滤镜和背景混合模式?

[英]CSS grayscale filter and background-blend-mode at same time?

I'm trying to treat an image the same way it is in a photoshop file - desaturating the image to grayscale, and then applying a color overlay with a multiply blend mode.我正在尝试以与在 photoshop 文件中相同的方式处理图像 - 将图像去饱和化为灰度,然后使用乘法混合模式应用颜色叠加。 To this end, I am styling a CSS background image with...为此,我正在为 CSS 背景图像设置样式...

.someclass
{
    /* grayscale */
    -webkit-filter: grayscale(1); 
    filter: gray; 
    filter: grayscale(1);
    filter: url(desaturate.svg#greyscale);

    /* multiply */
    background-color: rgba(190, 50, 50, 0.65);
    background-blend-mode: multiply;
}

The problem with this is that the grayscale filter ends up desaturating red color for the blend mode.这样做的问题是灰度滤镜最终会降低混合模式的红色饱和度。 In other words, the multiply is occurring first and then the grayscale.换句话说,乘法首先发生,然后是灰度。 How do I switch it so that the grayscale is applied first and then the blend mode is applied second?如何切换它以便首先应用灰度,然后再应用混合模式?

I have created a fiddle ( http://jsfiddle.net/g54LcoL1/1/ ) for the code and a screenshot (made in Photoshop) of what I would expect the the fiddle result to look like.我为代码创建了一个小提琴 ( http://jsfiddle.net/g54LcoL1/1/ ) 和我希望小提琴结果看起来像的屏幕截图(在 Photoshop 中制作)。 The bottom most image, div.grayscale.multiply , should be colored red.最底部的图像div.grayscale.multiply应该是红色的。

在此处输入图像描述

You can not do it with filter, but you can do it staying with blend mode for everything 您无法使用过滤器,但您可以使用混合模式保持一切

the grayscale equivalent in blend is luminosity, with the image as source and a solid white as backdrop 混合中的灰度等效物是光度,图像为源,背景为纯白色

So the background images, from bottom to top, are: 因此,从底部到顶部的背景图像是:

  1. white (as background-color) 白色(背景色)
  2. your image 你的形象
  3. solid red (that must be specified as a gradient for now) 纯红色(现在必须指定为渐变)

and the blend modes are luminosity and multiply 混合模式是光度和乘法

 .test { width: 400px; height: 400px; background-image: linear-gradient(0deg, red, red), url("http://cuteoverload.files.wordpress.com/kitteh_puddle-1312.jpg"); background-color: white; background-blend-mode: multiply, luminosity; background-size: cover; } 
 <div class="test"></div> 

The blend mode is applied to the background layers, and then the filter is applied to the whole element, so they are sort of dealing with two different things: by the time that the background is calculated (including the red-ish appearance), the whole element is converted to grayscale with the filter. 混合模式应用于背景图层,然后将滤镜应用于整个元素,因此它们处理两种不同的事情:在计算背景的时间(包括红色外观),使用过滤器将整个元素转换为灰度。

There is a proposed filter() function for image references, so in theory, you should be able to apply a filter to any image as it is loaded. 有一个建议的filter()函数用于图像引用,因此理论上,您应该能够在加载任何图像时应用滤镜。 I think this is the idea: 我认为这是个主意:

.someclass {
    background-image: filter(cat.jpg, grayscale(100%));
    background-color: red;
    background-blend-mode: multiply;
}

Sadly, I don't think this is implemented anywhere yet, it's just in the draft version of the Filters spec. 遗憾的是,我不认为这是在任何地方实现的,它只是在滤波器规范的草案版本中。

In general, the order of operations in terms of these types of "post-processing" CSS effects is defined in the same order as for SVG: filtering is done first, then clipping, then masking, then blending. 通常,根据这些类型的“后处理”CSS效果的操作顺序以与SVG相同的顺序定义:首先进行过滤,然后进行剪切,然后进行掩蔽,然后进行混合。

(See the Blending & Compositing Spec. ) So, there's nothing you can do in terms of changing that, I'm afraid. (参见混合和合成规范 )因此,在改变这一点方面你无能为力,我担心。

As Joel mentioned in his comment under the accepted answer, it's possible to do this using mix-blend-mode . 正如乔尔在接受答案的评论中提到的,可以使用mix-blend-mode来做到这一点。

In my example I'm going to use an actual image instead of a background image. 在我的例子中,我将使用实际图像而不是背景图像。 If it has to be a background image then you can still use the same technique. 如果它必须是背景图像,那么您仍然可以使用相同的技术。 Just replace the image with an empty div that has background image styling. 只需将图像替换为具有背景图像样式的空div。

Also I'm going to use the "screen" blend mode instead of "multiply" because it demonstrates the color removal more clearly. 此外,我将使用“屏幕”混合模式而不是“乘法”,因为它更清楚地演示了颜色去除。

With color removal: 随着颜色去除:

 .image-wrapper { background: red; display: inline-block; } .image-wrapper img { mix-blend-mode: screen; filter: grayscale(100%); } 
 <div class="image-wrapper"> <img src="https://static.decalgirl.com/assets/designs/large/clrkit.jpg" alt="Colorful cats"/> </div> 

Without color removal: 没有颜色去除:

 .image-wrapper { background: red; display: inline-block; } .image-wrapper img { mix-blend-mode: screen; } 
 <div class="image-wrapper"> <img src="https://static.decalgirl.com/assets/designs/large/clrkit.jpg" alt="Colorful cats"/> </div> 

More information can be found here: 更多信息可以在这里找到:

https://css-tricks.com/almanac/properties/m/mix-blend-mode/ https://css-tricks.com/almanac/properties/m/mix-blend-mode/

100% MIX-BLEND-MODE 100% 混合混合模式

  red  =========================================
                                                |
   cat     =======                              ==== Blending (luminosity)
                  |                             |
                  ==== Blending (multiply) ===
                  |
   white =========

 <div style=" position:relative; background-color: rgb(255,255,255); height: 316px; width: 450px;"> <div style=" background-image: url('http://4.bp.blogspot.com/_BSmKs8FzkfI/SCi18Kth3oI/AAAAAAAAACA/BzWFAOZXu3U/s1600/kitteh_puddle.jpg'); height: 316px; width: 450px; position:relative; left:0px; top:0px; mix-blend-mode: luminosity;"> </div> <div style=" background: rgba(190, 50, 50, 0.65); height: 316px; width: 450px; position:absolute; left:0px; top:0px; mix-blend-mode: multiply;"> </div> </div>

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

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