Skip to content

Commit 36f52a0

Browse files
committed
fix CMY tests
1 parent d9f830f commit 36f52a0

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

lib/Graphics/Toolkit/Color/Space.pm

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,26 @@ sub add_deformatter { shift->form->add_deformatter(@_) } # ~format_name, &defo
5050

5151
sub can_convert { (defined $_[1] and exists $_[0]{'convert'}{ uc $_[1] }) ? 1 : 0 }
5252

53-
sub add_converter {
54-
my ($self, $space_name, $to_code, $from_code, $mode) = @_;
55-
return 0 if not defined $space_name or ref $space_name or ref $from_code ne 'CODE' or ref $to_code ne 'CODE';
56-
return 0 if $self->can_convert( $space_name );
57-
$self->{'convert'}{ uc $space_name } = { from => $from_code, to => $to_code, mode => $mode };
58-
}
59-
6053
sub convert {
6154
my ($self, $values, $space_name) = @_;
62-
return unless $self->{'basis'}->is_array( $values ) and defined $space_name and $self->can_convert( $space_name );
63-
return [$self->{'convert'}{ uc $space_name }{'to'}->(@$values)];
55+
return unless $self->is_value_tuple( $values ) and defined $space_name and $self->can_convert( $space_name );
56+
return [$self->{'convert'}{ uc $space_name }{'to'}->( $values )];
6457
}
58+
6559
sub deconvert {
6660
my ($self, $values, $space_name) = @_;
6761
return unless ref $values eq 'ARRAY' and defined $space_name and $self->can_convert( $space_name );
68-
return [ $self->{'convert'}{ uc $space_name }{'from'}->(@$values) ];
62+
return [ $self->{'convert'}{ uc $space_name }{'from'}->( $values ) ];
6963
}
7064

65+
sub add_converter {
66+
my ($self, $space_name, $to_code, $from_code, $mode) = @_;
67+
return 0 if not defined $space_name or ref $space_name or ref $from_code ne 'CODE' or ref $to_code ne 'CODE';
68+
return 0 if $self->can_convert( $space_name );
69+
$self->{'convert'}{ uc $space_name } = { from => $from_code, to => $to_code, mode => $mode }; # what is mode ?
70+
}
71+
72+
7173
1;
7274

7375
__END__

lib/Graphics/Toolkit/Color/Space/Format.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ sub _value_regex {
6060
? (map {'\s*('.$self->{'value_format'}[$_].'\s*(?:'.quotemeta($self->{'suffix'}[$_]).')?)\s*' } $self->basis->iterator)
6161
: (map {'\s*'.$self->{'value_format'}[$_].'\s*(?:'.quotemeta($self->{'suffix'}[$_]).')?\s*' } $self->basis->iterator);
6262
}
63-
#### public formatting API #############################################
63+
#### public API: formatting value tuples ###############################
6464

6565
sub basis { $_[0]{'basis'}}
6666
sub has_format { (defined $_[1] and exists $_[0]{'format'}{ lc $_[1] }) ? 1 : 0 }

lib/Graphics/Toolkit/Color/Space/Instance/CMY.pm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ package Graphics::Toolkit::Color::Space::Instance::CMY;
77
use Graphics::Toolkit::Color::Space;
88

99
my $cmy_def = Graphics::Toolkit::Color::Space->new( axis => [qw/cyan magenta yellow/] );
10-
$cmy_def->add_converter('RGB', \&to_rgb, \&from_rgb );
10+
$cmy_def->add_converter('RGB', \&invert, \&invert );
1111

12-
sub from_rgb { map { 1 - $_} @_ }
13-
sub to_rgb { map { 1 - $_} @_ }
12+
sub invert { map { 1 - $_ } @{$_[0]} }
1413

1514
$cmy_def;
1615

lib/Graphics/Toolkit/Color/Space/Instance/RGB.pm

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,23 @@ use Graphics::Toolkit::Color::Space;
88
use Graphics::Toolkit::Color::Space::Util ':all';
99

1010
my $rgb_def = Graphics::Toolkit::Color::Space->new( axis => [qw/red green blue/], range => 255 );
11+
12+
$rgb_def->add_deformatter( 'array', sub { $_[1] if $rgb_def->is_value_tuple( $_[1] ) } );
1113
$rgb_def->add_formatter( 'hex_string', \&hex_from_rgb );
1214
$rgb_def->add_deformatter( 'hex_string', \&rgb_from_hex );
13-
$rgb_def->add_deformatter( 'array', sub { $_[1] if $rgb_def->is_value_tuple( $_[1] ) } );
1415

1516

1617
sub hex_from_rgb { sprintf("#%02x%02x%02x", @{$_[1]} ) }
1718

18-
sub rgb_from_hex { # translate #000000 and #000 --> r, g, b
19+
sub rgb_from_hex { # translate #000000 or #000 --> [ r, g, b ]
1920
my ($self, $hex) = @_;
2021
return "hex color definition '$hex' has to start with # followed by 3 or 6 hex characters (0-9,a-f)"
2122
unless defined $hex and not ref $hex
2223
and (length($hex) == 4 or length($hex) == 7)
23-
and substr($hex, 0, 1) eq '#' and $hex =~ /^#[\da-f]+$/i;
24+
and substr($hex, 0, 1) eq '#' and $hex =~ /^#[\da-f]+$/i; # ($_[0] =~ /^#[[:xdigit:]]{3}$/ or $_[0] =~ /^#[[:xdigit:]]{6}$/)
2425
$hex = substr $hex, 1;
2526
[(length $hex == 3) ? (map { hex($_.$_) } unpack( "a1 a1 a1", $hex))
2627
: (map { hex($_ ) } unpack( "a2 a2 a2", $hex))];
2728
}
2829

29-
# defined $_[0] and ($_[0] =~ /^#[[:xdigit:]]{3}$/ or $_[0] =~ /^#[[:xdigit:]]{6}$/)
30-
31-
3230
$rgb_def;

t/11_cmy_space.t

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use v5.12;
44
use warnings;
5-
use Test::More tests => 42;
5+
use Test::More tests => 45;
66

77
BEGIN { unshift @INC, 'lib', '../lib'}
88
my $module = 'Graphics::Toolkit::Color::Space::Instance::CMY';
@@ -11,7 +11,7 @@ my $def = eval "require $module";
1111
is( not($@), 1, 'could load the module');
1212
is( ref $def, 'Graphics::Toolkit::Color::Space', 'got space object by loading module');
1313
is( $def->name, 'CMY', 'color space has right name');
14-
is( $def->dimensions, 3, 'color space has 3 dimensions');
14+
is( $def->axis, 3, 'CMY color space has 3 axis');
1515

1616
is( ref $def->in_range([0,0,0]), 'ARRAY', 'check CMY values works on lower bound values');
1717
is( ref $def->in_range([1, 1, 1]), 'ARRAY', 'check CMY values works on upper bound values');
@@ -38,19 +38,22 @@ is( $cmy->[1], 1, 'passed (M) value');
3838
is( $cmy->[2], 0, 'added (Y) value when too few args');
3939

4040
$cmy = $def->clamp([-0.1, 2, 0.5, 0.4, 0.5]);
41+
is( ref $cmy, 'ARRAY', 'clamped tuple and got tuple back');
4142
is( int @$cmy, 3, 'removed missing argument in value vector by clamp');
4243
is( $cmy->[0], 0, 'clamped up (C) value to minimum');
4344
is( $cmy->[1], 1, 'clamped down (M) value to maximum');
4445
is( $cmy->[2], 0.5, 'passed (Y) value');
4546

4647

4748
$cmy = $def->deconvert( [0, 0.1, 1], 'RGB');
49+
is( ref $cmy, 'ARRAY', 'converted RGB values tuple into CMY tuple');
4850
is( int @$cmy, 3, 'converted RGB values to CMY');
4951
is( $cmy->[0], 1, 'converted to maximal cyan value');
5052
is( $cmy->[1], 0.9, 'converted to mid magenta value');
5153
is( $cmy->[2], 0, 'converted to minimal yellow value');
5254

5355
my $rgb = $def->convert( [1, 0.9, 0 ], 'RGB');
56+
is( ref $rgb, 'ARRAY', 'converted CMY values tuple into RGB tuple');
5457
is( int @$rgb, 3, 'converted CMY to RGB triplets');
5558
is( $rgb->[0], 0, 'converted red value');
5659
is( $rgb->[1], 0.1, 'converted green value');
@@ -69,5 +72,4 @@ is( $d->[0], -0.1, 'C delta');
6972
is( $d->[1], 0.3, 'M delta');
7073
is( $d->[2], 0.6, 'Y delta');
7174

72-
7375
exit 0;

0 commit comments

Comments
 (0)