X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/6e19e617482820b7d6894d11adbb65064ad49836..ddd5f9146c58ba38f16d2e525273b83076c4f93a:/Shiar_Sheet/Colour.pm diff --git a/Shiar_Sheet/Colour.pm b/Shiar_Sheet/Colour.pm index 303e375..7ea68e9 100644 --- a/Shiar_Sheet/Colour.pm +++ b/Shiar_Sheet/Colour.pm @@ -2,9 +2,9 @@ package Shiar_Sheet::Colour; use strict; use warnings; -use List::Util 'min'; +use List::Util qw( min max ); -our $VERSION = '1.00'; +our $VERSION = '1.01'; # ITU-R recommendation 601 luma co-efficients our $kr = .299; @@ -17,6 +17,22 @@ sub new { bless \@rgb, $class; } +sub newyuv { + # 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; + + my @rgb = map { max(0, min(255, $_ * 255)) } ( + $y + $cr, + $y - $cb * (1 - $kb) * $kb/$kg - $cr * $kr/$kg, + $y + $cb * (1 - $kb) , + ); + bless \@rgb, $class; +} + sub luminance { # perceived brightness my ($r, $g, $b) = @{ $_[0] };