shahinrostami / plotapi

Engaging visualisations, made easy.
https://plotapi.com
MIT License
267 stars 17 forks source link

Add ColorScheme to make it easier to find valid color schemes. #2

Closed philippeitis closed 3 years ago

philippeitis commented 4 years ago

This adds ColorScheme, which includes all supported color schemes as ColorScheme.name, and has a constructor which will change the color scheme strings as needed. This is intended to help discovery, (eg. IDE will show available colors, or you can look in __init__.py instead of going to d3 documentation, which won't specify that d3.scheme____ is required), and ensure that the user knows why their color choice failed.

>>> ColorScheme(5).Blues
d3.schemeBlues[5]

It also checks that d3 supports the given value of k (length) for the given color scheme, and raises an error if that is not the case - which is helpful if you get a blank page and don't know why.

>>> ColorScheme(2).Blues
AttributeError: d3.schemeBlues requires 3 <= k <= 11 (got 2)

Additionally, you don't need to call len(), so it's as easy as

>>> ColorScheme(["name1", "name2", "name3"]).Blues
d3.schemeBlues[3]

The following blocks show equivalent usages.

Chord(matrix, names, colors=f"d3.schemePuRd[{len(names)}]").show()
Chord(matrix, names, colors=ColorScheme(names).PuRd).show()
Chord(matrix, names, colors="d3.schemeSet3").show()
Chord(matrix, names, colors=ColorScheme.Set3).show()
shahinrostami commented 4 years ago

This looks great. I’ll take a closer look at it soon! Thank you

shahinrostami commented 3 years ago

Thank you for proposing the colour class! Coming soon is a solution that doesn't rely on the d3 colours.