termcol: hsv method in Colour module
[sheet.git] / Shiar_Sheet / Colour.pm
index cedfdfb9e31aa722ff8027657fba1b97c455fb26..cf22f3eac9726d4a6c86f10e66de932e0347f85a 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 use List::Util qw( min max );
 
-our $VERSION = '1.02';
+our $VERSION = '1.04';
 
 # ITU-R recommendation 601 luma co-efficients
 our $kr = .299;
@@ -42,9 +42,18 @@ sub luminance {
 }
 
 sub hue {
+       # colour shift (red = 0 .. 1)
        my ($r, $g, $b) = @{ $_[0] };
        my $hue = atan2 sqrt(3) * ($g - $b), $r*2 - $g - $b;
-       return ($hue + $PI) / $PI / 2; # 0 .. 1
+       $hue /= $PI * 2;  # -.5 .. .5
+       $hue++ if $hue < 0;  # fp $hue %= 1
+       return $hue;
+}
+
+sub hsv {
+       my ($rgb) = @_;
+       my $v = max(@{$rgb});
+       return $rgb->hue, abs(min(@{$rgb}) - $v), $v;
 }
 
 sub rgb24 {