-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Simplify RGB to HSV conversion #8432
base: main
Are you sure you want to change the base?
Conversation
For the double fmod(double x, double y) {
return x - y * trunc(x / y);
} Since |
I've been trying to tweak this code for long enough that I forgot the |
From what I can see, there are four basic changes here.
|
re: 1., I think declarations at the start of a block was required by the C89 standard, but is no longer by modern compilers. And I find declaring on first use more readable, rather than have to refer back. |
Originally I did it because the existing comment confused me, thinking negative values weren't being handled correctly. But the new code does replace
Because of the
|
To be clear, you're saying that this PR should not adjust the output data? |
Correct |
To see if the output stayed the same, I converted hopper to HSV, and saved the output to a file for comparison - radarhere@8811b00 / https://github.com/radarhere/Pillow/actions/runs/11159666541 When I introduced this change, the test started failing - meaning that this does change the output. |
See also python/cpython#124880 for a similar CPython change to |
25acdbf
to
c24ece0
Compare
By inlining some values, the starting hue calculation can be reduced from using three subtractions and two divisions to using one subtraction and one division. "fmod(n+1,1)" can be replaced with "n - floor(n)" due to the second parameter being 1. The saturation calculation can be done without floating point values. The "CLIP8"s are unnecessary because the values are already in the correct range.
An incorrect hue was being calculated when "h/6 is negative" because Python's
%
and C'sfmod
use a slightly different strategy. So to match Python, we need to implement it ourselves. It can also be simplified some due to the second parameter being 1.The starting hue calculation has also been simplified by inlining some values. ex.: