Skip to content

fix: handle scalar input with bytes=True in Colormap.__call__ - #154

Open
abhi-0203 wants to merge 1 commit into
pyapp-kit:mainfrom
abhi-0203:fix/scalar-bytes-call
Open

fix: handle scalar input with bytes=True in Colormap.__call__#154
abhi-0203 wants to merge 1 commit into
pyapp-kit:mainfrom
abhi-0203:fix/scalar-bytes-call

Conversation

@abhi-0203

Copy link
Copy Markdown

Summary

Fixes Colormap.__call__ raising ValueError when called with a scalar input and bytes=True.

Problem

When bytes=True, the LUT is converted to uint8 before indexing. For scalar input, the result is a 4-element uint8 array (e.g. [32, 144, 140, 255]), which is then passed to Color(). But Color() (via parse_rgba) only accepts:

  • integer arrays of length 3 (RGB)
  • float arrays of length 3 or 4

A uint8 array of length 4 doesn't match either condition, raising ValueError: Invalid color array.

cmap(0.5, bytes=True)  # raises ValueError

Fix

When bytes=True and the input is scalar, normalize the uint8 RGBA values back to [0, 1] float range before constructing Color(). The quantization to 8-bit is preserved — Color(rgba / 255) gives #20908C vs #21918C in float mode, showing the bytes=True effect.

Test results

>>> cmap(0.5, bytes=True)
#20908C  # was ValueError before
>>> cmap(0.5)
#21918C  # unchanged
>>> cmap(np.array([0.5]), bytes=True)
[[ 32 144 140 255]]  # unchanged

Closes #153

When bytes=True and a scalar input is provided, the LUT is converted to
uint8 before indexing. The resulting 4-element uint8 array was passed
directly to Color(), which expects float values in [0, 1]. This raised
ValueError: 'Invalid color array: array([32, 144, 140, 255], dtype=uint8)'.

Fix: when bytes=True and the input is scalar, normalize the uint8 RGBA
values back to [0, 1] float range before constructing Color(). The
quantization to 8-bit is preserved in the rounding.

Closes pyapp-kit#153
@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.67%. Comparing base (02fa073) to head (03fa2ff).

Files with missing lines Patch % Lines
src/cmap/_colormap.py 80.00% 1 Missing ⚠️

❌ Your patch check has failed because the patch coverage (80.00%) is below the target coverage (85.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #154      +/-   ##
==========================================
- Coverage   95.71%   95.67%   -0.04%     
==========================================
  Files         168      168              
  Lines        2192     2196       +4     
==========================================
+ Hits         2098     2101       +3     
- Misses         94       95       +1     

☔ 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.

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