Crash in `extend_colormap` when colormap contains more colors than requested
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 241
- Forks
- 9
- PR merge metrics
- No merged PRs in 30d
Description
Thanks for the nice library. I've come across a bug that occurrs when I want to extend a matplotlib colormap, e.g., `tab10`, but request fewer colors than the initial colormap contains.
For instance
```python
glasbey.extend_palette("tab10", 10)
glasbey.extend_palette("tab10", 15)
```
both work fine, but if I request something <10, e.g.,
```python
glasbey.extend_palette("tab10", 5)
```
I get the following error
```
ValueError Traceback (most recent call last)
Input In [24], in ()
1 import glasbey
----> 2 glasbey.extend_palette("tab10", 5)
File ~/miniconda3/envs/ml/lib/python3.10/site-packages/glasbey/_glasbey.py:338, in extend_palette(palette, palette_size, grid_size, as_hex, grid_space, lightness_bounds, chroma_bounds, hue_bounds, red_bounds, green_bounds, blue_bounds, colorblind_safe, cvd_type, cvd_severity)
334 if not colorblind_safe:
335 palette = cspace_convert(palette, "sRGB1", "CAM02-UCS").astype(
336 np.float32, order="C"
337 )
--> 338 palette = generate_palette_cam02ucs(colors, palette, np.uint32(palette_size))
339 else:
340 cvd_space = {
341 "name": "sRGB1+CVD",
342 "cvd_type": cvd_type,
343 "severity": cvd_severity,
344 }
ValueError: cannot assign slice from input of different size
```
I understand that it makes no sense to actually call this function when the colormap contains enough colors, but I'm trying to integrate this into my plotting utils where the number of colors I need can be highly variable, so this would be a "nice to have". If nothing else, a less crpytic error would be preferrable.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in glasbey/_glasbey.py at extend_palette around the call to generate_palette_cam02ucs shown in the traceback. Reproduce the failure with glasbey.extend_palette("tab10", 5), then inspect how palette_size is handled when it is smaller than the input palette. Confirm whether completion should truncate the palette or provide a clearer validation error, and verify that the example no longer produces the cryptic ValueError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- matplotlib, python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100