termcol: avoid hexadecimal conversion for newyuv colours
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 7 Feb 2016 23:18:05 +0000 (00:18 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 22 Mar 2017 21:28:04 +0000 (22:28 +0100)
Simplify default initialiser; previous version available as newyuv8().

Shiar_Sheet/Colour.pm
termcol.inc.pl

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] };
index eef6a8b0b0c96d1f39c66f7ac370e8e9975abb12..d4741edae96e012b7e6f4617b74768d1681b2d0e 100644 (file)
@@ -1,6 +1,6 @@
 use strict;
 use 5.010;
-use Shiar_Sheet::Colour '1.02';
+use Shiar_Sheet::Colour '1.05';
 
 +{
        html => {
@@ -149,8 +149,8 @@ use Shiar_Sheet::Colour '1.02';
                        }
 
                        # convert to rgb
-                       $y *= 255 / 32;
-                       $_ = $_ * 127.5 + 128 for $u, $v;
+                       $y /= 32;
+                       $_ /= 2 for $u, $v;
                        Shiar_Sheet::Colour->newyuv($y, $u, $v)->rgb48;
                } qw(
                        0    32  10+5 20-5 12+2 16-2 8+0  24-0
@@ -163,8 +163,7 @@ use Shiar_Sheet::Colour '1.02';
                name => 'MSX',
                list => [ map {
                        my ($y, @c) = split /;/;
-                       $y *= 255;
-                       $_ = ($_ - .47) / .53 * 127.5 + 128 for @c;
+                       $_ = ($_ - .47) / .53 / 2 for @c;
                        Shiar_Sheet::Colour->newyuv($y, @c)->rgb48;
                } qw(
                        0.00;0.47;0.47 0.00;0.47;0.47 0.53;0.20;0.07 0.67;0.27;0.17