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

Applying CSS Contrast + Brightness Adjustment #1958

Closed
samdesota opened this issue Nov 8, 2019 · 5 comments
Closed

Applying CSS Contrast + Brightness Adjustment #1958

samdesota opened this issue Nov 8, 2019 · 5 comments

Comments

@samdesota
Copy link

samdesota commented Nov 8, 2019

Hi, I've researching for a few hours how to apply a CSS filter operations brightness and contrast with sharp, but I haven't been able to succeed at both at the same time.

CSS brightness is simply a multiply operation I believe, so linear is ax + b, then:

image.linear(brightness)

For contrast, I ran across this: https://www.w3.org/TR/filter-effects/#contrastEquivalent, combining with a stack overflow result I was able to get this, which appears to work:

image.linear(contrast, -(128 * contrast) + 128);

However, for the life of me, I can't get these 2 operations to combine with correct results. It appears that the second linear call overwrites the first, is that correct behavior? I don't see it documented but perhaps I missed it.

So, I tried to apply both operations in one call via replacement, i.e.:

f (x) = b * x
g (x) = c * x + (-(128 * c) + 128)
f(g(x)) = (b * c * x) + (b * (-(128 * c) + 128))

To js:

image.linear(brightness * constrast, brightness * (-(128 * contrast) + 128));

But that didn't work either, perhaps my logic is off here.

Finally, I attempted to combine operation from above with the modulate brightness function to solve the brightness multiplier, but changing the brightness for some reason changes the hue of the image, or perhaps I don't understand the purpose of modulate.

So I'm left pretty lost in a field I don't know much about. Any pointers would be appreciated, and if there's any way that the sharp api could be extended to apply these 2 common option easily, that would be much appreciated, I know at least a few people have this use case.

@lovell
Copy link
Owner

lovell commented Nov 9, 2019

I would expect your suggested approach of using the modulate operation to adjust brightness (it uses to the linear LCH colourspace internally so should be more visually accurate) and the linear operation to adjust contrast to give the best results.

Do you have an example (code and images) of the unexpected hue change? It's always possible there might be some kind of rounding bug/error.

@samdesota
Copy link
Author

samdesota commented Nov 9, 2019

Yup, here's a self-contained example:

https://github.com/mrapogee/sharp-images-test

to run, you can

git clone https://github.com/mrapogee/sharp-images-test
cd sharp-images-test
npm install
npm run test

That will open index.html in your browser and show the difference with CSS filters. If you're not on macos, you can just open index.html in the browser yourself.

it uses this code:

const contrast = 1.5;
const brightness = 0.5;

await sharp(inputFile)
    .linear(contrast, -(128 * contrast) + 128)
    .modulate({ brightness: brightness })
    .toFile(outputFile);

Result: left is using css filters, right is using the sharp pipeline above

Screen Shot 2019-11-09 at 12 29 23 PM

@lovell
Copy link
Owner

lovell commented Nov 11, 2019

Thank you, this is a great example that led me straight to the problem, which was that the modulate operation was not ensuring the right (internal) colourspace for other colour-based operations.

Commit 833aaea fixes this and adds a unit test to prevent regression.

@lovell lovell added this to the v0.23.3 milestone Nov 11, 2019
@lovell
Copy link
Owner

lovell commented Nov 17, 2019

v0.23.3 now available, thank you for reporting this.

@lovell lovell closed this as completed Nov 17, 2019
@samdesota
Copy link
Author

🙌 Thank very much, appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants