Skip to content

Commit c6d48e5

Browse files
committed
start fix the space hub
1 parent 4657bbf commit c6d48e5

File tree

9 files changed

+126
-127
lines changed

9 files changed

+126
-127
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ sub _check_values_and_space {
2828
check_space_name( $space_name ) and return;
2929
my $space = get_space($space_name);
3030
$space->is_array( $values ) ? $space
31-
: carp 'need an ARRAY ref with '.$space->dimensions." $space_name values as first argument of $sub_name";
31+
: 'need an ARRAY ref with '.$space->dimensions." $space_name values as first argument of $sub_name";
3232
}
3333

3434
########################################################################
@@ -67,34 +67,34 @@ sub deconvert { # @... --> @RGB (base color space) # normalized values only
6767
my ($values, $space_name) = @_;
6868
my $space = _check_values_and_space( 'deconvert', $values, $space_name );
6969
return unless ref $space;
70-
my @values = $space->clamp( $values, 'normal');
71-
return @values if $space->name eq base_space->name;
72-
$space->convert( \@values, $base_package);
70+
$values = $space->clamp( $values, 'normal', -1);
71+
return $values if $space->name eq base_space->name;
72+
$space->convert( $values, $base_package);
7373
}
7474

75-
sub convert { # @RGB --> @... # normalized values only
75+
sub convert { # @RGB --> $@...|!~ # normalized values only
7676
my ($values, $space_name) = @_;
7777
my $space = _check_values_and_space( 'convert', $values, $space_name );
78-
return unless ref $space;
79-
my @values = base_space->clamp( $values, 'normal');
80-
return @values if $space->name eq base_space->name;
81-
$space->deconvert( \@values, $base_package);
78+
return $space unless ref $space;
79+
$values = base_space->clamp( $values, 'normal', -1);
80+
return $values if $space->name eq base_space->name;
81+
$space->deconvert( $values, $base_package);
8282
}
8383

8484
sub denormalize { # result clamped, alway in space
8585
my ($values, $space_name, $range) = @_;
8686
my $space = _check_values_and_space( 'denormalize', $values, $space_name );
8787
return unless ref $space;
88-
my @values = $space->clamp($values, 'normal');
89-
$space->denormalize( \@values, $range);
88+
$values = $space->clamp($values, 'normal');
89+
$space->denormalize( $values, $range);
9090
}
9191

9292
sub normalize {
9393
my ($values, $space_name, $range) = @_;
9494
my $space = _check_values_and_space( 'normalize', $values, $space_name );
9595
return unless ref $space;
96-
my @values = $space->clamp($values, $range);
97-
return unless defined $values[0];
96+
$values = $space->clamp($values, $range);
97+
return $values unless ref $values;
9898
$space->normalize( $values, $range);
9999
}
100100

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use Graphics::Toolkit::Color::Space;
88
use Graphics::Toolkit::Color::Space::Util qw/mult_matrix apply_d65 remove_d65/;
99

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

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

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use Graphics::Toolkit::Color::Space::Util qw/mult_matrix apply_d65 remove_d65/;
99

1010
my $hcl_def = Graphics::Toolkit::Color::Space->new(prefix => 'CIE', name => 'LCHab',
1111
axis => [qw/luminance croma hue/],
12-
short => [qw/luminance croma hue/],
13-
range => [0,100,'r'], 1, 1.08883] );
12+
#short => [qw/luminance croma hue/],
13+
range => [100, 1, 1.08883] );
1414

1515
$hcl_def->add_converter('RGB', \&to_rgb, \&from_rgb );
1616

@@ -41,11 +41,11 @@ sub to_rgb {
4141
$z = ($z**3 > 0.008856) ? ($z ** 3) : (($z - 0.137931034) / 7.7870689);
4242
$x *= 0.95047;
4343
$z *= 0.108883;
44-
my ($r, $g, $b) = mult_matrix([[ 3.2404542, -0.9692660, 0.0556434],
45-
[-1.5371385, 1.8760108, -0.2040259],
46-
[-0.4985314, 0.0415560, 1.0572252]], $x, $y, $z);
44+
my ($r, $g, $bl) = mult_matrix([[ 3.2404542, -0.9692660, 0.0556434],
45+
[-1.5371385, 1.8760108, -0.2040259],
46+
[-0.4985314, 0.0415560, 1.0572252]], $x, $y, $z);
4747

48-
return ( remove_d65($r), remove_d65($g), remove_d65($b));
48+
return ( remove_d65($r), remove_d65($g), remove_d65($bl));
4949
}
5050

5151

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package Graphics::Toolkit::Color::Space::Instance::LCHuv;
77
use Graphics::Toolkit::Color::Space;
88
use Graphics::Toolkit::Color::Space::Util qw/mult_matrix apply_d65 remove_d65/;
99

10-
1110
my $hcl_def = Graphics::Toolkit::Color::Space->new( prefix => 'CIE', name => 'LCHuv',
1211
axis => [qw/luminance croma hue/],
1312
range => [0.95047, 1, 1.08883] );

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ sub _range { # check if range def is valid and eval (exapand) it
8585
}
8686

8787
sub _precision { # check if precision def is valid and eval (exapand) it
88-
my ($self, $external_precision) = @_;
88+
my ($self, $external_precision, $external_range) = @_;
8989
return $self->{'precision'} unless defined $external_precision;
90-
91-
$external_precision = Graphics::Toolkit::Color::Space::Shape->new( $self->{'basis'}, $self->{'type'}, $self->{'range'}, $external_precision);
90+
my $range = $self->_range($external_range);
91+
return $range unless ref $range;
92+
$external_precision = Graphics::Toolkit::Color::Space::Shape->new( $self->{'basis'}, $self->{'type'}, $range, $external_precision);
9293
return (ref $external_precision) ? $external_precision->{'precision'} : undef ;
9394
}
9495

@@ -127,7 +128,7 @@ sub clamp {
127128
my ($self, $values, $range, $precision) = @_;
128129
$range = $self->_range( $range );
129130
return "bad range definition, need upper limit, 2 element ARRAY or ARRAY of 2 element ARRAYs" unless ref $range;
130-
$precision = $self->_precision( $precision );
131+
$precision = $self->_precision( $precision, $range );
131132
return "bad precision definition, need ARRAY with ints or -1" unless ref $precision;
132133
$values = [] unless ref $values eq 'ARRAY';
133134
push @$values, 0 while @$values < $self->basis->count;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ sub is_nr { $_[0] =~ /^\-?\d+(\.\d+)?$/ }
3535

3636
sub max {
3737
my $v = shift;
38-
for (@_) { $v = $_ if $v < $_ }
38+
for (@_) { next unless defined $_; $v = $_ if $v < $_ }
3939
$v;
4040
}
4141

4242
sub min {
4343
my $v = shift;
44-
for (@_) { $v = $_ if $v > $_ }
44+
for (@_) { next unless defined $_; $v = $_ if $v > $_ }
4545
$v;
4646
}
4747

t/18_xyz_space.t

Lines changed: 66 additions & 68 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 => 47;
5+
use Test::More tests => 57;
66
use Test::Warn;
77

88
BEGIN { unshift @INC, 'lib', '../lib'}
@@ -15,80 +15,78 @@ is( not($@), 1, 'could load the module');
1515
is( ref $def, 'Graphics::Toolkit::Color::Space', 'got tight return value by loading module');
1616
is( $def->name, 'CIEXYZ', 'color space has right name');
1717
is( $def->dimensions, 3, 'color space has 3 dimensions');
18+
19+
is( ref $def->in_range([0, 0, 0]), 'ARRAY', 'check minimal XYZ values are in bounds');
20+
is( ref $def->in_range([0.950, 1, 1.088]), 'ARRAY', 'check maximal XYZ values');
21+
is( ref $def->in_range([0,0]), '', "XYZ got too few values");
22+
is( ref $def->in_range([0, 0, 0, 0]), '', "XYZ got too many values");
23+
is( ref $def->in_range([-0.1, 0, 0]), '', "X value is too small");
24+
is( ref $def->in_range([1, 0, 0]), '', "X value is too big");
25+
is( ref $def->in_range([0, -0.1, 0]), '', "Y value is too small");
26+
is( ref $def->in_range([0, 1.1, 0]), '', "Y value is too big");
27+
is( ref $def->in_range([0, 0, -.1 ] ), '', "Z value is too small");
28+
is( ref $def->in_range([0, 0, 1.2] ), '', "Z value is too big");
29+
1830
is( $def->is_array([0,0,0]), 1, 'vector has 3 elements');
1931
is( $def->can_convert('rgb'), 1, 'do only convert from and to rgb');
2032
is( $def->can_convert('RGB'), 1, 'namespace can be written upper case');
2133
is( $def->is_partial_hash({x => 1, y => 0}), 1, 'found hash with some keys');
2234
is( $def->is_partial_hash({x => 1, z => 0}), 1, 'found hash with some other keys');
2335
is( $def->can_convert('yiq'), 0, 'can not convert to yiq');
24-
my @val = $def->deformat(['CIEXYZ', 1, 0, -0.1]);
25-
is( int @val, 3, 'deformated value triplet (vector)');
26-
is( $val[0], 1, 'first value good');
27-
is( $val[1], 0, 'second value good');
28-
is( $val[2], -0.1, 'third value good');
36+
37+
my $val = $def->deformat(['CIEXYZ', 1, 0, -0.1]);
38+
is( int @$val, 3, 'deformated value triplet (vector)');
39+
is( $val->[0], 1, 'first value good');
40+
is( $val->[1], 0, 'second value good');
41+
is( $val->[2], -0.1, 'third value good');
2942
is( $def->format([0,1,0], 'css_string'), 'ciexyz(0,1,0)', 'can format css string');
3043

31-
my @xyz = $def->deconvert( [ 0, 0, 0], 'RGB');
32-
is( int @xyz, 3, 'converted color black has three XYZ values');
33-
is( $xyz[0], 0, 'converted color black has computed right X value');
34-
is( $xyz[1], 0, 'converted color black has computed right Y value');
35-
is( $xyz[2], 0, 'converted color black has computed right Z value');
36-
37-
@xyz = $def->deconvert( [ 0.5, 0.5, 0.5], 'RGB');
38-
is( int @xyz, 3, 'converted color grey has three XYZ values');
39-
is( close_enough($xyz[0], 0.20344), 1, 'converted color grey has computed right X value');
40-
is( close_enough($xyz[1], 0.21404), 1, 'converted color grey has computed right Y value');
41-
is( close_enough($xyz[2], 0.23305), 1, 'converted color grey has computed right Z value');
42-
43-
@xyz = $def->deconvert( [ 1, 1, 1], 'RGB');
44-
is( int @xyz, 3, 'converted color white has three XYZ values');
45-
is( close_enough($xyz[0], 0.95047), 1, 'converted color white has computed right X value');
46-
is( close_enough($xyz[1], 1), 1, 'converted color white has computed right Y value');
47-
is( close_enough($xyz[2], 1.08883), 1, 'converted color white has computed right Z value');
48-
49-
@xyz = $def->deconvert( [ 1, 0, 0.5], 'RGB');
50-
is( int @xyz, 3, 'converted color pink has three XYZ values');
51-
is( close_enough($xyz[0], 0.45108), 1, 'converted color pink has computed right X value');
52-
is( close_enough($xyz[1], 0.22821), 1, 'converted color pink has computed right Y value');
53-
is( close_enough($xyz[2], 0.22274), 1, 'converted color pink has computed right Z value');
54-
55-
my @rgb = $def->convert( [0, 0, 0], 'RGB');
56-
is( int @rgb, 3, 'converted back black with 3 values');
57-
is( close_enough($rgb[0], 0), 1, 'right red value');
58-
is( close_enough($rgb[1], 0), 1, 'right green value');
59-
is( close_enough($rgb[2], 0), 1, 'right blue value');
60-
61-
@rgb = $def->convert( [0.20344, 0.21404, 0.23305], 'RGB');
62-
is( int @rgb, 3, 'converted back gray with 3 values');
63-
is( close_enough($rgb[0], 0.5), 1, 'right red value');
64-
is( close_enough($rgb[1], 0.5), 1, 'right green value');
65-
is( close_enough($rgb[2], 0.5), 1, 'right blue value');
66-
67-
@rgb = $def->convert( [0.95047, 1, 1.08883], 'RGB');
68-
is( int @rgb, 3, 'converted back gray with 3 values');
69-
is( close_enough($rgb[0], 1), 1, 'right red value');
70-
is( close_enough($rgb[1], 1), 1, 'right green value');
71-
is( close_enough($rgb[2], 1), 1, 'right blue value');
72-
73-
@rgb = $def->convert( [0.45108, 0.22821, 0.22274], 'RGB');
74-
is( int @rgb, 3, 'converted back gray with 3 values');
75-
is( close_enough($rgb[0], 1 ), 1, 'right red value');
76-
is( close_enough($rgb[1], 0 ), 1, 'right green value');
77-
is( close_enough($rgb[2], 0.5), 1, 'right blue value');
44+
my $xyz = $def->deconvert( [ 0, 0, 0], 'RGB');
45+
is( int @$xyz, 3, 'converted color black has three XYZ values');
46+
is( $xyz->[0], 0, 'converted color black has computed right X value');
47+
is( $xyz->[1], 0, 'converted color black has computed right Y value');
48+
is( $xyz->[2], 0, 'converted color black has computed right Z value');
49+
50+
$xyz = $def->deconvert( [ 0.5, 0.5, 0.5], 'RGB');
51+
is( int @$xyz, 3, 'converted color grey has three XYZ values');
52+
is( close_enough($xyz->[0], 0.20344), 1, 'converted color grey has computed right X value');
53+
is( close_enough($xyz->[1], 0.21404), 1, 'converted color grey has computed right Y value');
54+
is( close_enough($xyz->[2], 0.23305), 1, 'converted color grey has computed right Z value');
55+
56+
$xyz = $def->deconvert( [ 1, 1, 1], 'RGB');
57+
is( int @$xyz, 3, 'converted color white has three XYZ values');
58+
is( close_enough($xyz->[0], 0.95047), 1, 'converted color white has computed right X value');
59+
is( close_enough($xyz->[1], 1), 1, 'converted color white has computed right Y value');
60+
is( close_enough($xyz->[2], 1.08883), 1, 'converted color white has computed right Z value');
61+
62+
$xyz = $def->deconvert( [ 1, 0, 0.5], 'RGB');
63+
is( int @$xyz, 3, 'converted color pink has three XYZ values');
64+
is( close_enough($xyz->[0], 0.45108), 1, 'converted color pink has computed right X value');
65+
is( close_enough($xyz->[1], 0.22821), 1, 'converted color pink has computed right Y value');
66+
is( close_enough($xyz->[2], 0.22274), 1, 'converted color pink has computed right Z value');
67+
68+
my $rgb = $def->convert( [0, 0, 0], 'RGB');
69+
is( int @$rgb, 3, 'converted back black with 3 values');
70+
is( close_enough($rgb->[0], 0), 1, 'right red value');
71+
is( close_enough($rgb->[1], 0), 1, 'right green value');
72+
is( close_enough($rgb->[2], 0), 1, 'right blue value');
73+
74+
$rgb = $def->convert( [0.20344, 0.21404, 0.23305], 'RGB');
75+
is( int @$rgb, 3, 'converted back gray with 3 values');
76+
is( close_enough($rgb->[0], 0.5), 1, 'right red value');
77+
is( close_enough($rgb->[1], 0.5), 1, 'right green value');
78+
is( close_enough($rgb->[2], 0.5), 1, 'right blue value');
79+
80+
$rgb = $def->convert( [0.95047, 1, 1.08883], 'RGB');
81+
is( int @$rgb, 3, 'converted back gray with 3 values');
82+
is( close_enough($rgb->[0], 1), 1, 'right red value');
83+
is( close_enough($rgb->[1], 1), 1, 'right green value');
84+
is( close_enough($rgb->[2], 1), 1, 'right blue value');
85+
86+
$rgb = $def->convert( [0.45108, 0.22821, 0.22274], 'RGB');
87+
is( int @$rgb, 3, 'converted back gray with 3 values');
88+
is( close_enough($rgb->[0], 1 ), 1, 'right red value');
89+
is( close_enough($rgb->[1], 0 ), 1, 'right green value');
90+
is( close_enough($rgb->[2], 0.5), 1, 'right blue value');
7891

7992
exit 0;
80-
81-
82-
__END__
83-
ok( !$def->check([0, -0.5959, -0.5227]), 'check YIO values works on lower bound values');
84-
ok( !$def->check([1, 0.5959, 0.5227]), 'check YIO values works on upper bound values');
85-
warning_like {$def->check([0,0])} {carped => qr/needs 3 values/}, "check YIQ got too few values";
86-
warning_like {$def->check([0, 0, 0, 0])} {carped => qr/needs 3 values/}, "check YIQ got too many values";
87-
88-
is( $def->check([0,0,0]), undef, 'checked neutral values');
89-
warning_like {$def->check([-0.1, 0, 0])} {carped => qr/luminance value is below/}, "luminance value is too small";
90-
warning_like {$def->check([ 1.1, 0,0])} {carped => qr/luminance value is above/}, "luminance value is too big";
91-
warning_like {$def->check([0, -0.6, 0])} {carped => qr/in-phase value is below/}, "whiteness value is too small";
92-
warning_like {$def->check([0, 0.6,0])} {carped => qr/in-phase value is above/}, "whiteness value is too big";
93-
warning_like {$def->check([0,0, -0.53 ])} {carped => qr/quadrature value is below/},"quadrature value is too small";
94-
warning_like {$def->check([0,0, 0.53])} {carped => qr/quadrature value is above/}, "quadrature value is too big";

t/19_lab_space.t

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ is( not($@), 1, 'could load the module');
1515
is( ref $def, 'Graphics::Toolkit::Color::Space', 'got tight return value by loading module');
1616
is( $def->name, 'CIELAB', 'color space has right name');
1717
is( $def->dimensions, 3, 'color space has 3 dimensions');
18+
19+
20+
is( ref $def->in_range([0, 0, 0]), 'ARRAY', 'check minimal XYZ values are in bounds');
21+
is( ref $def->in_range([0.950, 1, 1.088]), 'ARRAY', 'check maximal XYZ values');
22+
is( ref $def->in_range([0,0]), '', "XYZ got too few values");
23+
is( ref $def->in_range([0, 0, 0, 0]), '', "XYZ got too many values");
24+
is( ref $def->in_range([-0.1, 0, 0]), '', "X value is too small");
25+
is( ref $def->in_range([1, 0, 0]), '', "X value is too big");
26+
is( ref $def->in_range([0, -0.1, 0]), '', "Y value is too small");
27+
is( ref $def->in_range([0, 1.1, 0]), '', "Y value is too big");
28+
is( ref $def->in_range([0, 0, -.1 ] ), '', "Z value is too small");
29+
is( ref $def->in_range([0, 0, 1.2] ), '', "Z value is too big");
30+
1831
is( $def->is_array([0,0,0]), 1, 'vector has 3 elements');
1932
is( $def->can_convert('rgb'), 1, 'do only convert from and to rgb');
2033
is( $def->can_convert('RGB'), 1, 'namespace can be written upper case');
@@ -82,18 +95,3 @@ is( close_enough($rgb[1], 0 ), 1, 'right green value');
8295
is( close_enough($rgb[2], 0.5), 1, 'right blue value');
8396

8497
exit 0;
85-
86-
87-
__END__
88-
ok( !$def->check([0, -0.5959, -0.5227]), 'check YIO values works on lower bound values');
89-
ok( !$def->check([1, 0.5959, 0.5227]), 'check YIO values works on upper bound values');
90-
warning_like {$def->check([0,0])} {carped => qr/needs 3 values/}, "check YIQ got too few values";
91-
warning_like {$def->check([0, 0, 0, 0])} {carped => qr/needs 3 values/}, "check YIQ got too many values";
92-
93-
is( $def->check([0,0,0]), undef, 'checked neutral values');
94-
warning_like {$def->check([-0.1, 0, 0])} {carped => qr/luminance value is below/}, "luminance value is too small";
95-
warning_like {$def->check([ 1.1, 0,0])} {carped => qr/luminance value is above/}, "luminance value is too big";
96-
warning_like {$def->check([0, -0.6, 0])} {carped => qr/in-phase value is below/}, "whiteness value is too small";
97-
warning_like {$def->check([0, 0.6,0])} {carped => qr/in-phase value is above/}, "whiteness value is too big";
98-
warning_like {$def->check([0,0, -0.53 ])} {carped => qr/quadrature value is below/},"quadrature value is too small";
99-
warning_like {$def->check([0,0, 0.53])} {carped => qr/quadrature value is above/}, "quadrature value is too big";

0 commit comments

Comments
 (0)