2
2
3
3
use v5.12;
4
4
use warnings;
5
- use Test::More tests => 67 ;
5
+ use Test::More tests => 126 ;
6
6
7
7
BEGIN { unshift @INC , ' lib' , ' ../lib' }
8
8
my $module = ' Graphics::Toolkit::Color::Space' ;
@@ -12,7 +12,7 @@ my $fspace = Graphics::Toolkit::Color::Space->new();
12
12
is( ref $fspace , ' ' , ' need axis names to create color space' );
13
13
14
14
my $space = Graphics::Toolkit::Color::Space-> new(axis => [qw/ AAA BBB CCC DDD/ ]);
15
- is( ref $space , $module , ' got axis names and created color space ' );
15
+ is( ref $space , $module , ' created color space just with axis names ' );
16
16
is( $space -> name, ' ABCD' , ' got space name from AXIS short names' );
17
17
is( $space -> axis, 4, ' counted axis right' );
18
18
is( $space -> is_value_tuple([1,2,3,4]), 1, ' correct value tuple' );
@@ -32,44 +32,100 @@ is( $space->is_partial_hash({aaa =>1,bbb=> 2, ccc=>3, z => 4}), 0, 'one bad key
32
32
is( ref $space -> basis, ' Graphics::Toolkit::Color::Space::Basis' , ' have a valid space basis sub object' );
33
33
is( ref $space -> shape, ' Graphics::Toolkit::Color::Space::Shape' , ' have a valid space shape sub object' );
34
34
is( ref $space -> form, ' Graphics::Toolkit::Color::Space::Format' ,' have a valid format sub object' );
35
+
36
+ $space = Graphics::Toolkit::Color::Space-> new(axis => [qw/ AAA BBB CCC DDD/ ], name => ' name' );
37
+ is( ref $space , $module , ' created color space just with axis names and space name' );
38
+ is( $space -> name, ' name' , ' got given space name back' );
39
+ is( ref $space -> basis, ' Graphics::Toolkit::Color::Space::Basis' , ' have a valid space basis sub object' );
40
+ is( ref $space -> shape, ' Graphics::Toolkit::Color::Space::Shape' , ' have a valid space shape sub object' );
41
+ is( ref $space -> form, ' Graphics::Toolkit::Color::Space::Format' ,' have a valid format sub object' );
42
+
43
+ $space = Graphics::Toolkit::Color::Space-> new(axis => [qw/ AAA BBB CCC DDD/ ], prefix => ' pre' );
44
+ is( $space -> name, ' preABCD' , ' got space name with given prefix' );
45
+ is( ref $space -> basis, ' Graphics::Toolkit::Color::Space::Basis' , ' have a valid space basis sub object' );
46
+ is( ref $space -> shape, ' Graphics::Toolkit::Color::Space::Shape' , ' have a valid space shape sub object' );
47
+ is( ref $space -> form, ' Graphics::Toolkit::Color::Space::Format' ,' have a valid format sub object' );
48
+
49
+ $space = Graphics::Toolkit::Color::Space-> new(axis => [qw/ AAA BBB CCC DDD/ ], prefix => ' pre' , name => ' Name' );
50
+ is( $space -> name, ' preName' , ' got space name with given prefix and givn Name' );
51
+ is( ref $space -> basis, ' Graphics::Toolkit::Color::Space::Basis' , ' have a valid space basis sub object' );
52
+ is( ref $space -> shape, ' Graphics::Toolkit::Color::Space::Shape' , ' have a valid space shape sub object' );
53
+ is( ref $space -> form, ' Graphics::Toolkit::Color::Space::Format' ,' have a valid format sub object' );
54
+
35
55
is( ref $space -> in_range([0,1,0.5,0.001]), ' ARRAY' , ' default to normal range' );
36
56
is( ref $space -> in_range([1,1.1,1,1]), ' ' , ' one value of tuple is out of range' );
37
57
my $val = $space -> clamp([-1,1.1,1]);
38
- is( ref $val , ' ARRAY' , ' got tuple back ' );
58
+ is( ref $val , ' ARRAY' , ' clamped value tuple is a tuple ' );
39
59
is( int @$val , 4, ' filled mising value in' );
40
60
is( $val -> [0], 0, ' clamped up first value' );
41
61
is( $val -> [1], 1, ' clamped down second value' );
42
62
is( $val -> [2], 1, ' passed through third value' );
43
63
is( $val -> [3], 0, ' zero is default value' );
44
64
65
+ $space = Graphics::Toolkit::Color::Space-> new(axis => [qw/ AAA BBB CCC DDD/ ], range => [10,20,' normal' , [-10,10]]);
66
+ is( ref $space , $module , ' created color space with axis names and ranges' );
67
+ is( ref $space -> shape, ' Graphics::Toolkit::Color::Space::Shape' , ' have a valid space shape sub object' );
68
+ is( ref $space -> in_range([10,10,1,10]), ' ARRAY' , ' max values are in range' );
69
+ is( ref $space -> in_range([0,0,0,-10]), ' ARRAY' , ' min values are in range' );
70
+ is( ref $space -> in_range([0,0,2,-10]), ' ' , ' one value is ou of range' );
71
+ $val = $space -> clamp([-1,20.1,1]);
72
+ is( ref $val , ' ARRAY' , ' clamped value tuple is a tuple' );
73
+ is( int @$val , 4, ' filled mising value in' );
74
+ is( $val -> [0], 0, ' clamped up first value' );
75
+ is( $val -> [1], 20, ' clamped down second value' );
76
+ is( $val -> [2], 1, ' passed through third value' );
77
+ is( $val -> [3], 0, ' zero is default value' );
45
78
46
- exit 0;
47
-
48
- __END__
49
- my $space = Graphics::Toolkit::Color::Space->new(axis => [qw/AAA BBB CCC DDD/], range => 20);
79
+ $val = $space -> normalize([5,10,0.5,0]);
80
+ is( ref $val , ' ARRAY' , ' normalized value tuple is a tuple' );
81
+ is( int @$val , 4, ' right amount of values' );
82
+ is( $val -> [0], 0.5, ' first value correct' );
83
+ is( $val -> [1], 0.5, ' second value correct' );
84
+ is( $val -> [2], 0.5, ' third value correct' );
85
+ is( $val -> [3], 0.5, ' fourth value correct' );
86
+
87
+ $val = $space -> denormalize([ 0.5, 0.5, 0.5, 0.5]);
88
+ is( ref $val , ' ARRAY' , ' denormalized value tuple is a tuple' );
89
+ is( int @$val , 4, ' right amount of values' );
90
+ is( $val -> [0], 5, ' first value correct' );
91
+ is( $val -> [1], 10, ' second value correct' );
92
+ is( $val -> [2], 0.5, ' third value correct' );
93
+ is( $val -> [3], 0, ' fourth value correct' );
94
+
95
+ $val = $space -> denormalize_delta([ 0.5, 0.5, 0.5, 0.5]);
96
+ is( ref $val , ' ARRAY' , ' denormalized range value tuple is a tuple' );
97
+ is( int @$val , 4, ' right amount of values' );
98
+ is( $val -> [0], 5, ' first value correct' );
99
+ is( $val -> [1], 10, ' second value correct' );
100
+ is( $val -> [2], 0.5, ' third value correct' );
101
+ is( $val -> [3], 10, ' fourth value correct - range had none zero min' );
102
+
103
+ $val = $space -> delta([ 1, 1, 1, 1], [ 5, 20, 0, -1]);
104
+ is( ref $val , ' ARRAY' , ' delta between value tuples is a tuple' );
105
+ is( int @$val , 4, ' right amount of values' );
106
+ is( $val -> [0], 4, ' first value correct' );
107
+ is( $val -> [1], 19, ' second value correct' );
108
+ is( $val -> [2], -1, ' third value correct' );
109
+ is( $val -> [3], -2, ' fourth value correct - range had none zero min' );
50
110
51
- is( ref $space, $module, 'could create a space object');
52
- is( $space->name, 'ABCD', 'space has right name');
53
- is( $space->dimensions, 4, 'space has four dimension');
54
111
is( $space -> has_format(' bbb' ), 0, ' vector name is not a format' );
55
112
is( $space -> has_format(' c' ), 0, ' vector sigil is not a format' );
56
113
is( $space -> has_format(' list' ),1, ' list is a format' );
57
114
is( $space -> has_format(' hash' ),1, ' hash is a format' );
58
115
is( $space -> has_format(' char_hash' ),1, ' char_hash is a format' );
59
-
60
116
is( ref $space -> format([1,2,3,4], ' hash' ), ' HASH' , ' formatted values into a hash' );
61
117
is( int ($space -> format([1,2,3,4], ' list' )), 4, ' got long enough list of values' );
62
-
63
118
is( $space -> format([1,2,3,4], ' bbb' ), 0, ' got no value by key name' );
64
119
is( $space -> format([1,2,3,4], ' AAA' ), 0, ' got no value by uc key name' );
65
120
is( $space -> format([1,2,3,4], ' c' ), 0, ' got no value by shortcut name' );
66
121
is( $space -> format([1,2,3,4], ' D' ), 0, ' got no value by uc shortcut name' );
122
+ is( $space -> has_format(' str' ), 0, ' formatter not yet inserted' );
67
123
68
- is( $ space->has_format ('str'), 0, 'formatter not yet inserted' );
69
- my $c = $space->add_formatter('str', sub { $_[0] . $_[1] . $_[2] . $_[3]} );
70
- is( ref $c, 'CODE', 'formatter code accepted ');
71
- is( $ space->has_format('str'), 1, 'formatter inserted ');
72
- is( $space->format([1,2,3,4], ' str'), '1234', 'inserted formatter works');
124
+ my $c = $ space-> add_formatter (' str' , sub { $_ [1][0] . $_ [1][1] . $_ [1][2] . $_ [1][3]} );
125
+ is( ref $c , ' CODE ' , ' formatter code accepted ' );
126
+ is( $space -> has_format( ' str ' ), 1, ' formatter inserted ' );
127
+ my $str = $ space-> format([1,2,3,4], ' str ' );
128
+ is( $str , ' 1234' , ' inserted formatter works' );
73
129
74
130
my $fval = $space -> deformat({a => 1, b => 2, c => 3, d => 4});
75
131
is( int @$fval , 4, ' deformatter recognized char hash' );
@@ -90,8 +146,10 @@ is( $fval, undef, 'char hash with bad key got ignored');
90
146
$fval = $space -> deformat({aaa => 1, bbb => 2, ccc => 3, dd => 4});
91
147
is( $fval , undef , ' char hash with bad key got ignored' );
92
148
93
- my $dc = $space->add_deformatter('str', sub { split ':', $_[0] });
149
+ is( $space -> has_deformat(' str' ), 0, ' deformatter not yet inserted' );
150
+ my $dc = $space -> add_deformatter(' str' , sub { [split ' :' , $_ [1]] });
94
151
is( ref $dc , ' CODE' , ' deformatter code accepted' );
152
+ is( $space -> has_deformat(' str' ), 1, ' deformatter accessible' );
95
153
$fval = $space -> deformat(' 1:2:3:4' );
96
154
is( int @$fval , 4, ' self made deformatter recognized str' );
97
155
is( $fval -> [0], 1, ' first value correctly deformatted' );
@@ -100,45 +158,28 @@ is( $fval->[2], 3, 'third value correctly deformatted');
100
158
is( $fval -> [3], 4, ' fourth value correctly deformatted' );
101
159
102
160
is( $space -> can_convert(' XYZ' ), 0, ' converter not yet inserted' );
103
- my $h = $space->add_converter('XYZ', sub { $_[0]+1, $_[1]+1, $_[2]+1, $_[3]+1},
104
- sub { $_[0]-1, $_[1]-1, $_[2]-1, $_[3]-1} );
161
+ my $h = $space -> add_converter(' XYZ' , sub { $_ [0][0] +1, $_ [0][ 1]+1, $_ [0][ 2]+1, $_ [0] [3]+1},
162
+ sub { $_ [0][0] -1, $_ [0][ 1]-1, $_ [0][ 2]-1, $_ [0] [3]-1} );
105
163
is( ref $h , ' HASH' , ' converter code accepted' );
106
164
is( $space -> can_convert(' XYZ' ), 1, ' converter inserted' );
107
- my $val = $space->convert([1,2,3,4], 'XYZ');
108
- is( int @$val, 4, 'converter did something');
165
+ $val = $space -> convert([1,2,3,4], ' XYZ' );
166
+ is( int @$val , 4, ' converter did something' );
109
167
is( $val -> [0], 2, ' first value correctly converted' );
110
168
is( $val -> [1], 3, ' second value correctly converted' );
111
169
is( $val -> [2], 4, ' third value correctly converted' );
112
170
is( $val -> [3], 5, ' fourth value correctly converted' );
113
171
$val = $space -> deconvert([2,3,4,5], ' xyz' );
114
- is( int @$val, 4, 'deconverter did something even if space spelled in lower case');
172
+ is( int @$val , 4, ' deconverter did something even if space spelled in lower case' );
115
173
is( $val -> [0], 1, ' first value correctly deconverted' );
116
174
is( $val -> [1], 2, ' second value correctly deconverted' );
117
175
is( $val -> [2], 3, ' third value correctly deconverted' );
118
176
is( $val -> [3], 4, ' fourth value correctly deconverted' );
119
177
178
+ exit 0;
179
+
180
+
120
181
121
- my $d = $space->delta([2,3,4,5], [1,5,1,1] );
122
- is( int @$d, 4, 'delta result has right length');
123
- is( $d->[0], -1, 'first value correctly deconverted');
124
- is( $d->[1], 2, 'second value correctly deconverted');
125
- is( $d->[2], -3, 'third value correctly deconverted');
126
- is( $d->[3], -4, 'fourth value correctly deconverted');
127
182
128
- my $tr = $space->clamp([-1, 0, 20.1, 21, 1]);
129
- is( int @$tr, 4, 'clamp kept correct vector length = 4');
130
- is( $tr->[0], 0, 'clamp up value below minimum');
131
- is( $tr->[1], 0, 'do not touch minimal value');
132
- is( $tr->[2], 20, 'clamp real into int');
133
- is( $tr->[3], 20, 'clamp down value above range max');
134
183
135
- is( ref $space->in_range([1,2,3,4]), 'ARRAY', 'all values in range');
136
184
137
- my $norm = $space->normalize([0, 10, 20, 15]);
138
- is( int @$norm, 4, 'normalized 4 into 4 values');
139
- is( $norm->[0], 0, 'normalized first min value');
140
- is( $norm->[1], 0.5, 'normalized second mid value');
141
- is( $norm->[2], 1, 'normalized third max value');
142
- is( $norm->[3], 0.75, 'normalized fourth value');
143
185
144
- exit 0;
0 commit comments