Skip to content

Commit

Permalink
fix next 3 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
lichtkind committed Apr 12, 2024
1 parent 1832930 commit bd5316d
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 468 deletions.
10 changes: 5 additions & 5 deletions lib/Graphics/Toolkit/Color/Space.pm
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ sub deformat {
return undef unless defined $values;
for my $deformatter (values %{$self->{'deformat'}}){
my @values = $deformatter->($values);
return @values if @values == $self->dimensions;
return \@values if @values == $self->dimensions;
}
return undef;
}
Expand All @@ -88,14 +88,14 @@ sub add_converter {
}
sub convert {
my ($self, $values, $space_name) = @_;
return unless $self->{'basis'}->is_array( $values ) and defined $space_name;
$self->{'convert'}{ uc $space_name }{'to'}->(@$values) if $self->can_convert( $space_name );
return unless $self->{'basis'}->is_array( $values ) and defined $space_name and $self->can_convert( $space_name );
return [$self->{'convert'}{ uc $space_name }{'to'}->(@$values)];
}

sub deconvert {
my ($self, $values, $space_name) = @_;
return unless ref $values eq 'ARRAY' and defined $space_name;
$self->{'convert'}{ uc $space_name }{'from'}->(@$values) if $self->can_convert( $space_name );
return unless ref $values eq 'ARRAY' and defined $space_name and $self->can_convert( $space_name );
return [ $self->{'convert'}{ uc $space_name }{'from'}->(@$values) ];
}

1;
Expand Down
2 changes: 1 addition & 1 deletion lib/Graphics/Toolkit/Color/Space/Shape.pm
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ sub delta { # values have to be normalized
$delta[$_] > 0.5 ? ($delta[$_]-1) : $delta[$_] } $self->basis->iterator ];
}

sub in_range {
sub in_range { # $vals -- $range, $precision --> $@vals | ~!
my ($self, $values, $range, $precision) = @_;
return 'color value vector in '.$self->basis->name.' needs '.$self->basis->count.' values'
unless $self->basis->is_array( $values );
Expand Down
72 changes: 36 additions & 36 deletions t/04_space.t
Original file line number Diff line number Diff line change
Expand Up @@ -38,51 +38,51 @@ is( ref $c, 'CODE', 'formatter code accepted');
is( $space->has_format('str'), 1, 'formatter inserted');
is( $space->format([1,2,3,4], 'str'), '1234', 'inserted formatter works');

my @fval = $space->deformat({a => 1, b => 2, c => 3, d => 4});
is( int @fval, 4, 'deformatter recognized char hash');
is( $fval[0], 1, 'first value correctly deformatted');
is( $fval[1], 2, 'second value correctly deformatted');
is( $fval[2], 3, 'third value correctly deformatted');
is( $fval[3], 4, 'fourth value correctly deformatted');

@fval = $space->deformat({aaa => 1, bbb => 2, ccc => 3, ddd => 4});
is( int @fval, 4, 'deformatter recognized hash');
is( $fval[0], 1, 'first value correctly deformatted');
is( $fval[1], 2, 'second value correctly deformatted');
is( $fval[2], 3, 'third value correctly deformatted');
is( $fval[3], 4, 'fourth value correctly deformatted');

@fval = $space->deformat({a => 1, b => 2, c => 3, e => 4});
is( $fval[0], undef, 'char hash with bad key got ignored');
@fval = $space->deformat({aaa => 1, bbb => 2, ccc => 3, dd => 4});
is( $fval[0], undef, 'char hash with bad key got ignored');
my $fval = $space->deformat({a => 1, b => 2, c => 3, d => 4});
is( int @$fval, 4, 'deformatter recognized char hash');
is( $fval->[0], 1, 'first value correctly deformatted');
is( $fval->[1], 2, 'second value correctly deformatted');
is( $fval->[2], 3, 'third value correctly deformatted');
is( $fval->[3], 4, 'fourth value correctly deformatted');

$fval = $space->deformat({aaa => 1, bbb => 2, ccc => 3, ddd => 4});
is( int @$fval, 4, 'deformatter recognized hash');
is( $fval->[0], 1, 'first value correctly deformatted');
is( $fval->[1], 2, 'second value correctly deformatted');
is( $fval->[2], 3, 'third value correctly deformatted');
is( $fval->[3], 4, 'fourth value correctly deformatted');

$fval = $space->deformat({a => 1, b => 2, c => 3, e => 4});
is( $fval, undef, 'char hash with bad key got ignored');
$fval = $space->deformat({aaa => 1, bbb => 2, ccc => 3, dd => 4});
is( $fval, undef, 'char hash with bad key got ignored');

my $dc = $space->add_deformatter('str', sub { split ':', $_[0] });
is( ref $dc, 'CODE', 'deformatter code accepted');
@fval = $space->deformat('1:2:3:4');
is( int @fval, 4, 'self made deformatter recognized str');
is( $fval[0], 1, 'first value correctly deformatted');
is( $fval[1], 2, 'second value correctly deformatted');
is( $fval[2], 3, 'third value correctly deformatted');
is( $fval[3], 4, 'fourth value correctly deformatted');
$fval = $space->deformat('1:2:3:4');
is( int @$fval, 4, 'self made deformatter recognized str');
is( $fval->[0], 1, 'first value correctly deformatted');
is( $fval->[1], 2, 'second value correctly deformatted');
is( $fval->[2], 3, 'third value correctly deformatted');
is( $fval->[3], 4, 'fourth value correctly deformatted');

is( $space->can_convert('XYZ'), 0, 'converter not yet inserted');
my $h = $space->add_converter('XYZ', sub { $_[0]+1, $_[1]+1, $_[2]+1, $_[3]+1},
sub { $_[0]-1, $_[1]-1, $_[2]-1, $_[3]-1} );
is( ref $h, 'HASH', 'converter code accepted');
is( $space->can_convert('XYZ'), 1, 'converter inserted');
my @val = $space->convert([1,2,3,4], 'XYZ');
is( int @val, 4, 'converter did something');
is( $val[0], 2, 'first value correctly converted');
is( $val[1], 3, 'second value correctly converted');
is( $val[2], 4, 'third value correctly converted');
is( $val[3], 5, 'fourth value correctly converted');
@val = $space->deconvert([2,3,4,5], 'xyz');
is( int @val, 4, 'deconverter did something even if space spelled in lower case');
is( $val[0], 1, 'first value correctly deconverted');
is( $val[1], 2, 'second value correctly deconverted');
is( $val[2], 3, 'third value correctly deconverted');
is( $val[3], 4, 'fourth value correctly deconverted');
my $val = $space->convert([1,2,3,4], 'XYZ');
is( int @$val, 4, 'converter did something');
is( $val->[0], 2, 'first value correctly converted');
is( $val->[1], 3, 'second value correctly converted');
is( $val->[2], 4, 'third value correctly converted');
is( $val->[3], 5, 'fourth value correctly converted');
$val = $space->deconvert([2,3,4,5], 'xyz');
is( int @$val, 4, 'deconverter did something even if space spelled in lower case');
is( $val->[0], 1, 'first value correctly deconverted');
is( $val->[1], 2, 'second value correctly deconverted');
is( $val->[2], 3, 'third value correctly deconverted');
is( $val->[3], 4, 'fourth value correctly deconverted');


my $d = $space->delta([2,3,4,5], [1,5,1,1] );
Expand Down
192 changes: 97 additions & 95 deletions t/10_rgb_space.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,100 +14,103 @@ is( ref $def, 'Graphics::Toolkit::Color::Space', 'got right return value by load
is( $def->name, 'RGB', 'color space has right name');
is( $def->dimensions, 3, 'color space has 3 dimensions');


ok( !$def->check([0,0,0]), 'check rgb values works on lower bound values');
ok( !$def->check([255,255,255]), 'check rgb values works on upper bound values');
warning_like {$def->check([0,0])} {carped => qr/needs 3 values/}, "check rgb got too few values";
warning_like {$def->check([0, 0, 0, 0])} {carped => qr/needs 3 values/}, "check rgb got too many values";
warning_like {$def->check([-1, 0, 0])} {carped => qr/red value/}, "red value is too small";
warning_like {$def->check([0.5, 0, 0])} {carped => qr/red value/}, "red value is not integer";
warning_like {$def->check([256, 0, 0])} {carped => qr/red value/}, "red value is too big";
warning_like {$def->check([0, -1, 0])} {carped => qr/green value/}, "green value is too small";
warning_like {$def->check([0, 0.5, 0])} {carped => qr/green value/}, "green value is not integer";
warning_like {$def->check([0, 256, 0])} {carped => qr/green value/}, "green value is too big";
warning_like {$def->check([0, 0, -1 ] )} {carped => qr/blue value/}, "blue value is too small";
warning_like {$def->check([0, 0, 0.5] )} {carped => qr/blue value/}, "blue value is not integer";
warning_like {$def->check([0, 0, 256] )} {carped => qr/blue value/}, "blue value is too big";

my @rgb = $def->clamp([]);
is( int @rgb, 3, 'clamp resets missing color to black');
is( $rgb[0], 0, 'default color is black (R)');
is( $rgb[1], 0, 'default color is black (G)');
is( $rgb[2], 0, 'default color is black (B)');

@rgb = $def->clamp([1,2]);
is( $rgb[0], 1, 'carry over first arg');
is( $rgb[1], 2, 'carry over second arg');
is( $rgb[2], 0, 'set missing color value to zero');
@rgb = $def->clamp([1.1, 2, 3, 4]);
is( $rgb[0], 1, 'clamped none int value down');
is( $rgb[1], 2, 'carried color is black (G) took second of too many args');
is( $rgb[2], 3, 'default color is black (B) too third of too many args');
is( int @rgb, 3, 'left out the needless argument');
@rgb = $def->clamp([-1,10,256]);
is( int @rgb, 3, 'clamp does not change number of negative values');
is( $rgb[0], 0, 'too low red value is clamp up');
is( $rgb[1], 10, 'in range green value is not touched');
is( $rgb[2], 255, 'too large blue value is clamp down');
is( ref $def->in_range([0,0,0]), 'ARRAY', 'check RGB values works on lower bound values');
is( ref $def->in_range([255,255,255]), 'ARRAY', 'check RGB values works on upper bound values');
is( ref $def->in_range([0,0]), '', "RGB got too few values");
is( ref $def->in_range([0, 0, 0, 0]), '', "RGB got too many values");
is( ref $def->in_range([-1, 0, 0]), '', "red value is too small");
is( ref $def->in_range([0.5, 0, 0]), '', "red value is not integer");
is( ref $def->in_range([256, 0, 0]), '', "red value is too big");
is( ref $def->in_range([0, -1, 0]), '', "green value is too small");
is( ref $def->in_range([0, 0.5, 0]), '', "green value is not integer");
is( ref $def->in_range([0, 256, 0]), '', "green value is too big");
is( ref $def->in_range([0, 0, -1 ] ), '', "blue value is too small");
is( ref $def->in_range([0, 0, 0.5] ), '', "blue value is not integer");
is( ref $def->in_range([0, 0, 256] ), '', "blue value is too big");


my $rgb = $def->clamp([]);
is( int @$rgb, 3, 'clamp resets missing color to black');
is( $rgb->[0], 0, 'default color is black (R)');
is( $rgb->[1], 0, 'default color is black (G)');
is( $rgb->[2], 0, 'default color is black (B)');

$rgb = $def->clamp([1,2]);
is( $rgb->[0], 1, 'carry over first arg');
is( $rgb->[1], 2, 'carry over second arg');
is( $rgb->[2], 0, 'set missing color value to zero');

$rgb = $def->clamp([1.1, 2, 3, 4]);
is( int @$rgb, 3, 'left out the needless argument');
is( $rgb->[0], 1, 'clamped none int value down');
is( $rgb->[1], 2, 'carried color is black (G) took second of too many args');
is( $rgb->[2], 3, 'default color is black (B) too third of too many args');

$rgb = $def->clamp([-1,10,256]);
is( int @$rgb, 3, 'clamp does not change number of negative values');
is( $rgb->[0], 0, 'too low red value is clamp up');
is( $rgb->[1], 10, 'in range green value is not touched');
is( $rgb->[2], 255, 'too large blue value is clamp down');

is( $def->format([0,0,0], 'hex'), '#000000', 'converted black from rgb to hex');
is( uc $def->format([255,255,255],'HEX'), '#FFFFFF', 'converted white from rgb to hex');
is( uc $def->format([ 10, 20, 30],'hex'), '#0A141E', 'converted random color from rgb to hex');

@rgb = $def->deformat('#332200');
is( int @rgb, 3, 'could deformat hex string');
is( $rgb[0], 51, 'red is correctly tranlated from hex');
is( $rgb[1], 34, 'green is correctly tranlated from hex');
is( $rgb[2], 0, 'blue is correctly tranlated from hex');

@rgb = $def->deformat('#DEF');
is( int @rgb, 3, 'could deformat short hex string');
is( $rgb[0], 221, 'converted (short form) hex to RGB red is correct');
is( $rgb[1], 238, 'converted (short form) hex to RGB green is correct');
is( $rgb[2], 255, 'converted (short form) hex to RGB blue is correct');

@rgb = $def->deformat([ 33, 44, 55]);
is( int @rgb, 3, 'number triplet in ARRAY is recognized by ARRAY');
is( $rgb[0], 33, 'red is transported');
is( $rgb[1], 44, 'green is transported');
is( $rgb[2], 55, 'blue is transported');

@rgb = $def->deformat([rgb => 11, 22, 256]);
is( int @rgb, 3, 'deformat lc named ARRAY: got 3 values');
is( $rgb[0], 11, 'red is correct');
is( $rgb[1], 22, 'green got transported');
is( $rgb[2], 256, 'blue value does not get clamped');

@rgb = $def->deformat(['CMY', 11, 22, 33]);
is( $rgb[0], undef, 'OO deformat reacts only to right name');

@rgb = $def->deformat('RGB: -1, 256, 3.3 ');
is( int @rgb, 3, 'deformat STRING format: got 3 values');
is( $rgb[0], -1, 'to small red is not clamped up');
is( $rgb[1], 256, 'too large green is not clamped down');
is( $rgb[2], 3.3, 'blue decimals do not get clamped');


@rgb = $def->deformat('rgb:0,1,2');
is( int @rgb, 3, 'deformat STRING format without spaces and lc name: got 3 values');
is( $rgb[0], 0, 'red is zero');
is( $rgb[1], 1, 'green is one');
is( $rgb[2], 2, 'blue is two');

@rgb = $def->deformat('cmy: 1,2,3.3');
is( $rgb[0], undef, 'OO deformat STRING reacts only to right space name');
$rgb = $def->deformat('#332200');
is( @$rgb, 3, 'could deformat hex string');
is( $rgb->[0], 51, 'red is correctly tranlated from hex');
is( $rgb->[1], 34, 'green is correctly tranlated from hex');
is( $rgb->[2], 0, 'blue is correctly tranlated from hex');

$rgb = $def->deformat('#DEF');
is( int @$rgb, 3, 'could deformat short hex string');
is( $rgb->[0], 221, 'converted (short form) hex to RGB red is correct');
is( $rgb->[1], 238, 'converted (short form) hex to RGB green is correct');
is( $rgb->[2], 255, 'converted (short form) hex to RGB blue is correct');

$rgb = $def->deformat([ 33, 44, 55]);
is( int @$rgb, 3, 'number triplet in ARRAY is recognized by ARRAY');
is( $rgb->[0], 33, 'red is transported');
is( $rgb->[1], 44, 'green is transported');
is( $rgb->[2], 55, 'blue is transported');

$rgb = $def->deformat([rgb => 11, 22, 256]);
is( int @$rgb, 3, 'deformat lc named ARRAY: got 3 values');
is( $rgb->[0], 11, 'red is correct');
is( $rgb->[1], 22, 'green got transported');
is( $rgb->[2], 256, 'blue value does not get clamped');

$rgb = $def->deformat(['CMY', 11, 22, 33]);
is( $rgb->[0], undef, 'OO deformat reacts only to right name');

$rgb = $def->deformat('RGB: -1, 256, 3.3 ');
is( int @$rgb, 3, 'deformat STRING format: got 3 values');
is( $rgb->[0], -1, 'to small red is not clamped up');
is( $rgb->[1], 256, 'too large green is not clamped down');
is( $rgb->[2], 3.3, 'blue decimals do not get clamped');



$rgb = $def->deformat('rgb:0,1,2');
is( int @$rgb, 3, 'deformat STRING format without spaces and lc name: got 3 values');
is( $rgb->[0], 0, 'red is zero');
is( $rgb->[1], 1, 'green is one');
is( $rgb->[2], 2, 'blue is two');

$rgb = $def->deformat('cmy: 1,2,3.3');
is( $rgb->[0], undef, 'OO deformat STRING reacts only to right space name');

is( $def->format([0,256,3.3], 'string'), 'rgb: 0, 256, 3.3', 'formated rgb triplet into value string');

@rgb = $def->deformat('rgb( -1 , 2.3, 4444)');
is( int @rgb, 3, 'deformat css STRING formatwith all hurdles: got 3 values');
is( $rgb[0], -1, 'red is -1');
is( $rgb[1], 2.3, 'green is one');
is( $rgb[2], 4444, 'blue is two');
$rgb = $def->deformat('rgb( -1 , 2.3, 4444)');
is( int @$rgb, 3, 'deformat css STRING formatwith all hurdles: got 3 values');
is( $rgb->[0], -1, 'red is -1');
is( $rgb->[1], 2.3, 'green is one');
is( $rgb->[2], 4444, 'blue is two');

is( $def->format([-1,2.3,4444], 'css_string'), 'rgb(-1,2.3,4444)', 'formated rgb triplet into css string');

my $rgb = $def->format([0,256,3.3], 'array');
$rgb = $def->format([0,256,3.3], 'array');
is( ref $rgb, 'ARRAY', 'formated into ARRAY');
is( @$rgb, 4, 'named RGB tuple has 4 elements');
is( $rgb->[0], 'rgb', 'tuple color name space');
Expand All @@ -117,17 +120,16 @@ is( $rgb->[3], 3.3, 'blue still has decimal');

is( $def->format([10,20,30], 'hex'), '#0a141e', 'formated rgb triplet into hex string');

my @d = $def->delta([0,44,256],[256,88,0]);
is( int @d, 3, 'delta vector has right length');
is( $d[0], 256, 'delta in R component');
is( $d[1], 44, 'delta in G component');
is( $d[2], -256, 'delta in B component');

@rgb = $def->denormalize( [0.3, 0.4, 0.5], [[0,255],[0,255],[0,255]] );
is( int @rgb, 3, 'denormalized triplet');
is( $rgb[0], 77, 'right red value');
is( $rgb[1], 102, 'right green value');
is( $rgb[2], 128, 'right blue value');

my $d = $def->delta([0,44,256],[256,88,0]);
is( int @$d, 3, 'delta vector has right length');
is( $d->[0], 256, 'delta in R component');
is( $d->[1], 44, 'delta in G component');
is( $d->[2], -256, 'delta in B component');

$rgb = $def->denormalize( [0.3, 0.4, 0.5], [[0,255],[0,255],[0,255]] );
is( int @$rgb, 3, 'denormalized triplet');
is( $rgb->[0], 77, 'right red value');
is( $rgb->[1], 102, 'right green value');
is( $rgb->[2], 128, 'right blue value');

exit 0;
Loading

0 comments on commit bd5316d

Please sign in to comment.