Is there any library that can convert RGB colors to ANSI colors?

John Machin sjmachin at lexicon.net
Sun Nov 30 05:07:15 EST 2008


On Nov 30, 8:04 pm, John Machin <sjmac... at lexicon.net> wrote:
> On Nov 30, 4:39 pm, ZelluX <zel... at gmail.com> wrote:
>
> > Convert RGB colors to the closest ANSI colors. For example, given RGB
> > color FF0000, it should print [31m.
>
> Maybe ... but you could write it yourself quickly enough; the code is
> a trivial loop over a list of the RGB values of the 8 possible
> colours, and would fit easily on a 24x80 terminal :-) Why don't you
> have a try at it and come back if you have any problems?
>

Ummm actually once you have got your rgb value as an integer (or a
tuple of 3 integers) -- in the example, 0xff0000 or (0xff, 0, 0) --
the answer can be obtained in one line with operations no more
complicated than shifting, masking, and addition.



More information about the Python-list mailing list