Printing RGB colors in the terminal

← Posts · 03/04/2023 · 1 minute

Using terminal escape sequence:


def print_rgb(r, g, b):
    print('\N{ESC}[48;' + f"2;{r};{g};{b}m " + '\u001b[0m', end="")

This can be used in the following manner:

import matplotlib
    
cmap = matplotlib.colormaps["viridis"]
for i in range(10):
    print_rgb(*[int(255 * c) for c in cmap(float(i / 10))[:-1]])