Color::Rgb - Simple rgb.txt parsing class


$Revision: 1.4 $


    use Color::Rgb;
    $rgb = new Color::Rgb(rgb_txt=>'/usr/X11R6/lib/X11/rgb.txt');

    @rgb = $rgb->rgb('red');            # returns 255, 0, 0
    $red = $rgb->rgb('red', ',');       # returns the above rgb list as
                                        # comma separated string
    $red_hex=$rgb->hex('red');          # returns 'FF0000'
    $red_hex=$rgb->hex('red', '#');     # returns '#FF0000'

    $my_hex = $rgb->rgb2hex(255,0,0);   # returns 'FF0000'
    $my_rgb = $rgb->hex2rgb('#FF0000'); # returns list of 255,0,0


Color::Rgb - simple rgb.txt parsing class.


  • new([rgb_txt=$rgb_file])> - constructor. Returns a Color::Rgb object. Optionally accepts a path to the rgb.txt file. If you omit the file, it will use the path in the $Color::Rgb::RGB_TXT variable, which defaults to '/usr/X11R6/lib/X11/rgb.txt'. It means, instead of using rgb_txt=>'' option, you could also set the value of the $Color::Rgb::RGB_TXT variable to the correct path before you call the new() constructor (but definitely after you load the Color::Rgb class with use or require).

    Note: If your system does not provide with any rgb.txt file, Color::Rgb distribution includes one you can use instead.

  • name2rgb($name [,$delimiter]) - returns list of numeric Red, Green and Blue values for a $name delimited (optionally) by a $delimiter . $name is name of the color in the English language (Ex., 'black', 'red', 'purple' etc.).


        my ($r, $g, $b) = $rgb->rgb('blue');      # returns list: 00, 00, 255
        my $string      = $rgb->rgb('blue', ','); # returns string: '00,00,255'

    If name does not exist in the rgb.txt file it will return undef.

  • name2hex($name [,$prefix]) - similar to rgb($name) method, but returns hexadecimal string representing red, green and blue colors, prefixed (optionally) with $prefix. If $name does not exist in the rgb.txt file it will return undef.

  • hex($name, [,$delimiter]) - alias to name2hex()

  • rgb($name, [,$delimiter]) - alias to name2rgb()

  • rgb2hex($r, $g, $b [,$prefix]) - converts rgb value to hexadecimal string. This method has nothing to do with the rgb.txt file, so none of the arguments need to exist in the file.


        @rgb = (128, 128, 128);               # RGB representation of grey
        $hex_grey = $rgb->rgb2hex(@rgb);      # returns string 'C0C0C0'
        $hex_grey = $rgb->rgb2hex(@rgb, '#'); # returns string '#C0C0C0'
  • hex2rgb('hex' [,$delim]) - the opposite of rgb2hex(): takes a hexadecimal representation of a color and returns a numeric list of Red, Green and Blue. If optional $delim delimiter is present, it returns the string of RGB colors delimited by the $delimiter. Characters like '#' and '0x' in the beginning of the hexadecimal value will be ignored. Examples:

        $hex = '#00FF00';   # represents blue
        @rgb = $rgb->hex2rgb($hex);            #returns list of 0, 255, 0
        $rgb_string = $rgb->hex2rgb($hex,','); #returns string '0,255,0'

    Note: hex2rgb() expects valid hexadecimal representation of a color in 6 character long string. If not, it might not work properly.

  • names([$pattern] - returns a list of all the names in the rgb.txt file. If $pattern is given as the first argument, it will return only the names matching the pattern. Example:

        @colors = $rgb->names;         # returns all the names
        @gray_colors = $rgb->names('gray'); # returns list of all the names
                                            # matching the word 'gray'


Following people contributed to this library with their patches and/or bug reports. (list is in chronological order)

  • Marc-Olivier BERNARD <> notified of the warnings that the library produced while "warnings" pragma enabled and improper parsed rgb values that contain single "0". This bug was fixed in 1.2

  • Martin Herrmann <> noticed a bug in rgb2hex() method which was failing if the blue value was a single "0". This problem is fixed in 1.3


Color::Rgb is a free software and can be modified and distributed under the same terms as Perl itself.


Color::Rgb is maintained by Sherzod B. Ruzmetov <>.



1 POD Error

The following errors were encountered while parsing the POD:

Around line 310:

You forgot a '=back' before '=head1'