termcol: avoid hexadecimal conversion for newyuv colours
[sheet.git] / Shiar_Sheet / Colour.pm
index cf22f3eac9726d4a6c86f10e66de932e0347f85a..e431cfed034bed92ed05b847ed1e0586201264f0 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 use List::Util qw( min max );
 
-our $VERSION = '1.04';
+our $VERSION = '1.05';
 
 # ITU-R recommendation 601 luma co-efficients
 our $kr = .299;
@@ -20,12 +20,9 @@ sub new {
 }
 
 sub newyuv {
-       # convert from YPbPr values 0..255 (or hex string)
+       # convert from YPbPr values -1..1
        my $class = shift;
-       my ($y, $cb, $cr) = @_ >= 3 ? @_ : (map {hex} $_[0] =~ /(..)/g);
-
-       $_ -= 128 for $cb, $cr;
-       $_ /= 255 for $y, $cb, $cr;
+       my ($y, $cb, $cr) = @_;
 
        my @rgb = map { max(0, min(255, $_ * 255)) } (
                $y                             + $cr,
@@ -35,6 +32,17 @@ sub newyuv {
        bless \@rgb, $class;
 }
 
+sub newyuv8 {
+       # convert from YPbPr values 0..255 (or hex string)
+       my $class = shift;
+       my ($y, $cb, $cr) = @_ >= 3 ? @_ : (map {hex} $_[0] =~ /(..)/g);
+
+       $_ -= 128 for $cb, $cr;
+       $_ /= 255 for $y, $cb, $cr;
+
+       $class->newyuv($y, $cb, $cr);
+}
+
 sub luminance {
        # perceived brightness
        my ($r, $g, $b) = @{ $_[0] };