-
Notifications
You must be signed in to change notification settings - Fork 61
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
Implement CIEDE2000 color difference for Lab/Lch #162
Conversation
Cool! It's going to take me a bit to review the math heavy part. But I appreciate the added tests a lot! That's always nice. My understanding is that there's a conversion to Lch somewhere in there. I wonder if it would be possible to piggyback on that fact and implement it for both types at the same time. Maybe even simplify it a bit. Considering they are essentially the same color space, I would suggest implementing it for both in either case. Other than that, you may want to run it through |
The original Lab implementation took a few iterations and was harder than I expected. I started with trying to port the Wiki equations but consulted this paper and http://brucelindbloom.com/index.html?Eqn_DeltaE_CIE2000.html to clarify some details.
|
The build fails on the Linux CI machines but passes on Mac. On my current Windows desktop it passes
Page 24 of the paper (4 of the pdf) has the test data (expecting
|
I do appreciate all the hard work you put into this! I hope my previous comment didn't come out as dismissive. I was tired and not in shape for reading calculations. Regarding the formatting, I see your point, and doing a formatting run may be a good idea. That should probably be done periodically, even, as not everyone uses There's an attribute you can add to the function to skip formatting there. That will preserve your custom formatting locally. It's much better than the alternative. As for using Lch to make it simpler, that was just a guess. I wasn't sure how much of the conversion would be possible to reuse. That error you found is suspiciously large, yes. I have seen differences between the Mac and Linux runners before, though. That's why approximate equality is often used. But one would think the error would be smaller... If it's the test you quoted, it sounds like Linux has a less accurate |
134c076
to
90645b6
Compare
No worries, I didn't read it that way and I learned something from it 👍 I added an attribute to skip formatting the For the floating point, I can imagine there's a large amount of truncation or rounding due to the extra conversions involved. For the Lch test, I convert the original Lab color data to Lch. Then during // CIEDE2000 test
for expected in data.iter() {
let result_lab = expected.c1.get_color_difference(&expected.c2);
assert_relative_eq!(result_lab, expected.delta_e, epsilon = 0.001);
let lch1 = Lch::from(expected.c1);
let lch2 = Lch::from(expected.c2);
let result_lch = lch1.get_color_difference(&lch2);
assert_relative_eq!(result_lch, expected.delta_e, epsilon = 0.001);
} It's not ideal but could we have a larger epsilon in the Lch test for tests run on Linux? Beyond that, I'm not sure what else I can do to determine the root cause that would be worth the time. |
Oh, so the error is only for Something else I was about to suggest, is to split the code into one part that gets the parameters from each format, and then sends them into a shared function. If we can isolate the "preparation" part from the parts they have in common, I think both maintenance and trouble shooting will be easier. |
f64 worked! Separating the parameters from the calculation is a good suggestion to do anyway. I will try to move the main calculation into a shared function which accepts parameters from preparation functions. |
Great! 🎉 It's probably good to add a small comment that explains why the test is performed like that, just for future adventurers. It's easy to forget things like this. Other than that, I think merging the implementations would be the last thing that's left to do. |
I separated the shared code to its own function but I wasn't sure of the best way for it to be shared by both Lab and Lch. |
You can make a private |
b822562
to
a6063fe
Compare
Alright, nice! I think it's probably good to squash all of this now when there are a few edits. It's self contained enough for a single commit. Then I'm happy to merge when you are. |
Did you have more to review or I should squash it now? |
I looked through it just now and I think it's good to go. I'm not going to go into details about naming and formatting here. Translating formulas to code is always weird an it's always possible to polish it later. |
Add ColorDifference trait and module Add csv data and tests
a6063fe
to
2f62c57
Compare
I understand. Science and math programming tends to be ugly in my experience, especially with formulas as complicated as this. I'd follow any guidelines or preferences you have in the future but didn't see any immediate examples in the codebase. I leaned toward explicitness and trying to stay close to the variables as I transcribed them knowing they can be safely cleaned up later. I'm glad it works and thanks for the guidance. |
I agree and it makes it easier to compare to the reference material. I prefer to start like this and improve incrementally. You are welcome to come back to this and iterate on it if you want to, but I think this is good for version 1. Thanks for taking the time to implement it! It's very much appreciated! bors r+ |
Build succeeded |
Add ColorDifference trait
Add csv data and test
Closes #143