summaryrefslogtreecommitdiffstats
path: root/src/corelib/plugin/qlibrary_win.cpp
diff options
context:
space:
mode:
authorMarc Mutz <[email protected]>2025-12-05 17:34:24 +0100
committerMarc Mutz <[email protected]>2025-12-17 12:14:17 +0000
commit780208a5b3711db192efa830a731fc96a7e83c0a (patch)
tree774676e074241fde893b3f741dabe0e128d80c4b /src/corelib/plugin/qlibrary_win.cpp
parenta8f3e8202bad97b9426db89608013ab3e395ef4a (diff)
QColor: rewrite toHsv() to avoid float comparisonsHEADdev
The input data is integral. So keep using integral types for as long as possible and only convert to float when performing a calculation that requires fractional results. This avoids the need for qFuzzyCompare(), which has a pre-condition that neither of its argument must be zero, which an RGB value cannot guarantee. Here is why this works: The old code immediately scaled the r, g, b values from 0...USHRT_MAX to 0..1 float values. So, r, g, b, min, max, and delta variables were rescaled. The new code doesn't scale said variables. So fractions (like `delta / max` or `(g - b) / delta` remain mathematically the same (and numerically, too, if the division is done in FP), and so both `hue` as well as `saturation` don't change value. There's another copy of this code that needs the same treatment (for HSL), but they are sufficiently different from each other to take that in a follow-up patch. Also: - Move the assertion into the third leg of the if-else chain. Now that we use precise integer arithmetic, it cannot happen that none of r, g, b are equal to max({r,g,b}), so if it wasn't equal to r or g, it must be equal to b. This allows us to leave `hue` partially-formed, so the compiler can warn about it being used uninitialized. - clean up overparenthesization, whitespace and make the individual legs of the if-else chain look more similar by using an explicit 0 literal in the first Drive-by changes: - use std::minmax(initializer_list) instead of our own Q_MIN_3/Q_MAX_3 macros (the macros can't be removed, yet, as they're still used in toHsl()). - reduce the scope of `delta`, after replacing the check for 0 with an integral one As a consequence of using less FP arithmetic, tst_bench_qcolor's toHsv() runtime goes down from 230ms (best-of-10) to 160 (ditto), a whopping 40% reduction. Amends the start of the public history. Pick-to: 6.11 6.10 6.8 6.5 Task-number: QTBUG-142020 Change-Id: I370e8a214e48479b0c6fd0e48eb8f43c66920103 Reviewed-by: Edward Welbourne <[email protected]>
Diffstat (limited to 'src/corelib/plugin/qlibrary_win.cpp')
0 files changed, 0 insertions, 0 deletions