Skip to content

fix: return a uint8 tuple for scalar input with bytes=True - #168

Merged
tlambert03 merged 1 commit into
pyapp-kit:mainfrom
tlambert03:fix-scalar-bytes
Oct 20, 2026
Merged

tlambert03 merged 1 commit into
pyapp-kit:mainfrom
tlambert03:fix-scalar-bytes

Conversation

@tlambert03

Copy link
Copy Markdown
Member

fixes #153 by going with option 2 from the discussion there (thanks @matthiasschabel): scalar input with bytes=True now returns a tuple of four np.uint8, exactly like matplotlib does, rather than raising.

In [1]: from cmap import Colormap

In [2]: Colormap("viridis")(0.5, bytes=True)
Out[2]: (np.uint8(32), np.uint8(144), np.uint8(140), np.uint8(255))

In [3]: Colormap("viridis")(0.5)  # unchanged
Out[3]: #21918C

I went with the tuple (rather than a (4,) array) to match mpl's scalar behavior... it also means cm(0.5, bytes=True) == (32, 144, 140, 255) just works and the result is hashable. The only thing you lose vs an array is .dtype. (I didn't reuse RGBA8 here, since its alpha is a float in 0-1, while bytes mode gives alpha in 0-255.)

The overloads now split on bytes, so type checkers see Color for bytes=False and tuple[np.uint8, ...] for bytes=True.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.91%. Comparing base (0e5d91e) to head (1bf8413).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #168   +/-   ##
=======================================
  Coverage   95.90%   95.91%           
=======================================
  Files         168      168           
  Lines        2248     2252    +4     
=======================================
+ Hits         2156     2160    +4     
  Misses         92       92           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tlambert03
tlambert03 merged commit a9eab0b into pyapp-kit:main Oct 20, 2026
25 checks passed
@tlambert03
tlambert03 deleted the fix-scalar-bytes branch October 20, 2026 10:53
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.

Colormap.__call__ with a scalar and bytes=True raises ValueError

1 participant