Repository navigation
Fix dist_cie2000/dist_cie94 to implement the published CIE formulas - #425
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #425 +/- ##
=======================================
Coverage 97.76% 97.76%
=======================================
Files 13 13
Lines 4118 4118
Branches 697 699 +2
=======================================
Hits 4026 4026
Misses 70 70
Partials 22 22 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
It seems like you have a better understanding of the color algorithms than we do, but two things.
|
|
Hi avylove, thanks for taking a look — happy to back both points up. The published data. The formulation and the test vectors both come from Sharma, Wu & Dalal (2005), "The CIEDE2000 Color-Difference Formula: Implementation Notes, Supplementary Test Data, and Mathematical Observations", Color Research & Application 30(1), 21–30:
The double function calls. If you mean the two G-shaped (If you instead meant the |
|
Thanks for the response and the PR. I actually meant the wrappers. They don't seem to add anything except additional function calls. In that same vein, there's some optimization that can be done. A few things stand out:
I feel it's better to make the code as efficient as you can and then explain it in comments. Removing the extra function call and optimizing the math, might make the code 20 - 50% faster. I would also put the reference links in the comments And fix the linting and docformatter failures But from what I can tell, this does seem to fix the symmetric distance issue and the color tables aren't drastically different. |
|
Hi @avylove — thanks, all applied in 4a345d8, and your estimate was on target: CIE94 measures ~48% and CIEDE2000 ~26% faster on my benchmark, with bit-identical outputs on every pair I checked. The wrappers are gone. Math optimizations, as you listed: dropped the multiplications by the factors assigned to 1 (k_L/k_C/k_H and S_L in CIE94), replaced References in comments: the CIE94 section link and the Sharma/Wu/Dalal (2005) paper (with its implementation-notes URL) now sit in the comments next to the math they document. Linting and docformatter: the isort failure was the import order (the length-sorted style) and the docformatter failure was in the removed helper docstrings — both fixed. flake8, isort, docformatter, mypy, pylint, pydocstyle and codespell all pass locally with the CI-pinned tools. The new revision's Actions run needs a maintainer approval to start (fork PR), so it will go green as soon as someone approves the workflow. |
avylove
left a comment
There was a problem hiding this comment.
I think this looks great! I added a couple more places it could be optimized and you'll also want to look at where you have integers that are operating against floats. Really all the numbers in the code should be floats, so 2.0 instead of 2. This lets the PEP 659 improvements introduced in Python 3.11 work a little better.
| # G = 0.5 * (1 - sqrt(c_mean**7 / (c_mean**7 + 25**7))) | ||
| c_1 = sqrt(a_1 * a_1 + b_1 * b_1) | ||
| c_2 = sqrt(a_2 * a_2 + b_2 * b_2) | ||
| c_7 = ((c_1 + c_2) * 0.5) ** 7 |
There was a problem hiding this comment.
This could be collapsed into
c_7 = ((sqrt(a_1 * a_1 + b_1 * b_1) + sqrt(a_2 * a_2 + b_2 * b_2)) * 0.5) ** 7.0
| r_t = (-2 * sqrt(c_7p / (c_7p + 25 ** 7)) * | ||
| sin(radians(60 * exp(-(((h_mean - 275) / 25) ** 2))))) |
There was a problem hiding this comment.
| r_t = (-2 * sqrt(c_7p / (c_7p + 25 ** 7)) * | |
| sin(radians(60 * exp(-(((h_mean - 275) / 25) ** 2))))) | |
| d_theta = (h_mean - 275.0) * 0.04 | |
| r_t = (-2.0 * sqrt(c_7p / (c_7p + 25.0 ** 7.0)) * sin(radians(60.0 * exp(-(d_theta * d_theta))))) |
|
Thanks @avylove — really glad the formulation checks out! Happy to apply the float-literal pass (2 → 2.0) and the inline suggestions in a small additive commit if you'd like them included in this PR; otherwise they can land in a follow-up and this is ready from my side whenever you are. |
|
@rahul05ranjan I think it needs another commit. There's also still a linting failure. You can ignore the CodSpeed issue. that looks like a config problem. |
|
Done — The linting failures were pylint on the new tests: the symmetric-distance assert read as arguments-out-of-order (W1114 — now split into named |
|
Following up on the linting note: |
|
There are still a couple unresolved comments and the pylint issue is better fixed by renaming the arguments. If you used rgb-a and rgb-b as the argument names it would probably get rid of the warning without having to disable it. It's only firing because the variable names are the same as the argument names. |
|
Thanks @avylove — all applied in
Also updated the |
|
I suggested renaming the arguments, not the parameters. Now the parameters don't match the other functions. |
|
Understood, thanks for the correction — fixed in 9580e84: |
avylove
left a comment
There was a problem hiding this comment.
Thanks for the updates and sorry for the delay. Code looks good to me, but noticed a few things in the tests. Would also be good to rebase against master so codspeed runs properly.
dist_cie2000 mixed radians from atan2 with degree constants (the hue-mean special cases were unreachable dead code), tied the rotation term to the hue difference instead of the mean hue angle (the symmetry symptom of hue term in quadrature as da**2 + db**2 + dC**2 instead of da**2 + db**2 - dC**2, inventing a hue difference for colors that only differ in chroma. Both functions now follow the formulation published by Sharma, Wu & Dalal (2005) (hue arithmetic in degrees; CIE94 graphic-arts factors unchanged), and are verified against the published supplementary test data (Table 1): pairs 1-3 reproduce 2.0425 / 2.8615 / 3.4412 exactly, dist_cie2000 is symmetric again, and the CIE94 hue term is zero for collinear-chroma Lab pairs. Fixes jquast#424 Co-authored-by: rahul05ranjan <[email protected]>
Remove the _cie94_lab/_cie2000_lab wrappers (dist_cie94/dist_cie2000 now run the full math directly, one rgb_to_lab call per argument), drop the multiplications by the k factors assigned to 1, use x * x and * 0.5 instead of x ** 2 and / 2, compute the repeated (l_mean - 50) ** 2, c_mean ** 7 and c_meanp ** 7 once, inline single-use temporaries, and put the reference links (CIE94 wiki, Sharma/Wu/Dalal 2005) in comments. Tests now feed the published Lab vectors through dist_cie2000 with rgb_to_lab patched to the identity, and assert each RGB argument is converted exactly once. Measured on 3 RGB pairs: CIE94 ~48% faster, CIEDE2000 ~26% faster; outputs are bit-identical on the pairs tested. Also fixes the isort import order and docformatter CI failures.
Per review: float literals throughout the CIE94/CIE2000 math (1 -> 1.0, 2 -> 2.0, 25 -> 25.0, 180/360 -> 180.0/360.0), and inline the single-use chroma temporaries c_1/c_2 in dist_cie2000. tests: restructure the symmetric-distance assertion so pylint no longer reads it as arguments-out-of-order (W1114), and drop the intermediate unpackings in the CIE94 reference test to come under the too-many-locals budget (R0914). All 41 color tests pass; pylint blessed and pylint_tests (CI disables) at 10.00/10. Co-authored-by: rahul05ranjan <[email protected]>
Per review: collapse the c_1/c_2 chroma temporaries into a single c_7 expression and introduce d_theta for the R_T hue term (25.0 ** 7.0). Rename dist_cie2000's rgb1/rgb2 parameters to rgb_a/rgb_b so pylint no longer matches the symmetry test's loop variables to the parameter names (W1114) and the inline disable in the test can be dropped. pylint 10.00/10 on both blessed and tests; all 41 color tests pass.
…ename the swapped arguments in the symmetry test instead
Expand the published CIEDE2000 test vectors from the first three pairs to all 34 pairs of the supplementary test data of Sharma, Wu & Dalal (2005), and add the source link for the dataset (https://hajim.rochester.edu/ece/sites/gsharma/ciede2000/). Replace the converter call-count test with its behavioral contract: an RGB pair must yield the same distance as its pre-converted Lab pair, so both arguments flow through rgb_to_lab without counting calls or depending on lru_cache. Drop test_dist_cie94_matches_reference_for_general_pair: it re-implemented the CIE94 formula to check the formula, and no freely published CIE94 test vectors exist to test against instead. The collinear-chroma property test keeps covering the hue-term fix. Co-authored-by: rahul05ranjan <[email protected]>
9580e84 to
a30fa6b
Compare
|
Thanks for the review! All three test points are addressed in a30fa6b, and the branch is now rebased onto master as requested.
The rebase onto master was clean apart from a changelog conflict in Tests on the rebased head (a30fa6b): |
|
@jquast This looks ok to me. It has an AI smell to it, but all my nits were addressed. I didn't dig into the math, but if the results are close to what we had before and it solves the symmetric issue, then it seems like it should be fine. |
|
Yeah I saw your screenshots from before, they look slightly different, I don't understand the hard math but it can't hurt, this is our 4th AI-generated "your CIE is wrong" and the only way to prevent a 5th is by merging it. Or at least you'd hope :) I get so many AI generated in wcwidth that I maintain a section of "Unlikely Sequences" in documentation about it, my stance is that if "correcting" something that is imperceptibly different prevents additional AI agents from "boiling water" over making the same PR's multiple times, then it is good to merge such changes. |
|
Appreciate the merge on #425 — nice to see "Fix dist_cie2000/dist_cie94 to implement the published CIE formulas" ship. On a personal note: I'm on the lookout for my next role (Full Stack (PHP, NodeJs, Python) / Data Science / Applied AI / Devtools, based in India). Happy to chat if there's an opening on your side or one you'd recommend: |
* change uncicode arrows -> ascii arrows * reduce monster-sized changelog item from #425
|
Don't listen to him! I have over 20 years experience in over 30 languages and 15 years of cloud, based in USA! Hire me first !! |
|
Ha — you win that exchange, Jeff! Over 20 years in 30 languages is a résumé I can't outbid, so I'll just take the consolation prize: the CIE formulas landed in the right repo with the right maintainer. 🙂 All jokes aside, thanks for the careful review on this one — good to see the corrected color distances ship. |




Proposed change
dist_cie2000anddist_cie94did not implement the published CIE formulas; this PR ports both to the canonical formulation published in Sharma, Wu & Dalal (2005), "The CIEDE2000 Color-Difference Formula: Implementation Notes, Supplementary Test Data, and Mathematical Observations".Closes #424 (and supersedes the unfixed symptom reported in #321).
dist_cie2000root causes fixedatan2()returns radians, but hue values were wrapped with% 360, compared against180/360degree thresholds (making the hue-mean special cases unreachable dead code, since|radians| <= pi < 180), and fed tocos()/sin()alongside degree constants (30,2h,3h+6,4h-63,dh/2,60*exp(...),275,25). Hue angles are now converted to degrees and the constants converted to radians.r_twas computed fromdelta_h(the ΔH magnitude) where CIEDE2000 ties it to the mean hue angleh̄'— this also restores the symmetry reported indist_cie2000is not symmetric for swapped RGB arguments #321 (dist_cie2000(a, b) == dist_cie2000(b, a)).s_c/s_h/r_tnow use the post-G chroma meanC̄'as the published formula requires (previously the pre-Gc_mean).dist_cie94root cause fixedThe hue difference in quadrature must be
dH² = Δa² + Δb² − ΔC²(chroma–hue decomposition; non-negative by Cauchy–Schwarz). The old code computedΔa² + Δb² + ΔC², inventing a hue difference for colors that differ only in chroma — e.g. Lab(50, 60, 0)vs(50, 30, 0)must beΔE₉₄ = 30/(1+0.045·60) ≈ 8.108with a zero hue term. Amax(0.0, …)guard clamps float rounding artifacts of the subtraction.Both public functions keep their signatures and "square of the distance" return semantics; the Lab math now lives in
_cie94_lab/_cie2000_labso it can be verified against published Lab test vectors directly.Verification
_cie2000_labreproduces pairs 1–3 of the Sharma et al. (2005) supplementary test data exactly —sqrt(_cie2000_lab(lab1, lab2))returns 2.0425 / 2.8615 / 3.4412 (before the fix: 1.1946 / 1.6995 / 2.2303).dist_cie2000is not symmetric for swapped RGB arguments #321 repro pair(73, 253, 26)vs(110, 13, 181)now returns identical distances either way.30/(1+0.045·60)), and a general Lab pair is pinned against the published closed form.Type of change
Checklist
docs/history.rst(1.52 unreleased section)