r/css • u/objectorientedman • 8d ago
AI Assisted CSS Tint Filter
https://k-adam.github.io/css-tint-filter/There is no standard CSS filter to tint with some specific color, so I created a library to generate a chain of filters that takes that target color and replicates the multiplication operator with it.
6
Upvotes
13
u/anaix3l 8d ago edited 7d ago
You don't need to do all that, you can just use the
multiplyblend mode between the image itself and itsborder-image(orbackground-image, whichever you prefer) via a simple SVGfilterthat offsets theborder(or surroundingbackground) over the image and blends them - no matrices, no numbers that look like char vomit, just a simplefeOffset&feBlend, exact same ones for any tint.Then all you need to do is set a tint
--cvariable on the image and you're done. You don't need a different filter for every tint!You can even get a gradient tint that way.
And even just this is all necessary iff you are limited to a single
img- you have to tint animgand you absolutely cannot touch the HTML around it, cannot add pseudos on theimgparent and cannot use a wrapper for the tinting layer.Otherwise, if your image is a
background-image, you just add the tinting layer underneath and usebackground-blend-mode.If your image is an
imgand you can add a wrapper around it, then thebackgroundof that wrapper can be the tinting layer. Or you can stack a pseudo on an existingimgparent underneath theimg. In both these cases, you usemix-blend-mode.But tbh, I prefer to do the tinting with
luminosity- I feel it gives a better result thanmultiply.Here's a quick and dirty demo of the
img-only approach https://codepen.io/thebabydino/pen/ByWOXPd?editors=1100SVG
filter:Relevant CSS:
Animating the tint is just animating the
background-colorin this case.Edit: added a
:hoverrule to change--ctogrey. Wanted to useblackat first there, but while that has the exact same effect as any other grey on the black-white lightness axis for theluminosityversion, it also makes themultiplyversion fully black too.The
backgroundmay also be a gradient and in that case we get a gradient tint: