It’s the first time I’m going to use SVG graphics and I’m a bit confused… I’ve researched extensively but I cannot seem to find an answer to what I’m trying to do.
I want to give an image the following appearance, given the source image is a color image.
The main reason behind this effect is to overcome bad quality images (both in terms of aesthetics and resolution) my clients might upload to their websites.
In Photoshop this is a Gradient Map and a transparent grid in Mode Multiply.
I’ve managed to apply a filter for the grayscale and “gradient map” but I cannot add the grid/pattern.
This is my code so far:
SVG file gradient-map.svg
<svg xmlns="http://www.w3.org/2000/svg">
<filter id="duotone_filter">
<feColorMatrix type="matrix" result="grayscale"
values="1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
0 0 0 1 0" >
</feColorMatrix>
<feComponentTransfer color-interpolation-filters="sRGB" result="duotone">
<feFuncR type="table" tableValues="0.0039525691 0.5764705882"></feFuncR>
<feFuncG type="table" tableValues="0.0123456790 0.8431372549"></feFuncG>
<feFuncB type="table" tableValues="0.0123456790 0.9803921569"></feFuncB>
<feFuncA type="table" tableValues="0 1"></feFuncA>
</feComponentTransfer>
</filter>
</svg>
HTML
<div class="image">
<img src="link/to/file" />
</div>
CSS
.image {
filter: url('../images/gradient-map.svg#duotone_filter');
}
And the SVG file for the pattern/fill
<svg xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="6px" height="6px" viewBox="0 0 6 6">
<circle cx="3" cy="3" r="2" />
</svg>
Is this the way things should be done? How should I proceed to achieve this effect?
If you could also link me to useful resources I would much appreciate. I haven’t found any recent guides or articles.
Thanks.
2
Answers
You’re on the right track. Here’s a demo I put together (and on Codepen). And the output:
There’s a helpful question to Overlay grid on responsive image, but I opted for simply overlaying and scaling a large transparent PNG. You’ll likely end up with better results if you use a small, repeating grid section. Of course you’ll have to choose what color, size, etc. to make the grid overlay.
As a side note, you cannot use the
:after
or:before
pseudo-elements withimg
elements, so you need to wrap them in a container. You could also accomplish this with an additional element, like:But I prefer pseudo-elements so I don’t have to repeat
<div class="grid-overlay"></div>
every time I want the grid overlay.Also an example using only SVGs (symbols):
One final example that also works in Firefox and uses two SVG filters. The key to this example is that it uses another filter to create a composite image. I took another SVG from the web but you could use an inline SVG and reference it by ID as well.
A handy way to do this is to combine the grid overlay with the filter by putting inline SVG for the grid into an feImage primitive.
(I also changed your greyscale matrix because it was only using the red channel as source which is going to create weird results on blue/green heavy photos.)
The source SVG for the inlined data uri is
If you want to tweak it – I use this converter for svg+xml – which is pretty useful https://codepen.io/yoksel/details/JDqvs