Skip to content

Colors given as tuple of ints are not working anymore for LinearColormap and StepColormap since v0.8.0 #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Spyromain opened this issue Feb 27, 2025 · 6 comments · May be fixed by #192
Labels

Comments

@Spyromain
Copy link

When I create a LinearColormap or a StepColormap with a list of tuples of ints as argument for the colors parameter, it is no longer converted to floats and leads to incorrect results.

Current behaviour (v0.8.0 v0.8.1):

>>> from branca.colormap import LinearColormap
>>> colormap = LinearColormap(colors=[(128, 64, 255), (255, 128, 0)])
>>> colormap.colors
[(128, 64, 255, 1.0), (255, 128, 0, 1.0)]
>>> colormap.rgb_bytes_tuple(0)
(32767, 16383, 65279)
>>> colormap.rgba_floats_tuple(1)
(255, 128, 0, 1.0)

Expected behaviour (v0.7.2):

>>> from branca.colormap import LinearColormap
>>> colormap = LinearColormap(colors=[(128, 64, 255), (255, 128, 0)])
>>> colormap.colors
[(0.5019607843137255, 0.25098039215686274, 1.0, 1.0), (1.0, 0.5019607843137255, 0.0, 1.0)]
>>> colormap.rgb_bytes_tuple(0)
(128, 64, 255)
>>> colormap.rgba_floats_tuple(1)
(1.0, 0.5019607843137255, 0.0, 1.0)

This seems to be due to the internal _parse_color function that was broken in 8a8a214#diff-6886114fc8e4fd6197705978c768e1f49469e2455fb1c4a8efda26a7b11bfa0bL51.

Current behaviour (v0.8.0 v0.8.1):

>>> from branca.colormap import _parse_color
>>> _parse_color((128, 64, 255))
(128, 64, 255, 1.0)

Expected behaviour (v0.7.2):

>>> from branca.colormap import _parse_color
>>> _parse_color((128, 64, 255))
(0.5019607843137255, 0.25098039215686274, 1.0, 1.0)
@Conengmo Conengmo added the bug label Mar 15, 2025
@Conengmo
Copy link
Member

Sorry about that @Spyromain, and thanks for bringing this to attention!

@Conengmo Conengmo linked a pull request Mar 15, 2025 that will close this issue
@Conengmo
Copy link
Member

@Spyromain I made a PR that should fix your issue. Would you be able to review it?

@Spyromain
Copy link
Author

@Conengmo, thank you for fixing it!

Just wanted to point out that there is another bug that was already present in the previous versions and will still be there with this condition in the PR: https://github.com/python-visualization/branca/pull/192/files#diff-6886114fc8e4fd6197705978c768e1f49469e2455fb1c4a8efda26a7b11bfa0bR80.

Tuples of ints with only zeros or ones will not be normalized when converted to floats.

Current behaviour:

>>> from branca.colormap import _parse_color_as_numerical_sequence
>>> _parse_color_as_numerical_sequence((0, 0, 1))
(0.0, 0.0, 1.0, 1.0)

Expected behaviour:

>>> from branca.colormap import _parse_color_as_numerical_sequence
>>> _parse_color_as_numerical_sequence((0, 0, 1))
(0.0, 0.0, 0.00392156862745098, 1.0)

@Conengmo
Copy link
Member

Great point! If a user provides ints between 0 and 1, we should assume they are bytes with a range of 0 - 255 and normalize accordingly. If they are floats between 0 and 1 we should assume they are already normalized. I'll update the code and add a test case for it.

@Conengmo
Copy link
Member

@Spyromain I updated the PR to address the case you brought up. Could you perhaps take another look? Hopefully it's all good now, but if you see anything that's not as expected I'd love to hear it.

@Spyromain
Copy link
Author

@Conengmo, seems all good to me, thanks again! 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants