I’ve done this in the past and I know it exists, but for some reason I don’t find it right now (wasted couple of hours with no success).
I want to mimic the Photoshop
RGB
levels via CSS
.
On the following image I changed the middle value from 1 to 0.5.
I wanna get the same effect (or at least as close as possible) with CSS.
I’ve tried with the following code: https://jsfiddle.net/txwu3so5/
I need to find some replacement code to get that effect. The actual code affects the white color (I don’t want that).
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
.img_02 {
filter: url(#level-50);
}
</style>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" style="position:absolute;height:0;">
<defs>
<filter id="level-50" x="0" y="0">
<!-- begin of code to replace -->
<feColorMatrix type="matrix" values="
0.5 0 0 0 0
0 0.5 0 0 0
0 0 0.5 0 0
0 0 0 1 0"
/>
<!-- end of code to replace -->
</filter>
</defs>
</svg>
<img class="img_01" src="https://image.ibb.co/kzz41Q/image.png" />
<br /><br />
<img class="img_02" src="https://image.ibb.co/kzz41Q/image.png" />
</body>
</html>
In Photoshop
I usually change from 1 to different values, for example: 0.75, 0.5, etc. I want to mimic this somehow with CSS
. I remembered I used a code very similar to the above one, the only thing that needs to be changed is the replacement code.
[EDIT 1]
This is part of another problem. It is a requirement to use the SVG
filter
tag.
2
Answers
You are basically updating the gamma of the image here. There is a built-in filter component in SVG to do that.
See demo below.
Note: the filter doesn’t update properly on Chrome, so try this demo with Firefox instead
To fully duplicate the RGB levels capability from Photoshop, you’d need three filter components: an feComponentTransfer/gamma and two feComponentTransfer/tables. If you’re just adjusting the middle pivot value as above, you just need the first feComponentTranfer. To read more on how ComponentTransfers work – please see the webplatform docs (although they’ve started to rot a little.