Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is there any library that can convert RGB colors to ANSI colors?

Reply
Thread Tools

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

 
 
ZelluX
Guest
Posts: n/a
 
      11-30-2008
Convert RGB colors to the closest ANSI colors. For example, given RGB
color FF0000, it should print [31m.
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      11-30-2008
On Nov 30, 4:39*pm, ZelluX <(E-Mail Removed)> 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?

Cheers,
John
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      11-30-2008
On Nov 30, 8:04*pm, John Machin <(E-Mail Removed)> wrote:
> On Nov 30, 4:39*pm, ZelluX <(E-Mail Removed)> 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.
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      12-01-2008
ZelluX wrote:

> Convert RGB colors to the closest ANSI colors. For example, given RGB
> color FF0000, it should print [31m.


from functools import partial

def to_rgb(color):
return (color >> 16) & 0xFF, (color >> & 0xFF, color & 0xFF

target_colors = {
0x000000: "\33[30m",
0xFF0000: "\33[31m",
# ...
0xFFFFFF: "\33[37m",}

def euclidian(c1, c2):
r, g, b = to_rgb(c1)
s, h, c = to_rgb(c2)
r -= s
g -= h
b -= c
return r*r+g*g+b*b

def closest_color(color, target_colors=target_colors, dist=euclidian):
return min(target_colors, key=partial(dist, color))

if __name__ == "__main__":
black = target_colors[0]
for color in 0xff0000, 0x00ff00, 0x808080, 0x008000:
bestmatch = closest_color(color)
code = target_colors[bestmatch]
print "#%06x --> %sSAMPLE%s" % (color, code, black)

If the results are not good enough for your application you can convert to
another colorspace before calculating the distance.

Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Are there statistics packages in ANSI C and/or ANSI C++? lbrtchx@gmail.com C Programming 11 04-28-2008 03:00 AM
Are there statistics packages in ANSI C and/or ANSI C++? lbrtchx@gmail.com C++ 1 04-24-2008 06:44 PM
Colorsmart RGB or Adobe RGB? Morton Digital Photography 2 12-14-2007 01:47 AM
RGB(more than 16 colors please)?? Niks C Programming 2 10-04-2005 07:43 PM



Advertisments