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

Fellow Monks, Has anyone or does anyone know a way of converting HSV colours (say $h,$s,$v) to the corresponding RGB ($r, $g, $b) values ?? Thanks

Replies are listed 'Best First'.
Re: Converting HSV colours to RGB
by rob_au (Abbot) on Jan 17, 2002 at 16:20 UTC
    You can perform such conversions using the Imager::Color module. eg.

    #!/usr/bin/perl use Imager::Color; use strict; # define HSV colour option within the object initiation # my $hsv = Imager::Color->new( hsv => [ 120, 0.5, 1 ] # hue, v, s ); my @rgb = $hsv->rgba;

     

    Update

     

    Alternatively, if you want to roll your own conversion tool, the following code could be used, based on the converstion algorithm documentation at http://www.cs.rit.edu/~ncs/color/t_convert.html. eg.

    #!/usr/bin/perl use POSIX; use strict; sub hsv2rgb { my ( $h, $s, $v ) = @_; if ( $s == 0 ) { return $v, $v, $v; } $h /= 60; my $i = floor( $h ); my $f = $h - $i; my $p = $v * ( 1 - $s ); my $q = $v * ( 1 - $s * $f ); my $t = $v * ( 1 - $s * ( 1 - $f ) ); if ( $i == 0 ) { return $v, $t, $p; } elsif ( $i == 1 ) { return $q, $v, $t; } elsif ( $i == 2 ) { return $p, $v, $t; } elsif ( $i == 3 ) { return $p, $q, $v; } elsif ( $i == 4 ) { return $t, $p, $v; } else { return $v, $p, $q; } }
    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'
      Thanks mate, Should solve my problem
        case 1 is wrong! should be: elsif ( $i == 1 ) { return $q, $v, $p;