Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

RGB values and color name

by mdog (Pilgrim)
on Aug 25, 2003 at 18:24 UTC ( [id://286445]=perlquestion: print w/replies, xml ) Need Help??

mdog has asked for the wisdom of the Perl Monks concerning the following question:

Brethern --

Is there a way to pass an RGB value to a function/module/etc and get back a fairly simple color name?

All I am looking to do is determine if a color is red, black, green, yellow, blue, etc (not mauve, terracotta, or taupe) based on it's RGB value.

The RGB value is not something nice and round like 255,0,0 for red but 223,8,13 (which still looks really red). So I can't do a look up in rgb.txt because there isn't an entry for that color.

I have tried using GD's
colorClosest $image->colorClosest(red,green,blue) object method This returns the index of the color closest in the color table to the +red green and blue components specified. If no colors have yet been a +llocated, then this call returns -1. Example: $apricot = $myImage->colorClosest(255,200,180);
and then turn that into an RGB colorset so I can do a look up of the color via:
rgb $image->rgb(colorIndex) object method This returns a list containing the red, green and blue components of t +he specified color index. Example: @RGB = $myImage->rgb($peachy);
But for the example I gave for red above, it returns the exact same RGB numbers.

Couldn't find any modules on CPAN that would do what I want or out on the Net.

I figure this is a kind of subjective request ("does this look blue to you?") and that I'll probably have to write a routine that has a range of numbers that "equal" blue or red or whatever but am hoping this has already been done.

Thanks,
Matt

Replies are listed 'Best First'.
Re: RGB values and color name
by tcf22 (Priest) on Aug 25, 2003 at 18:32 UTC
      Doesn't get easier than that! ++
Re: RGB values and color name
by Mr. Muskrat (Canon) on Aug 25, 2003 at 18:35 UTC

    You should be able to use Graphics::ColorNames in conjunction with GD to achieve the desired result.

    Added:

    #!/usr/bin/perl use strict; use warnings; use Graphics::ColorNames qw( hex2tuple ); use GD; our %COLORS; tie %COLORS, 'Graphics::ColorNames'; my $img = new GD::Image(100, 100); foreach my $color (keys %COLORS) { $img->colorAllocate( hex2tuple( $COLORS{$color} ) ); } my $apricot = $img->colorClosest(255,200,180); print "\$apricot is supposed to be 255 200 180 but is actually @{[$img +->rgb($apricot)]}\n"; my $reddish = $img->colorClosest(223,8,13); print "\$reddish is supposed to be 223 8 13 but is actually @{[$img->r +gb($reddish)]}\n";

      Many thanks! Exactly what I was looking for....

      Didn't understand that I need to allocate colors before I could use colorClosest. I guess that's why when I tried to use colorClosest and convert it back to RGB it didn't have any other colors to spit back to me than the same color. It didn't know any others.

      PerlMonks rule!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://286445]
Approved by ybiC
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found