Skip to content

Commit

Permalink
Visually more uniform colortable
Browse files Browse the repository at this point in the history
High variations in saturation make the colortable somewhat non-uniform.
By splitting into low/high saturation groups, the table IMHO looks
better.
  • Loading branch information
timhoffm committed Mar 14, 2024
1 parent b1857a1 commit 3f155c8
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions galleries/examples/color/named_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@ def plot_colortable(colors, *, ncols=4, sort_colors=True):
swatch_width = 48
margin = 12

# Sort colors by hue, saturation, value and name.
if sort_colors is True:
names = sorted(
colors, key=lambda c: tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(c))))
# Sort colors: To make the visual perception more smooth, we split
# into two groups with low-saturation / high-saturation, and sort
# by hue, saturation and value within these groups.
by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))),
name)
for name, color in colors.items())

non_sat = [(hsv, name) for hsv, name in by_hsv if hsv[1] < 0.2]
sat = [(hsv, name) for hsv, name in by_hsv if hsv[1] >= 0.2]
by_hsv = non_sat + sat

names = [name for hsv, name in by_hsv]
else:
names = list(colors)

Expand Down

0 comments on commit 3f155c8

Please sign in to comment.