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

Discussion in 'Python' started by ZelluX, Nov 30, 2008.

  1. ZelluX

    ZelluX Guest

    Convert RGB colors to the closest ANSI colors. For example, given RGB
    color FF0000, it should print [31m.
     
    ZelluX, Nov 30, 2008
    #1
    1. Advertising

  2. ZelluX

    John Machin Guest

    On Nov 30, 4:39 pm, ZelluX <> 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
     
    John Machin, Nov 30, 2008
    #2
    1. Advertising

  3. ZelluX

    John Machin Guest

    On Nov 30, 8:04 pm, John Machin <> wrote:
    > On Nov 30, 4:39 pm, ZelluX <> 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.
     
    John Machin, Nov 30, 2008
    #3
  4. ZelluX

    Peter Otten Guest

    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 >> 8) & 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
     
    Peter Otten, Dec 1, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nitin
    Replies:
    6
    Views:
    487
    Keith Thompson
    Jul 16, 2005
  2. Niks

    RGB(more than 16 colors please)??

    Niks, Oct 4, 2005, in forum: C Programming
    Replies:
    2
    Views:
    337
    Keith Thompson
    Oct 4, 2005
  3. Replies:
    1
    Views:
    492
  4. Replies:
    11
    Views:
    1,074
    Keith Thompson
    Apr 28, 2008
  5. Frank Iannarilli

    pre-ansi to ansi c++ conversion?

    Frank Iannarilli, Jul 21, 2009, in forum: C++
    Replies:
    2
    Views:
    416
Loading...

Share This Page