Skip to content

feat: add color_names and remove the wildcard from name2color - #249

Merged
petercorke merged 4 commits into
mainfrom
feat/color-names
Nov 23, 2026
Merged

petercorke merged 4 commits into
mainfrom
feat/color-names

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Adds color_names() and removes the wildcard feature from name2color().

name2color() returned a color value, a list of names (when the name contained ., ? or *) or None. That made its return type awkward: three callers needed a # type: ignore or an Any. The wildcard test was also unreliable. A name was treated as a pattern only if it contained one of three characters, so ^red or [ab] returned None, and the pattern was matched with re.match, which only anchors the start.

  • color_names(pattern=None) returns every Matplotlib color name, or those whose whole name matches a Python regular expression (re.fullmatch). The docstring states, in an .. important:: block, that this is a Python regular expression and not a shell wildcard: .*coral.*, not *coral*. A pattern that is not a valid regular expression raises ValueError whose message says * and ? are not shell wildcards.
  • name2color() raises ValueError for a name with a wildcard, and the message names color_names(). Its return type is now ndarray | None. Both changes have .. versionchanged:: 2.5.0.
  • The color2name() docstring said a color name may contain a wildcard such as "?burnt", which as a regular expression is an error. That line is removed.
  • The book's chapter 10 example name2color(".*coral.*") becomes color_names(".*coral.*") in tests/RVC/chap10.ipynb. This is a change to the printed book code, so it needs an errata entry (the maintainer is adding one).

Stacked on the colorspace fix (PR for bug/name2color), which has to merge first. Tests cover listing all names, patterns, whole-name matching, alternatives, no match, every name being accepted by name2color, and the two error messages. mypy reports no issues and the full suite passes (2127 passed, 33 skipped).

AI-assisted: written with Claude Code (Claude Sonnet 5.5) and reviewed by the maintainer.

🤖 Generated with Claude Code

petercorke and others added 2 commits November 23, 2026 15:48
…locus

name2color caught ValueError around the whole conversion, so an unknown
colorspace returned None, the same as an unknown color name.
plot_spectral_locus tested colorspace in ("rg"), a substring test, so "r",
"g" and "" were accepted as rg, and any other value raised
UnboundLocalError instead of the ValueError its docstring promises.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
name2color returned a color value, a list of names (for a name containing
., ? or *) or None, which made its return type awkward for callers.
color_names(pattern) lists the Matplotlib color names, all of them or those
whose whole name matches a Python regular expression. name2color now raises
ValueError for a name with a wildcard, pointing to color_names. The chap10
notebook uses color_names for the coral example.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

codacy-production Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 16 complexity · 0 duplication

Metric Results
Complexity 16
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

petercorke and others added 2 commits November 23, 2026 16:27
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@petercorke
petercorke merged commit 031611f into main Nov 23, 2026
32 checks passed
@petercorke
petercorke deleted the feat/color-names branch November 23, 2026 07:20
@github-actions github-actions Bot mentioned this pull request Nov 23, 2026
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.

1 participant