From: Mischa POSLAWSKY Date: Sun, 7 Feb 2016 23:18:05 +0000 (+0100) Subject: termcol: avoid hexadecimal conversion for newyuv colours X-Git-Tag: v1.9~34 X-Git-Url: http://git.shiar.nl/sheet.git/commitdiff_plain/1b74f2fe18e42ec7acd07793fc45da6c0849fcc9 termcol: avoid hexadecimal conversion for newyuv colours Simplify default initialiser; previous version available as newyuv8(). --- diff --git a/Shiar_Sheet/Colour.pm b/Shiar_Sheet/Colour.pm index cf22f3e..e431cfe 100644 --- a/Shiar_Sheet/Colour.pm +++ b/Shiar_Sheet/Colour.pm @@ -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] }; diff --git a/termcol.inc.pl b/termcol.inc.pl index eef6a8b..d4741ed 100644 --- a/termcol.inc.pl +++ b/termcol.inc.pl @@ -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