Back to home page

Project CMSSW displayed by LXR

 
 

    


Warning, /Utilities/ReleaseScripts/ignominy/hsv2rgb is written in an unsupported language. File is not indexed.

0001 #!/usr/bin/env perl
0002 use warnings;
0003 
0004 sub min {
0005   my $min = shift;
0006   foreach my $val (@_) {
0007     $min = $min > $val ? $val : $min;
0008   }
0009   return $min;
0010 }
0011 
0012 sub max {
0013   my $max = shift;
0014   foreach my $val (@_) {
0015     $max = $max > $val ? $max : $val;
0016   }
0017   return $max;
0018 }
0019 
0020 sub rgb2hsv {
0021   my ($r, $g, $b) = @_;
0022   my ($h, $s, $v);
0023   my $max = &max($r, $g, $b);
0024   my $min = &min($r, $g, $b);
0025 
0026   $v = $max;
0027   $s = ($max ? ($max - $min)/$max : 0.0);
0028   if (! $s) {
0029     $h = 0;
0030   } else {
0031     my $d = $max - $min;
0032     if ($r == $max) {
0033       $h = 0. + ($g - $b)/$d;
0034     } elsif ($g == $max) {
0035       $h = 2. + ($b - $r)/$d;
0036     } elsif ($b == $max) {
0037       $h = 4. + ($r - $g)/$d;
0038     }
0039     $h /= 6.;
0040     $h += 1. if ($h < 0.);
0041   }
0042 
0043   $h = int($h * 1000 + .5)/1000;
0044   $s = int($s * 1000 + .5)/1000;
0045   $v = int($v * 1000 + .5)/1000;
0046 
0047   return ($h, $s, $v);
0048 }
0049 
0050 sub rgb2yiq {
0051   my ($r, $g, $b) = @_;
0052   return (.299 * $r + .587 * $g + .115 * $b,
0053           .596 * $r - .275 * $g - .321 * $b,
0054           .212 * $r - .523 * $g + .311 * $b);
0055 }
0056 
0057 sub hsv2rgb {
0058   my ($h, $s, $v) = @_;
0059   if ($s eq 0.) {
0060     # die "bad HSV H value: $h -- should be 0\n" if $h;
0061     return ($v, $v, $v);
0062   } else {
0063     $h = 0. if $h eq 1.;
0064     $h *= 6.;
0065     my $i = int($h);
0066     my $f = $h - $i;
0067     my $p = $v * (1. - $s);
0068     my $q = $v * (1. - $s * $f);
0069     my $t = $v * (1. - $s * (1. - $f));
0070 
0071     return ($v, $t, $p) if $i == 0;
0072     return ($q, $v, $p) if $i == 1;
0073     return ($p, $v, $t) if $i == 2;
0074     return ($p, $q, $v) if $i == 3;
0075     return ($t, $p, $v) if $i == 4;
0076     return ($v, $p, $q) if $i == 5;
0077     die;
0078   }
0079 }
0080 
0081 my @hsv = @ARGV;
0082 my @rgb = &hsv2rgb(@hsv);
0083 
0084 map { $_ = int($_ * 255 + .5) } @rgb;
0085 print join(" ", @rgb), "\n";