Skip to content

fix: guard TemperatureCache.complement against a zero temperature range#29

Merged
T-Dynamos merged 2 commits into
T-Dynamos:mainfrom
yuksblog:fix/temperature-cache-zero-range
Jul 13, 2026
Merged

fix: guard TemperatureCache.complement against a zero temperature range#29
T-Dynamos merged 2 commits into
T-Dynamos:mainfrom
yuksblog:fix/temperature-cache-zero-range

Conversation

@yuksblog

Copy link
Copy Markdown
Contributor

Fixes #28

The bug

A seed whose HCT sits at a tone extreme (#FFFFFF, #000000, #010101) collapses the gamut to a single point, so every hue in the 361-entry sweep resolves to the same color and the warmest and coldest entries are identical. complement divided by that zero range and raised ZeroDivisionError:

from materialyoucolor.hct.hct import Hct
from materialyoucolor.scheme.scheme_fidelity import SchemeFidelity

SchemeFidelity(Hct.from_int(0xFFFFFFFF), False, 0.0, spec_version="2021")
# ZeroDivisionError: float division by zero

#FFFFFF is an ordinary seed for a user to pick, so this is reachable from normal input, via get_tertiary_palette in any scheme that needs the complement.

The fix

Return the input when the range is zero: every candidate has the same temperature, so none is a better complement than another. This mirrors the zero-range case that relative_temperature already handles a few lines below.

Verification

Non-degenerate seeds are bit-for-bit unchanged. I compared every color of all 9 dynamic schemes across 5 seeds x light/dark x 4 contrast levels x both spec versions (42,480 values) before and after the change: identical.

The regression check went into tests/test_all.py, since that is what CI already runs and it fails the build on a non-zero exit — without the fix in the first commit, the second commit makes the wheel build go red. It sits ahead of the early exit on Windows so it runs on every platform. Happy to drop that commit if you would rather keep test_all.py purely visual.

yuksblog added 2 commits July 13, 2026 16:35
A seed whose HCT sits at a tone extreme (#FFFFFF, #000000, ...) collapses the
gamut to a single point, so every hue in the 361-entry sweep resolves to the
same color and the warmest and coldest entries are identical. complement
divided by that zero range and raised ZeroDivisionError, which is reachable
from any scheme that needs the complement -- SchemeFidelity, for instance,
via get_tertiary_palette.

Return the input when the range is zero: every candidate has the same
temperature, so none is a better complement than another. This mirrors the
zero-range case that relative_temperature already handles, and leaves every
non-degenerate seed bit-for-bit unchanged.
CI already runs test_all.py and fails the build on a non-zero exit, so the
degenerate seeds fit as an extra construction check alongside the existing
ones: without the preceding fix, this raises ZeroDivisionError and the wheel
build goes red.

Placed ahead of the early exit on Windows so the check runs on every platform.

@T-Dynamos T-Dynamos left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - Thanks.

@T-Dynamos
T-Dynamos merged commit 65f2bcd into T-Dynamos:main Jul 13, 2026
4 checks passed
@yuksblog
yuksblog deleted the fix/temperature-cache-zero-range branch July 13, 2026 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ZeroDivisionError in TemperatureCache.complement for seeds at a tone extreme (e.g. #FFFFFF, #000000) with SchemeFidelity

2 participants