Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try SVG-based duotone filter #88

Merged
merged 9 commits into from
Sep 1, 2020
Merged

Try SVG-based duotone filter #88

merged 9 commits into from
Sep 1, 2020

Conversation

ajlende
Copy link
Contributor

@ajlende ajlende commented May 5, 2020

@jasmussen This is what I was able to get done today. It's still very broken, but I found a way to get the server-side rendering mostly working.

The ids are a little wonky, the filter is applied to the placeholder, it bleeds outside of the image in Chrome, it doesn't render at all in Firefox, and there isn't a way to disable them at the moment. But there are duotone filters and you can change the color of them. 🙂

There's a lot of other effects that can be done and this technique could be applied to any block. Going the SVG filter route also opens up the possibility of tweaking things like gamma and contrast on the duotone and adding more colors to the gradient.

Related to #12 and #13

duotone

image

image

@jasmussen
Copy link
Member

OMG this is so good.

I'm going to see what tweaks I can push!

Screenshot 2020-05-05 at 09 38 04

@jasmussen
Copy link
Member

This is really cool. I'm impressed you managed to build this as a plugin.

One challenge, though — the filter should ideally be attached to only the element that is to receive the filter. In the case of the Image block, it's the img. That will also avoid the resize handles and caption being filtered.

In the case of the Cover block it's a bit more tricky as it's the block itself that holds the background image, i.e. the filter will affect child blocks also. We could potentially start with the Image block and then refactor the cover block to be more amenable to this type of filtering.

I managed to fix that extra padding around the image, that was curious indeed and to be quite honest I'm not sure why my fix works.

I created https://codepen.io/joen/pen/RwWxZpN?editors=1100 to debug this — it appears the feColorMatrix was the cause of it. When I use the identical color matrix as I created for the previous test codepen, everything is fine, even the colors. Look:

extra padding

Again, I don't know why this works, but it appears to.

@jasmussen
Copy link
Member

I managed to get it working in Firefox.

Turns out, for an element to be able to be filtered by an SVG filter, that SVG must not be display: none;. So I applied an array of other tricks that should ensure the SVG is invisible to the user, but technically visible to the filter.

Screenshot 2020-05-05 at 10 36 09

@ajlende
Copy link
Contributor Author

ajlende commented May 5, 2020

I think I know why your fix worked. I thought it would be okay to set the alpha to a constant 1 by making the last row 00001 changing it back to 00010 uses the alpha from the image. When they're calculating the filter, they must be doing it on a canvas larger than the image (maybe a power of two size in pixels for efficiency when sending to the GPU). The padding shows the edge of that canvas.

The special values I had in the rows above are weighted values for producing a more natural looking grayscale instead of just taking the average of the values. They're the values for calculating luminance when converting to a YUV color space https://www.mathworks.com/help/matlab/ref/rgb2gray.html#algorithms

@jasmussen
Copy link
Member

Too awesome, feel free to restore your weighted values! I did not mean to overwrite your work, so absolutely go with the better values if you hvae the larger matte worked out.

@ajlende ajlende force-pushed the try/duotone-filter branch 2 times, most recently from ccfe04e to 366df38 Compare September 1, 2020 18:25
@ajlende ajlende marked this pull request as ready for review September 1, 2020 20:47
@ajlende ajlende changed the title Add SVG-based duotone filter Try SVG-based duotone filter Sep 1, 2020
@ajlende ajlende merged commit 778e3c4 into master Sep 1, 2020
@ajlende ajlende deleted the try/duotone-filter branch September 1, 2020 20:49
@ajlende
Copy link
Contributor Author

ajlende commented Sep 1, 2020

The block filter approach can only apply duotone to an entire block at a time, so I'll be opening another PR with a standalone block with only the image part filtered

@ajlende ajlende mentioned this pull request Sep 9, 2020
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants