package Shiar_Sheet::Colour;

use strict;
use warnings;
use List::Util 'min';

our $VERSION = '1.00';

# ITU-R recommendation 601 luma co-efficients
our $kr = .299;
our $kb = .114;
our $kg = 1 - $kb - $kr;

sub new {
	my $class = shift;
	my @rgb = @_ >= 3 ? @_ : (map {hex} $_[0] =~ /(..)/g);
	bless \@rgb, $class;
}

sub luminance {
	# perceived brightness
	my ($r, $g, $b) = @{ $_[0] };
	return $r*$kr + $g*$kg + $b*$kb;
}

sub rgb24 {
	my $str = '';
	$str .= sprintf '%X', min($_ / 17 + .5, 15) for @{ $_[0] };
	return $str;
}

sub rgb48 {
	my $str = '';
	$str .= sprintf '%02X', $_ for @{ $_[0] };
	return $str;
}

1;

