Pre-existing high-contrast palettes from the ANSI color set, to use in a terminal app?

Question:

Looking to convey more information in a Rich Table by using colors (specifically to track which modules given classes come from).

On the web, it’s fairly easy on find color palettes that are optimized for contrast, rather than esthetics. Here’s a 6 color example. Then it’s just question of using the RGB/HSL specs to drive your CSS.

Rich has a nice list of ANSI colors in rich.colors.ANSI_COLOR_NAMES. But there is no indication of what colors would constitute a high-contrast 10-12 color palette.

Are there such lists available, for ANSI colors to be used in terminal apps? Or should I just find a web palette and use rich.colors.Color.from_rgb() to build myself such a palette?

Asked By: JL Peyret

||

Answers:

OK, well, that was simple, no need to mess around with from_rgb, because of what styles support.

And, after reading Will’s answer, I’ve modified my original solution to use saved themes. Thanks again for an awesome library, Will!

Alternatively you can use a CSS-like syntax to specify a color with a “#” followed by three pairs of hex characters, or in RGB form with three decimal integers. The following two lines both print “Hello” in the same color (purple):

console.print("Hello", style="#af00ff")

Found a 20-element list here (last 2 are black and white which I am skipping).

hivis.theme.ini
[styles]

;  High constrast palette from 
;  https://sashamaps.net/docs/resources/20-colors/

hivis0 = #e6194b
hivis1 = #3cb44b
hivis2 = #ffe119
hivis3 = #4363d8
hivis4 = #f58231
hivis5 = #911eb4
hivis6 = #46f0f0
hivis7 = #f032e6
hivis8 = #bcf60c
hivis9 = #fabebe
hivis10 = #008080
hivis11 = #e6beff
hivis12 = #9a6324
hivis13 = #fffac8
hivis14 = #800000
hivis15 = #aaffc3
hivis16 = #808000
hivis17 = #ffd8b1
hivis18 = #000075
hivis19 = #808080
;hivis20 = #ffffff
;hivis21 = #000000
script.py
from rich.console import Console
from rich.theme import Theme

with open("hivis.theme.ini") as fi:
    theme = Theme.from_file(fi)

palette = [name for name  in theme.styles.keys() if name.startswith("hivis")]

console = Console(theme=theme)
console.print("[hivis1] First Hivis.  [hivis2] Second Hivis.")
for ix, rgb in enumerate(palette):
    console.print(f"[{palette[ix]}] {rgb}")

enter image description here

Answered By: JL Peyret

Consider using Rich’s themes to give names to your colors. That way you won’t have to reference your list. You can do print("Hello [color3]World![/]")

Answered By: Will McGugan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.