vendredi 9 avril 2021

How do I test if a value is a valid matplotlib color specification?

In the spirit of defensive programming, I would like to test if a given user input is a valid matplotlib color specification.

Matplotlib colors can typically be specified in a variety of ways:

  • strings (e.g. 'b', 'blue', '#ff5733')
  • floats (e.g. 0.5)
  • RBG (e.g. np.array([0, 0, 0]))
  • RGBA (e.g. np.array([0, 0, 0, 1]))

Testing for the type and shape of the input is not enough, as not all strings are valid colors, and not all values are acceptable float/RGB/RGBA color specifications.

All of this is further complicated by the fact that many matplotlib functions create several artists, such that iterables of these color specifications are also often valid. However, I would already be content if I could just test if a color specification is valid for a single PathPatch artist (without actually drawing the artist).

Aucun commentaire:

Enregistrer un commentaire