Skip to content

Commit 0c6669e

Browse files
committed
extended possibilities of range def + tests
1 parent d4f9b68 commit 0c6669e

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use warnings;
55

66
package Graphics::Toolkit::Color::Space::Instance::LAB;
77
use Graphics::Toolkit::Color::Space;
8-
se Graphics::Toolkit::Color::Space::Util qw/mult_matrix apply_d65 remove_d65/;
8+
use Graphics::Toolkit::Color::Space::Util qw/mult_matrix apply_d65 remove_d65/;
99

1010
my $lab_def = Graphics::Toolkit::Color::Space->new( axis => [qw/L* a* b*/], #
1111
prefix => 'CIE',
12-
range => [100, 500, 200] );
12+
range => [[0, 100, 'r'], [-500, 500, 'r'], [-200, 200, 'r']] );
1313

1414
$lab_def->add_converter('RGB', \&to_rgb, \&from_rgb );
1515

@@ -26,21 +26,18 @@ sub from_rgb {
2626
$y = ($y > 0.008856) ? ($y ** (1/3)) : (7.7870689 * $y + 0.137931034);
2727
$z = ($z > 0.008856) ? ($z ** (1/3)) : (7.7870689 * $z + 0.137931034);
2828

29-
my $l = (116 * $y) - 16;
30-
my $a = 500 * ($x - $y);
31-
my $b = 200 * ($y - $z);
32-
return ($l, $a, $b);
29+
return ((116 * $y) - 16, $a = 500 * ($x - $y), 200 * ($y - $z));
3330
}
3431

3532

3633
sub to_rgb {
3734
my ($l, $a, $b) = @_;
38-
$i = ($i * $i_size) - $i_max;
39-
$q = ($q * $q_size) - $q_max;
40-
my $r = $y + ( 0.956 * $i) + ( 0.619 * $q);
41-
my $g = $y + (-0.272 * $i) + (-0.647 * $q);
42-
my $b = $y + (-1.106 * $i) + ( 1.703 * $q);
43-
return ($r, $g, $b);
35+
#~ $i = ($i * $i_size) - $i_max;
36+
#~ $q = ($q * $q_size) - $q_max;
37+
#~ my $r = $y + ( 0.956 * $i) + ( 0.619 * $q);
38+
#~ my $g = $y + (-0.272 * $i) + (-0.647 * $q);
39+
#~ my $b = $y + (-1.106 * $i) + ( 1.703 * $q);
40+
#~ return ($r, $g, $b);
4441
}
4542

4643
$lab_def;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ sub new {
2424
for my $i ($basis->iterator) {
2525
my $drange = $range->[$i]; # range def of this dimension
2626

27-
if (not ref $drange and $drange > 0){
27+
if (not ref $drange and $drange and $drange eq 'normal'){
28+
$range->[$i] = [0,1];
29+
} elsif (not ref $drange and $drange > 0){
2830
$drange = int $drange;
2931
$range->[$i] = [0, $drange];
30-
} elsif (ref $drange eq 'ARRAY' and @$drange == 2
32+
} elsif (ref $drange eq 'ARRAY' and (@$drange == 2 or @$drange == 3)
3133
and defined $drange->[0] and defined $drange->[1] and $drange->[0] < $drange->[1]) { # full valid def
3234
} else { return }
3335
}
@@ -57,6 +59,7 @@ sub dimension_is_int {
5759
return 0 if $r->[0] == 0 and $r->[1] == 1; #normal
5860
return 0 if int($r->[0]) != $r->[0];
5961
return 0 if int($r->[1]) != $r->[1];
62+
return 0 if exists $r->[2] and $r->[2] and lc(substr $r->[2], 0, 1) eq 'r'; # r or real
6063
1;
6164
}
6265
sub _range {

t/03_space_shape.t

Lines changed: 25 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 => 75;
5+
use Test::More tests => 91;
66
use Test::Warn;
77

88
BEGIN { unshift @INC, 'lib', '../lib'}
@@ -20,7 +20,7 @@ is( Graphics::Toolkit::Color::Space::Shape->new( $basis, {}), undef, 'range defi
2020
is( Graphics::Toolkit::Color::Space::Shape->new( $basis, [[1,3],[1,3] ]), undef, 'not enough dimensions');
2121
is( Graphics::Toolkit::Color::Space::Shape->new( $basis, [[1,3],[1,3],[1,3],[1,3] ]), undef, 'too many dimensions');
2222
is( Graphics::Toolkit::Color::Space::Shape->new( $basis, [[1,3],[1,3],[1] ]), undef, 'one dimension had too short def');
23-
is( Graphics::Toolkit::Color::Space::Shape->new( $basis, [[1,3],[1,3],[1,2,3] ]), undef, 'one dimension had too long def');
23+
is( Graphics::Toolkit::Color::Space::Shape->new( $basis, [[1,3],[1,3],[1,2,3,4] ]), undef, 'one dimension had too long def');
2424
is( Graphics::Toolkit::Color::Space::Shape->new( $basis, [[1,3],[1,3],[2,2] ]), undef, 'range min is not smaller than max');
2525
is( Graphics::Toolkit::Color::Space::Shape->new( $basis, [[1,3],[1,3],[1,2] ],{}), undef, 'type def has to be array too');
2626
is( Graphics::Toolkit::Color::Space::Shape->new( $basis, [[1,3],[1,3],[1,2] ],[1,1]), undef, 'type def too short');
@@ -30,8 +30,31 @@ is( Graphics::Toolkit::Color::Space::Shape->new( $basis, [[1,3],[1,3],[1,2] ],[1
3030

3131
my $shape = Graphics::Toolkit::Color::Space::Shape->new( $basis, 20);
3232
is( ref $shape, $module, 'created shape with 0..20 range');
33+
is( $shape->dimension_is_int(0), 1, 'first dimension outputs is int values');
34+
is( $shape->dimension_is_int(1), 1, 'second dimension outputs is int values');
35+
is( $shape->dimension_is_int(2), 1, 'third dimension outputs is int values');
36+
is( $shape->dimension_is_int(3), undef, 'there is no fourth dimension ');
37+
3338
my $bshape = Graphics::Toolkit::Color::Space::Shape->new( $basis, [[-5,5],[-5,5],[-5,5]], ['angular', 'circular', 0]);
3439
is( ref $bshape, $module, 'created 3D bowl shape with -5..5 range');
40+
is( $bshape->dimension_is_int(0), 1, 'first dimension outputs is int values');
41+
is( $bshape->dimension_is_int(1), 1, 'second dimension outputs is int values');
42+
is( $bshape->dimension_is_int(2), 1, 'third dimension outputs is int values');
43+
44+
my $nshape = Graphics::Toolkit::Color::Space::Shape->new( $basis, 'normal');
45+
is( $nshape->dimension_is_int(0), 0, 'first normal dimension outputs is not int');
46+
is( $nshape->dimension_is_int(1), 0, 'second normal dimension outputs is not int');
47+
is( $nshape->dimension_is_int(2), 0, 'third normal dimension outputs is not int');
48+
49+
my $mshape = Graphics::Toolkit::Color::Space::Shape->new( $basis, ['normal', 100, [2, 2.2]]);
50+
is( $mshape->dimension_is_int(0), 0, 'first particular normal dimension is not int');
51+
is( $mshape->dimension_is_int(1), 1, 'second dimension defined by int is int');
52+
is( $mshape->dimension_is_int(2), 0, 'third dimension defined by real range is not int');
53+
54+
my $oshape = Graphics::Toolkit::Color::Space::Shape->new( $basis, [[0, 10, 'int'], [0, 10, 'real'], [0, 10, 'r'],]);
55+
is( $oshape->dimension_is_int(0), 1, 'first dimension outputs is int');
56+
is( $oshape->dimension_is_int(1), 0, 'second dimension outputs is not int');
57+
is( $oshape->dimension_is_int(2), 0, 'third dimension outputs is not int');
3558

3659
my @d = $shape->delta(1, [1,5,4,5] );
3760
is( int @d, 0, 'reject compute delta on none vector on first arg position');

0 commit comments

Comments
 (0)