Skip to content

Commit b652ab9

Browse files
committed
more color space tests
1 parent b291ba3 commit b652ab9

File tree

5 files changed

+51
-26
lines changed

5 files changed

+51
-26
lines changed

Changes

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
1.8 2024-04-22 lichtkind
33
-------
44
* = mid level feature release
5-
* * added color space support: XYZ LAB LUV HCL LCH
5+
* * added color space support for: XYZ LAB LUV LCHab LCHuv NCol
66
* + extended range definitions with explicit type names
7+
* + color spaces can now define value procision,
8+
value suffix ('%') and
9+
value parsing regex
710
* ? added documentation for color space object and missing formats
8-
* & moved code into space::Format attr object class
11+
* & moved code into Space::Format space attr object class
912
* & removed Carp and Test::Warn as dependencies
1013
* & moved color set function into own package
1114
* ~ default space of methods 'distance', 'blend' and 'gradient'

lib/Graphics/Toolkit/Color/Space.pm

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ sub new {
2222

2323
########################################################################
2424

25-
sub basis { $_[0]{'basis'} }
26-
sub name { $_[0]->basis->space_name } # --> ~
27-
sub axis { $_[0]->basis->count } # --> +
28-
sub is_value_tuple { $_[0]->basis->is_value_tuple( $_[1] ) } # @+values --> ?
29-
sub is_partial_hash { $_[0]->basis->is_partial_hash( $_[1] ) }# %+values --> ?
25+
sub basis { $_[0]{'basis'} }
26+
sub name { shift->basis->space_name } # --> ~
27+
sub axis { shift->basis->count } # --> +
28+
sub is_value_tuple { shift->basis->is_value_tuple(@_) } # @+values --> ?
29+
sub is_partial_hash { shift->basis->is_partial_hash(@_) } # %+values --> ?
3030

3131
########################################################################
3232

@@ -50,13 +50,11 @@ sub add_deformatter { shift->form->add_deformatter(@_) } # ~format_name, &defo
5050
#### conversion ########################################################
5151

5252
sub can_convert { (defined $_[1] and exists $_[0]{'convert'}{ uc $_[1] }) ? 1 : 0 }
53-
5453
sub convert {
5554
my ($self, $values, $space_name) = @_;
5655
return unless $self->is_value_tuple( $values ) and defined $space_name and $self->can_convert( $space_name );
5756
return [$self->{'convert'}{ uc $space_name }{'to'}->( $values )];
5857
}
59-
6058
sub deconvert {
6159
my ($self, $values, $space_name) = @_;
6260
return unless ref $values eq 'ARRAY' and defined $space_name and $self->can_convert( $space_name );

t/02_space_basis.t

Lines changed: 5 additions & 2 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 => 117;
5+
use Test::More tests => 120;
66

77
BEGIN { unshift @INC, 'lib', '../lib'}
88
my $module = 'Graphics::Toolkit::Color::Space::Basis';
@@ -80,7 +80,10 @@ is( $s3d->is_hash({a => 1, B => 1, g => 3}), 1, 'shortcut casing get
8080
is( $s3d->is_hash({a => 1, b => 1, g => 3, h => 4}), 0, 'too many hash key shortcut ');
8181
is( $s3d->is_hash({alph => 1, beth => 1, gimel => 4, daleth => 2, he => 4}), 0, 'one wrong hash key');
8282

83-
is( $s5d->is_partial_hash([]), 0, 'array is not a partial hash');
83+
is( $s5d->is_partial_hash(''), 0, 'string is not a partial hash');
84+
is( $s5d->is_partial_hash([]), 0, 'array is not a partial hash');
85+
is( $s5d->is_partial_hash({}), 0, 'empty hash is not a partial hash');
86+
is( $s5d->is_partial_hash({gamma => 1}), 0, 'wrong key for partial hash');
8487
is( $s5d->is_partial_hash({aleph => 1, beth => 2, gimel => 3, daleth => 4, he => 5}), 1, 'valid hash with right keys is also correct partial hash');
8588
is( $s5d->is_partial_hash({aleph => 1, beth => 20, gimel => 3, daleth => 4, he => 5, o => 6}), 0, 'partial hash can not have more keys than full hash definition');
8689
is( $s5d->is_partial_hash({aleph => 1 }), 1, 'valid partial hash to have only one korrect key');

t/03_space_shape.t

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,33 @@ my $basis = Graphics::Toolkit::Color::Space::Basis->new( [qw/AAA BBB CCC/] );
1515
my $shape = Graphics::Toolkit::Color::Space::Shape->new( $basis);
1616
is( ref $shape, $module, 'created shape with default settings');
1717

18-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, {}), qr/invalid axis type/, 'type definition needs to be an ARRAY');
19-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, []), qr/invalid axis type/, 'type definition needs to have same length');
20-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, ['yes','no','maybe']), qr/invalid axis type/, 'undefined values');
21-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, [1,2,3]), qr/invalid axis type/, 'undefined numeric values');
18+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, {}), qr/invalid axis type/, 'type definition needs to be an ARRAY');
19+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, []), qr/invalid axis type/, 'type definition needs to have same length');
20+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, ['yes','no','maybe']), qr/invalid axis type/, 'undefined values');
21+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, [1,2,3]), qr/invalid axis type/, 'undefined numeric values');
2222
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, ['linear','circular','no']), $module, 'valid type def');
2323

24-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, {}), qr/invalid range/, 'range definition needs to be an ARRAY');
24+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, {}), qr/invalid range/, 'range definition needs to be an ARRAY');
2525
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, 1), $module, 'uniform scalar range');
2626
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, 'normal'), $module, 'normal scalar range');
2727
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, 'percent'), $module, 'percent scalar range');
28-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, []), qr/invalid range/, 'range definition ARRAY has to have same lngth');
28+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, []), qr/invalid range/, 'range definition ARRAY has to have same lngth');
2929
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [1,2,3]), $module, 'ARRAY range with right amount of ints');
3030
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1,2],[1,2]]), $module, 'full ARRAY range');
3131
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1.1,1.2],[1,2]]), $module, 'full ARRAY range with decimals');
32-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1,2]]), qr/invalid range/, 'not enough elements in range def');
33-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1,2],[1,2],[1,2]]), qr/invalid range/, 'too many elements in range def');
34-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[2,1],[1,2]]), qr/lower bound/, 'one range def element is backward');
35-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1],[1,2]]), qr/two elements/, 'one range def element is too small');
36-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1,2,3],[1,2]]), qr/two elements/, 'one range def element is too big');
37-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1,'-'],[1,2]]), qr/none numeric/, 'one range def element has a none number');
32+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1,2]]), qr/invalid range/, 'not enough elements in range def');
33+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1,2],[1,2],[1,2]]), qr/invalid range/, 'too many elements in range def');
34+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[2,1],[1,2]]), qr/lower bound/, 'one range def element is backward');
35+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1],[1,2]]), qr/two elements/, 'one range def element is too small');
36+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1,2,3],[1,2]]), qr/two elements/, 'one range def element is too big');
37+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef, [[1,2],[1,'-'],[1,2]]), qr/none numeric/, 'one range def element has a none number');
3838

3939
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, undef,undef, 0), $module, 'accepting third constructor arg - precision zero');
4040
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, undef,undef, 2), $module, 'precision 2');
4141
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, undef,undef, -1), $module, 'precision -1');
4242
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, undef,undef, [0,1,-1]), $module, 'full precision def');
43-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef,undef, [1,2]), qr/value precision/, 'precision def too short');
44-
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef,undef, [1,2,3,-1]), qr/value precision/, 'precision def too long');
43+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef,undef, [1,2]), qr/value precision/, 'precision def too short');
44+
like( Graphics::Toolkit::Color::Space::Shape->new( $basis, undef,undef, [1,2,3,-1]), qr/value precision/, 'precision def too long');
4545

4646
is( ref Graphics::Toolkit::Color::Space::Shape->new( $basis, undef,undef,undef, '%'), $module, 'accepting fourth constructor arg - a suffix for axis numbers');
4747

t/05_space.t

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,28 @@ is( $space->is_value_tuple([1,2,3,4]), 1, 'correct value tuple');
1919
is( $space->is_value_tuple([1,2,3,4,5]), 0, 'too long value tuple');
2020
is( $space->is_value_tuple([1,2,3,]), 0, 'too short value tuple');
2121
is( $space->is_value_tuple({1=>1,2=>2,3=>3,4=>4,}), 0, 'wrong ref type for value tuple');
22-
is( $space->is_value_tuple(''), 0, 'no ref type can not be value tuple');
22+
is( $space->is_value_tuple(''), 0, 'none ref type can not be value tuple');
23+
is( $space->is_partial_hash(''), 0, 'need a hash ref to be a partial hash');
24+
is( $space->is_partial_hash({}), 0, 'a partial hash needs to have at least one key');
25+
is( $space->is_partial_hash({eta =>1}), 0, 'wrong key for partial hash');
26+
is( $space->is_partial_hash({aaa =>1}), 1, 'right key for partial hash');
27+
is( $space->is_partial_hash({aaa =>1,bbb=> 2}), 1, 'two right keys for partial hash');
28+
is( $space->is_partial_hash({aaa =>1,bbb=> 2, ccc=>3}), 1, 'three right keys for partial hash');
29+
is( $space->is_partial_hash({aaa =>1,bbb=> 2, ccc=>3, ddd => 4}), 1, 'four right keys for partial hash');
30+
is( $space->is_partial_hash({aaa =>1,bbb=> 2, ccc=>3, d => 4}), 1, 'can mix full names and shortcut names');
31+
is( $space->is_partial_hash({aaa =>1,bbb=> 2, ccc=>3, z => 4}), 0, 'one bad key makes partial hash invalid');
32+
is( ref $space->basis, 'Graphics::Toolkit::Color::Space::Basis', 'have a valid space basis sub object');
33+
is( ref $space->shape, 'Graphics::Toolkit::Color::Space::Shape', 'have a valid space shape sub object');
34+
is( ref $space->form, 'Graphics::Toolkit::Color::Space::Format','have a valid format sub object');
35+
is( ref $space->in_range([0,1,0.5,0.001]), 'ARRAY', 'default to normal range');
36+
is( ref $space->in_range([1,1.1,1,1]), '', 'one value of tuple is out of range');
37+
my $val = $space->clamp([-1,1.1,1]);
38+
is( ref $val, 'ARRAY', 'got tuple back');
39+
is( int @$val, 4, 'filled mising value in');
40+
is( $val->[0], 0, 'clamped up first value');
41+
is( $val->[1], 1, 'clamped down second value');
42+
is( $val->[2], 1, 'passed through third value');
43+
is( $val->[3], 0, 'zero is default value');
2344

2445

2546
exit 0;

0 commit comments

Comments
 (0)