diff --git a/lib/Graphics/Toolkit/Color/Space.pm b/lib/Graphics/Toolkit/Color/Space.pm index 0ec2f6a..0c9ead8 100644 --- a/lib/Graphics/Toolkit/Color/Space.pm +++ b/lib/Graphics/Toolkit/Color/Space.pm @@ -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; } @@ -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; diff --git a/lib/Graphics/Toolkit/Color/Space/Shape.pm b/lib/Graphics/Toolkit/Color/Space/Shape.pm index bbc6be9..c5d017a 100644 --- a/lib/Graphics/Toolkit/Color/Space/Shape.pm +++ b/lib/Graphics/Toolkit/Color/Space/Shape.pm @@ -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 ); diff --git a/t/04_space.t b/t/04_space.t index 8d098af..c476479 100644 --- a/t/04_space.t +++ b/t/04_space.t @@ -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] ); diff --git a/t/10_rgb_space.t b/t/10_rgb_space.t index a922222..c5818b9 100644 --- a/t/10_rgb_space.t +++ b/t/10_rgb_space.t @@ -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'); @@ -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; diff --git a/t/11_cmy_space.t b/t/11_cmy_space.t index c8ff493..933778d 100644 --- a/t/11_cmy_space.t +++ b/t/11_cmy_space.t @@ -14,61 +14,61 @@ is( ref $def, 'Graphics::Toolkit::Color::Space', 'got space object by loading mo is( $def->name, 'CMY', 'color space has right name'); is( $def->dimensions, 3, 'color space has 3 dimensions'); -ok( !$def->check([0,0,0]), 'check cmy values works on lower bound values'); -ok( !$def->check([1, 1, 1]), 'check cmy values works on upper bound values'); -warning_like {$def->check([0,0])} {carped => qr/needs 3 values/}, "check cmy got too few values"; -warning_like {$def->check([0, 0, 0, 0])} {carped => qr/needs 3 values/}, "check cmy got too many values"; -warning_like {$def->check([-1, 0, 0])} {carped => qr/cyan value/}, "cyan value is too small"; -warning_like {$def->check([2, 0, 0])} {carped => qr/cyan value/}, "cyan value is too big"; -warning_like {$def->check([0, -1, 0])} {carped => qr/magenta value/}, "magenta value is too small"; -warning_like {$def->check([0, 2, 0])} {carped => qr/magenta value/}, "magenta value is too big"; -warning_like {$def->check([0, 0, -1 ] )} {carped => qr/yellow value/}, "yellow value is too small"; -warning_like {$def->check([0, 0, 2] )} {carped => qr/yellow value/}, "yellow value is too big"; - - -my @cmy = $def->clamp([]); -is( int @cmy, 3, 'default color is set by clamp'); -is( $cmy[0], 0, 'default color is black (C) no args'); -is( $cmy[1], 0, 'default color is black (M) no args'); -is( $cmy[2], 0, 'default color is black (Y) no args'); - -@cmy = $def->clamp([0, 1]); -is( int @cmy, 3, 'clamp added missing argument in vector'); -is( $cmy[0], 0, 'passed (C) value'); -is( $cmy[1], 1, 'passed (M) value'); -is( $cmy[2], 0, 'added (Y) value when too few args'); - -@cmy = $def->clamp([-0.1, 2, 0.5, 0.4, 0.5]); -is( int @cmy, 3, 'removed missing argument in value vector by clamp'); -is( $cmy[0], 0, 'clamped up (C) value to minimum'); -is( $cmy[1], 1, 'clamped down (M) value to maximum'); -is( $cmy[2], 0.5, 'passed (Y) value'); - - -@cmy = $def->deconvert( [0, 0.1, 1], 'RGB'); -is( int @cmy, 3, 'converted RGB values to CMY'); -is( $cmy[0], 1, 'converted to maximal cyan value'); -is( $cmy[1], 0.9, 'converted to mid magenta value'); -is( $cmy[2], 0, 'converted to minimal yellow value'); - -my @rgb = $def->convert( [1, 0.9, 0 ], 'RGB'); -is( int @rgb, 3, 'converted CMY to RGB triplets'); -is( $rgb[0], 0, 'converted red value'); -is( $rgb[1], 0.1, 'converted green value'); -is( $rgb[2], 1, 'converted blue value'); - - -my @d = $def->delta([.2,.2,.2],[.2,.2,.2]); -is( int @d, 3, 'zero delta vector has right length'); -is( $d[0], 0, 'no delta in C component'); -is( $d[1], 0, 'no delta in M component'); -is( $d[2], 0, 'no delta in Y component'); - -@d = $def->delta([0.1,0.2,0.4],[0, 0.5, 1]); -is( int @d, 3, 'delta vector has right length'); -is( $d[0], -0.1, 'C delta'); -is( $d[1], 0.3, 'M delta'); -is( $d[2], 0.6, 'Y delta'); +is( ref $def->in_range([0,0,0]), 'ARRAY', 'check CMY values works on lower bound values'); +is( ref $def->in_range([1, 1, 1]), 'ARRAY', 'check CMY values works on upper bound values'); +is( ref $def->in_range([0,0]), '', "CMY got too few values"); +is( ref $def->in_range([0, 0, 0, 0]), '', "CMY got too many values"); +is( ref $def->in_range([-1, 0, 0]), '', "cyan value is too small"); +is( ref $def->in_range([2, 0, 0]), '', "cyan value is too big"); +is( ref $def->in_range([0, -1, 0]), '', "magenta value is too small"); +is( ref $def->in_range([0, 2, 0]), '', "magenta value is too big"); +is( ref $def->in_range([0, 0, -1 ] ), '', "yellow value is too small"); +is( ref $def->in_range([0, 0, 2] ), '', "yellow value is too big"); + + +my $cmy = $def->clamp([]); +is( int @$cmy, 3, 'default color is set by clamp'); +is( $cmy->[0], 0, 'default color is black (C) no args'); +is( $cmy->[1], 0, 'default color is black (M) no args'); +is( $cmy->[2], 0, 'default color is black (Y) no args'); + +$cmy = $def->clamp([0, 1]); +is( int @$cmy, 3, 'clamp added missing argument in vector'); +is( $cmy->[0], 0, 'passed (C) value'); +is( $cmy->[1], 1, 'passed (M) value'); +is( $cmy->[2], 0, 'added (Y) value when too few args'); + +$cmy = $def->clamp([-0.1, 2, 0.5, 0.4, 0.5]); +is( int @$cmy, 3, 'removed missing argument in value vector by clamp'); +is( $cmy->[0], 0, 'clamped up (C) value to minimum'); +is( $cmy->[1], 1, 'clamped down (M) value to maximum'); +is( $cmy->[2], 0.5, 'passed (Y) value'); + + +$cmy = $def->deconvert( [0, 0.1, 1], 'RGB'); +is( int @$cmy, 3, 'converted RGB values to CMY'); +is( $cmy->[0], 1, 'converted to maximal cyan value'); +is( $cmy->[1], 0.9, 'converted to mid magenta value'); +is( $cmy->[2], 0, 'converted to minimal yellow value'); + +my $rgb = $def->convert( [1, 0.9, 0 ], 'RGB'); +is( int @$rgb, 3, 'converted CMY to RGB triplets'); +is( $rgb->[0], 0, 'converted red value'); +is( $rgb->[1], 0.1, 'converted green value'); +is( $rgb->[2], 1, 'converted blue value'); + + +my $d = $def->delta([.2,.2,.2],[.2,.2,.2]); +is( int @$d, 3, 'zero delta vector has right length'); +is( $d->[0], 0, 'no delta in C component'); +is( $d->[1], 0, 'no delta in M component'); +is( $d->[2], 0, 'no delta in Y component'); + +$d = $def->delta([0.1,0.2,0.4],[0, 0.5, 1]); +is( int @$d, 3, 'delta vector has right length'); +is( $d->[0], -0.1, 'C delta'); +is( $d->[1], 0.3, 'M delta'); +is( $d->[2], 0.6, 'Y delta'); exit 0; diff --git a/t/12_cmyk_space.t b/t/12_cmyk_space.t index e66a138..c1adc62 100644 --- a/t/12_cmyk_space.t +++ b/t/12_cmyk_space.t @@ -14,90 +14,91 @@ is( ref $def, 'Graphics::Toolkit::Color::Space', 'got tight return value by load is( $def->name, 'CMYK', 'color space has right name'); is( $def->dimensions, 4, 'color space has 4 dimensions'); -ok( !$def->check([0,0,0,0]), 'check cmy values works on lower bound values'); -ok( !$def->check([1, 1, 1,1]), 'check cmy values works on upper bound values'); -warning_like {$def->check([0,0,0])} {carped => qr/needs 4 values/}, "check cmyk got too few values"; -warning_like {$def->check([0,0,0,0,0])} {carped => qr/needs 4 values/}, "check cmyk got too many values"; -warning_like {$def->check([-1,0,0,0])} {carped => qr/cyan value/}, "cyan value is too small"; -warning_like {$def->check([2,0,0,0])} {carped => qr/cyan value/}, "cyan value is too big"; -warning_like {$def->check([0,-1,0,0])} {carped => qr/magenta value/}, "magenta value is too small"; -warning_like {$def->check([0,2,0,0])} {carped => qr/magenta value/}, "magenta value is too big"; -warning_like {$def->check([0,0,-1,0])} {carped => qr/yellow value/}, "yellow value is too small"; -warning_like {$def->check([0,0,2,0])} {carped => qr/yellow value/}, "yellow value is too big"; -warning_like {$def->check([0,0,0,-1])} {carped => qr/key value/}, "key value is too small"; -warning_like {$def->check([0,0,0,2])} {carped => qr/key value/}, "key value is too big"; - - -my @cmyk = $def->clamp([]); -is( int @cmyk, 4, 'missing args are clamped down to black (default color)'); -is( $cmyk[0], 0, 'default color is black (C)'); -is( $cmyk[1], 0, 'default color is black (M)'); -is( $cmyk[2], 0, 'default color is black (Y)'); -is( $cmyk[3], 0, 'default color is black (K)'); - -@cmyk = $def->clamp([0.1, 0.2, 0.3]); -is( int @cmyk, 4, 'clamp added missing argument in vector'); -is( $cmyk[0], 0.1, 'passed (C) value when too few args'); -is( $cmyk[1], 0.2, 'passed (M) value when too few args'); -is( $cmyk[2], 0.3, 'passed (Y) value when too few args'); -is( $cmyk[3], 0, 'added zero value (K) when too few args'); - -@cmyk = $def->clamp([0.1, 0.2, 0.3, 0.4, 0.5]); -is( int @cmyk, 4, 'clamp removed missing argument in vector'); -is( $cmyk[0], 0.1, 'passed (C) value when too few args'); -is( $cmyk[1], 0.2, 'passed (M) value when too few args'); -is( $cmyk[2], 0.3, 'passed (Y) value when too few args'); -is( $cmyk[3], 0.4, 'added (K) value when too few args'); - -@cmyk = $def->clamp([-1,0,1,1.1]); -is( int @cmyk, 4, 'clamp kept vector length'); -is( $cmyk[0], 0, 'too low cyan value is clamped up'); -is( $cmyk[1], 0, 'min magenta value is kept'); -is( $cmyk[2], 1, 'max yellow value is kept'); -is( $cmyk[3], 1, 'too large key value is clamped down'); - - -@cmyk = $def->deconvert( [0.5, 0.5, 0.5], 'RGB'); -is( int @cmyk, 4, 'converted grey has four cmyk values'); -is( $cmyk[0], 0, 'converted grey has right cyan value'); -is( $cmyk[1], 0, 'converted grey has right magenta value'); -is( $cmyk[2], 0, 'converted grey has right yellow value'); -is( $cmyk[3], 0.5, 'converted grey has right key value'); - -my @rgb = $def->convert( [0, 0, 0, 0.5], 'RGB'); -is( int @rgb, 3, 'converted back grey has three rgb values'); -is( $rgb[0], 0.5, 'converted back grey has right red value'); -is( $rgb[1], 0.5, 'converted back grey has right green value'); -is( $rgb[2], 0.5, 'converted back grey has right blue value'); - -@cmyk = $def->deconvert( [0.3, 0.4, 0.5], 'RGB'); -is( int @cmyk, 4, 'converted color has four cmyk values'); -is( $cmyk[0], 0.4, 'converted color has right cyan value'); -is( $cmyk[1], 0.2, 'converted color has right magenta value'); -is( $cmyk[2], 0 , 'converted color has right yellow value'); -is( $cmyk[3], 0.5, 'converted color has right key value'); - -@rgb = $def->convert( [0.4, 0.2, 0, 0.5], 'RGB'); -is( int @rgb, 3, 'trimmed and converted back color black'); -is( $rgb[0], 0.3, 'right red value'); -is( $rgb[1], 0.4, 'right green value'); -is( $rgb[2], 0.5, 'right blue value'); - -@cmyk = $def->deformat([cmyk => 11, 22, 256, -1]); -is( int @cmyk, 4, 'deformat lc named ARRAY: got 4 values'); -is( $cmyk[0], 11, 'cyan got transported'); -is( $cmyk[1], 22, 'also too large magenta'); -is( $cmyk[2], 256, 'yallow transported, range ignored'); -is( $cmyk[3], -1, 'too small key ignored'); - -@cmyk = $def->deformat(['CMYK', 11, 22, 33]); -is( $cmyk[0], undef, 'OO deformat reacts only to right amount of values'); - -@cmyk = $def->deformat('cmyk: -1, 256, 3.3, 4 '); -is( int @cmyk, 4, 'deformat STRING: got 4 values'); -is( $cmyk[0], -1, 'cyan'); -is( $cmyk[1], 256, 'magenta'); -is( $cmyk[2], 3.3, 'yellow'); -is( $cmyk[3], 4, 'key value'); +is( ref $def->in_range([0,0,0, 0]), 'ARRAY', 'check CMYK values works on lower bound values'); +is( ref $def->in_range([1, 1, 1, 1]), 'ARRAY', 'check CMYK values works on upper bound values'); +is( ref $def->in_range([0,0,0]), '', "CMYK got too few values"); +is( ref $def->in_range([0, 0, 0, 0, 0]), '', "CMYK got too many values"); + +is( ref $def->in_range([-1, 0, 0, 0]), '', "cyan value is too small"); +is( ref $def->in_range([2, 0, 0, 0]), '', "cyan value is too big"); +is( ref $def->in_range([0, -1, 0, 0]), '', "magenta value is too small"); +is( ref $def->in_range([0, 2, 0, 0]), '', "magenta value is too big"); +is( ref $def->in_range([0, 0, -1, 0 ] ), '', "yellow value is too small"); +is( ref $def->in_range([0, 0, 2, 0] ), '', "yellow value is too big"); +is( ref $def->in_range([0, 0, 0, -1] ), '', "key value is too small"); +is( ref $def->in_range([0, 0, 0, 2] ), '', "key value is too big"); + + +my $cmyk = $def->clamp([]); +is( int @$cmyk, 4, 'missing args are clamped down to black (default color)'); +is( $cmyk->[0], 0, 'default color is black (C)'); +is( $cmyk->[1], 0, 'default color is black (M)'); +is( $cmyk->[2], 0, 'default color is black (Y)'); +is( $cmyk->[3], 0, 'default color is black (K)'); + +$cmyk = $def->clamp([0.1, 0.2, 0.3]); +is( int @$cmyk, 4, 'clamp added missing argument in vector'); +is( $cmyk->[0], 0.1, 'passed (C) value when too few args'); +is( $cmyk->[1], 0.2, 'passed (M) value when too few args'); +is( $cmyk->[2], 0.3, 'passed (Y) value when too few args'); +is( $cmyk->[3], 0, 'added zero value (K) when too few args'); + +$cmyk = $def->clamp([0.1, 0.2, 0.3, 0.4, 0.5]); +is( int @$cmyk, 4, 'clamp removed missing argument in vector'); +is( $cmyk->[0], 0.1, 'passed (C) value when too few args'); +is( $cmyk->[1], 0.2, 'passed (M) value when too few args'); +is( $cmyk->[2], 0.3, 'passed (Y) value when too few args'); +is( $cmyk->[3], 0.4, 'added (K) value when too few args'); + +$cmyk = $def->clamp([-1,0,1,1.1]); +is( int @$cmyk, 4, 'clamp kept vector length'); +is( $cmyk->[0], 0, 'too low cyan value is clamped up'); +is( $cmyk->[1], 0, 'min magenta value is kept'); +is( $cmyk->[2], 1, 'max yellow value is kept'); +is( $cmyk->[3], 1, 'too large key value is clamped down'); + + +$cmyk = $def->deconvert( [0.5, 0.5, 0.5], 'RGB'); +is( int @$cmyk, 4, 'converted grey has four cmyk values'); +is( $cmyk->[0], 0, 'converted grey has right cyan value'); +is( $cmyk->[1], 0, 'converted grey has right magenta value'); +is( $cmyk->[2], 0, 'converted grey has right yellow value'); +is( $cmyk->[3], 0.5, 'converted grey has right key value'); + +my $rgb = $def->convert( [0, 0, 0, 0.5], 'RGB'); +is( int @$rgb, 3, 'converted back grey has three rgb values'); +is( $rgb->[0], 0.5, 'converted back grey has right red value'); +is( $rgb->[1], 0.5, 'converted back grey has right green value'); +is( $rgb->[2], 0.5, 'converted back grey has right blue value'); + +$cmyk = $def->deconvert( [0.3, 0.4, 0.5], 'RGB'); +is( int @$cmyk, 4, 'converted color has four cmyk values'); +is( $cmyk->[0], 0.4, 'converted color has right cyan value'); +is( $cmyk->[1], 0.2, 'converted color has right magenta value'); +is( $cmyk->[2], 0 , 'converted color has right yellow value'); +is( $cmyk->[3], 0.5, 'converted color has right key value'); + +$rgb = $def->convert( [0.4, 0.2, 0, 0.5], 'RGB'); +is( int @$rgb, 3, 'trimmed and converted back color black'); +is( $rgb->[0], 0.3, 'right red value'); +is( $rgb->[1], 0.4, 'right green value'); +is( $rgb->[2], 0.5, 'right blue value'); + +$cmyk = $def->deformat([cmyk => 11, 22, 256, -1]); +is( int @$cmyk, 4, 'deformat lc named ARRAY: got 4 values'); +is( $cmyk->[0], 11, 'cyan got transported'); +is( $cmyk->[1], 22, 'also too large magenta'); +is( $cmyk->[2], 256, 'yallow transported, range ignored'); +is( $cmyk->[3], -1, 'too small key ignored'); + +$cmyk = $def->deformat(['CMYK', 11, 22, 33]); +is( $cmyk, undef, 'OO deformat reacts only to right amount of values'); + +$cmyk = $def->deformat('cmyk: -1, 256, 3.3, 4 '); +is( int @$cmyk, 4, 'deformat STRING: got 4 values'); +is( $cmyk->[0], -1, 'cyan'); +is( $cmyk->[1], 256, 'magenta'); +is( $cmyk->[2], 3.3, 'yellow'); +is( $cmyk->[3], 4, 'key value'); exit 0; diff --git a/t/13_hsl_space.t b/t/13_hsl_space.t index e477c24..866ea6a 100644 --- a/t/13_hsl_space.t +++ b/t/13_hsl_space.t @@ -9,81 +9,79 @@ BEGIN { unshift @INC, 'lib', '../lib'} my $module = 'Graphics::Toolkit::Color::Space::Instance::HSL'; my $def = eval "require $module"; -use Graphics::Toolkit::Color::Space::Util ':all'; +use Graphics::Toolkit::Color::Space::Util 'close_enough'; is( not($@), 1, 'could load the module'); is( ref $def, 'Graphics::Toolkit::Color::Space', 'got tight return value by loading module'); is( $def->name, 'HSL', 'color space has right name'); is( $def->dimensions, 3, 'color space has 3 dimensions'); - -ok( !$def->check([0,0,0]), 'check hsl values works on lower bound values'); -ok( !$def->check([360,100,100]), 'check hsl values works on upper bound values'); -warning_like {$def->check([0,0])} {carped => qr/needs 3 values/}, "check cmy got too few values"; -warning_like {$def->check([0, 0, 0, 0])} {carped => qr/needs 3 values/}, "check cmy got too many values"; - -warning_like {$def->check([-1, 0, 0])} {carped => qr/hue value/}, "hue value is too small"; -warning_like {$def->check([0.5, 0,0])} {carped => qr/hue value/}, "hue value is not integer"; -warning_like {$def->check([361, 0,0])} {carped => qr/hue value/}, "hue value is too big"; -warning_like {$def->check([0, -1, 0])} {carped => qr/saturation value/}, "saturation value is too small"; -warning_like {$def->check([0, 0.5,0])} {carped => qr/saturation value/}, "saturation value is not integer"; -warning_like {$def->check([0, 101,0])} {carped => qr/saturation value/}, "saturation value is too big"; -warning_like {$def->check([0,0, -1 ])} {carped => qr/lightness value/}, "lightness value is too small"; -warning_like {$def->check([0,0, 0.5])} {carped => qr/lightness value/}, "lightness value is not integer"; -warning_like {$def->check([0,0, 101])} {carped => qr/lightness value/}, "lightness value is too big"; - - -my @hsl = $def->clamp([]); -is( int @hsl, 3, 'missing values are clamped to black (default color)'); -is( $hsl[0], 0, 'default color is black (H)'); -is( $hsl[1], 0, 'default color is black (S)'); -is( $hsl[2], 0, 'default color is black (L)'); - -@hsl = $def->clamp([0,100]); -is( int @hsl, 3, 'clamp added missing value'); -is( $hsl[0], 0, 'carried first min value (H)'); -is( $hsl[1], 100, 'carried second max value (S)'); -is( $hsl[2], 0, 'set missing value to zero'); - -@hsl = $def->clamp( [-1, -1, 101, 4]); -is( int @hsl, 3, 'clamp removed superfluous value'); -is( $hsl[0], 359, 'rotated up (H) value'); -is( $hsl[1], 0, 'clamped up (S) value'); -is( $hsl[2], 100, 'clamped down(L) value');; - -@hsl = $def->deconvert( [0.5, 0.5, 0.5], 'RGB'); -is( int @hsl, 3, 'converted color grey has three hsl values'); -is( $hsl[0], 0, 'converted color grey has computed right hue value'); -is( $hsl[1], 0, 'converted color grey has computed right saturation'); -is( $hsl[2], 0.5, 'converted color grey has computed right lightness'); - -my @rgb = $def->convert( [0, 0, 0.5], 'RGB'); -is( int @rgb, 3, 'converted back color grey has three rgb values'); -is( $rgb[0], 0.5, 'converted back color grey has right red value'); -is( $rgb[1], 0.5, 'converted back color grey has right green value'); -is( $rgb[2], 0.5, 'converted back color grey has right blue value'); - - -@hsl = $def->deconvert( [0.00784, 0.7843, 0.0902], 'RGB'); -is( int @hsl, 3, 'converted blue color has three hsl values'); -is( close_enough($hsl[0], 0.35097493), 1, 'converted color grey has computed right hue value'); -is( close_enough($hsl[1], 0.98), 1, 'converted color grey has computed right saturation'); -is( close_enough($hsl[2], 0.4), 1, 'converted color grey has computed right lightness'); - -@rgb = $def->convert( [0.351, 0.98, 0.4], 'RGB'); -is( int @rgb, 3, 'converted back color grey has three rgb values'); -is( close_enough($rgb[0], 0.00784), 1, 'converted back color grey has right red value'); -is( close_enough($rgb[1], 0.7843), 1, 'converted back color grey has right green value'); -is( close_enough($rgb[2], 0.0902), 1, 'converted back color grey has right blue value'); - -my @d = $def->delta([0.3,0.3,0.3],[0.3,0.4,0.2]); -is( int @d, 3, 'delta vector has right length'); -is( $d[0], 0, 'no delta in hue component'); -is( $d[1], 0.1, 'positive delta in saturation component'); -is( $d[2], -0.1, 'negatve delta in lightness component'); - -@d = $def->delta([0.9,0,0],[0.1,0,0]); -is( $d[0], .2, 'negative delta across the cylindrical border'); -@d = $def->delta([0.3,0,0],[0.9,0,0]); -is( $d[0], -.4, 'negative delta because cylindrical quality of dimension'); +is( ref $def->in_range([0, 0, 0]), 'ARRAY', 'check HSL values works on lower bound values'); +is( ref $def->in_range([360,100,100]), 'ARRAY', 'check HSL values works on upper bound values'); +is( ref $def->in_range([0,0]), '', "HSL got too few values"); +is( ref $def->in_range([0, 0, 0, 0]), '', "HSL got too many values"); +is( ref $def->in_range([-1, 0, 0]), '', "hue value is too small"); +is( ref $def->in_range([1.1, 0, 0]), '', "hue is not integer"); +is( ref $def->in_range([361, 0, 0]), '', "hue value is too big"); +is( ref $def->in_range([0, -1, 0]), '', "saturation value is too small"); +is( ref $def->in_range([0, 1.1, 0]), '', "saturation value is not integer"); +is( ref $def->in_range([0, 101, 0]), '', "saturation value is too big"); +is( ref $def->in_range([0, 0, -1 ] ), '', "lightness value is too small"); +is( ref $def->in_range([0, 0, 1.1] ), '', "lightness value is not integer"); +is( ref $def->in_range([0, 0, 101] ), '', "lightness value is too big"); + + +my $hsl = $def->clamp([]); +is( int @$hsl, 3, 'missing values are clamped to black (default color)'); +is( $hsl->[0], 0, 'default color is black (H)'); +is( $hsl->[1], 0, 'default color is black (S)'); +is( $hsl->[2], 0, 'default color is black (L)'); + +$hsl = $def->clamp([0,100]); +is( int @$hsl, 3, 'clamp added missing value'); +is( $hsl->[0], 0, 'carried first min value (H)'); +is( $hsl->[1], 100, 'carried second max value (S)'); +is( $hsl->[2], 0, 'set missing value to zero'); + +$hsl = $def->clamp( [-1, -1, 101, 4]); +is( int @$hsl, 3, 'clamp removed superfluous value'); +is( $hsl->[0], 359, 'rotated up (H) value'); +is( $hsl->[1], 0, 'clamped up (S) value'); +is( $hsl->[2], 100, 'clamped down(L) value');; + + +$hsl = $def->deconvert( [0.5, 0.5, 0.5], 'RGB'); +is( int @$hsl, 3, 'converted color grey has three hsl values'); +is( $hsl->[0], 0, 'converted color grey has computed right hue value'); +is( $hsl->[1], 0, 'converted color grey has computed right saturation'); +is( $hsl->[2], 0.5, 'converted color grey has computed right lightness'); + +my $rgb = $def->convert( [0, 0, 0.5], 'RGB'); +is( int @$rgb, 3, 'converted back color grey has three rgb values'); +is( $rgb->[0], 0.5, 'converted back color grey has right red value'); +is( $rgb->[1], 0.5, 'converted back color grey has right green value'); +is( $rgb->[2], 0.5, 'converted back color grey has right blue value'); + +$hsl = $def->deconvert( [0.00784, 0.7843, 0.0902], 'RGB'); +is( int @$hsl, 3, 'converted blue color has three hsl values'); +is( close_enough($hsl->[0], 0.35097493), 1, 'converted color blue has computed right hue value'); +is( close_enough($hsl->[1], 0.98), 1, 'converted color blue has computed right saturation'); +is( close_enough($hsl->[2], 0.4), 1, 'converted color blue has computed right lightness'); + +$rgb = $def->convert( [0.351, 0.98, 0.4], 'RGB'); +is( int @$rgb, 3, 'converted back color grey has three rgb values'); +is( close_enough($rgb->[0], 0.00784), 1, 'converted back color grey has right red value'); +is( close_enough($rgb->[1], 0.7843), 1, 'converted back color grey has right green value'); +is( close_enough($rgb->[2], 0.0902), 1, 'converted back color grey has right blue value'); + +my $d = $def->delta([0.3,0.3,0.3],[0.3,0.4,0.2]); +is( int @$d, 3, 'delta vector has right length'); +is( $d->[0], 0, 'no delta in hue component'); +is( $d->[1], 0.1, 'positive delta in saturation component'); +is( $d->[2], -0.1, 'negatve delta in lightness component'); + +$d = $def->delta([0.9,0,0],[0.1,0,0]); +is( $d->[0], .2, 'negative delta across the cylindrical border'); +$d = $def->delta([0.3,0,0],[0.9,0,0]); +is( $d->[0], -.4, 'negative delta because cylindrical quality of dimension'); exit 0; diff --git a/t/14_hsv_space.t b/t/14_hsv_space.t index 6d53549..3536a58 100644 --- a/t/14_hsv_space.t +++ b/t/14_hsv_space.t @@ -1,5 +1,5 @@ #!/usr/bin/perl - + use v5.12; use warnings; use Test::More tests => 53; @@ -15,73 +15,72 @@ is( ref $def, 'Graphics::Toolkit::Color::Space', 'got tight return value by load is( $def->name, 'HSV', 'color space has right name'); is( $def->dimensions, 3, 'color space has 3 dimensions'); +is( ref $def->in_range([0, 0, 0]), 'ARRAY', 'check HSV values works on lower bound values'); +is( ref $def->in_range([360,100,100]), 'ARRAY', 'check HSV values works on upper bound values'); +is( ref $def->in_range([0,0]), '', "HSV got too few values"); +is( ref $def->in_range([0, 0, 0, 0]), '', "HSV got too many values"); +is( ref $def->in_range([-1, 0, 0]), '', "hue value is too small"); +is( ref $def->in_range([1.1, 0, 0]), '', "hue is not integer"); +is( ref $def->in_range([361, 0, 0]), '', "hue value is too big"); +is( ref $def->in_range([0, -1, 0]), '', "saturation value is too small"); +is( ref $def->in_range([0, 1.1, 0]), '', "saturation value is not integer"); +is( ref $def->in_range([0, 101, 0]), '', "saturation value is too big"); +is( ref $def->in_range([0, 0, -1 ] ), '', "value value is too small"); +is( ref $def->in_range([0, 0, 1.1] ), '', "value value is not integer"); +is( ref $def->in_range([0, 0, 101] ), '', "value value is too big"); -ok( !$def->check([0,0,0]), 'check hsv values works on lower bound values'); -ok( !$def->check([360,100,100]), 'check hsv values works on upper bound values'); -warning_like {$def->check([0,0])} {carped => qr/needs 3 values/}, "check cmy got too few values"; -warning_like {$def->check([0, 0, 0, 0])} {carped => qr/needs 3 values/}, "check cmy got too many values"; - -warning_like {$def->check([-1, 0, 0])} {carped => qr/hue value/}, "hue value is too small"; -warning_like {$def->check([0.5, 0,0])} {carped => qr/hue value/}, "hue value is not integer"; -warning_like {$def->check([361, 0,0])} {carped => qr/hue value/}, "hue value is too big"; -warning_like {$def->check([0, -1, 0])} {carped => qr/saturation value/}, "saturation value is too small"; -warning_like {$def->check([0, 0.5,0])} {carped => qr/saturation value/}, "saturation value is not integer"; -warning_like {$def->check([0, 101,0])} {carped => qr/saturation value/}, "saturation value is too big"; -warning_like {$def->check([0,0, -1 ])} {carped => qr/value value/}, "value value is too small"; -warning_like {$def->check([0,0, 0.5])} {carped => qr/value value/}, "value value is not integer"; -warning_like {$def->check([0,0, 101])} {carped => qr/value value/}, "value value is too big"; -my @hsv = $def->clamp([]); -is( int @hsv, 3, 'clamp added three missing values as zero'); -is( $hsv[0], 0, 'default color is black (H)'); -is( $hsv[1], 0, 'default color is black (S)'); -is( $hsv[2], 0, 'default color is black (V)'); -@hsv = $def->clamp([0,100]); -is( int @hsv, 3, 'added one missing value'); -is( $hsv[0], 0, 'carried first min value'); -is( $hsv[1], 100, 'carried second max value'); -is( $hsv[2], 0, 'set missing color value to zero (V)'); -@hsv = $def->clamp([-1.1,-1,101,4]); -is( int @hsv, 3, 'removed superfluous value'); -is( $hsv[0], 359, 'rotated up (H) value and removed decimals'); -is( $hsv[1], 0, 'clamped up too small (S) value'); -is( $hsv[2], 100, 'clamped down too large (V) value');; +my $hsv = $def->clamp([]); +is( int @$hsv, 3, 'clamp added three missing values as zero'); +is( $hsv->[0], 0, 'default color is black (H)'); +is( $hsv->[1], 0, 'default color is black (S)'); +is( $hsv->[2], 0, 'default color is black (V)'); +$hsv = $def->clamp([0,100]); +is( int @$hsv, 3, 'added one missing value'); +is( $hsv->[0], 0, 'carried first min value'); +is( $hsv->[1], 100, 'carried second max value'); +is( $hsv->[2], 0, 'set missing color value to zero (V)'); +$hsv = $def->clamp([-1.1,-1,101,4]); +is( int @$hsv, 3, 'removed superfluous value'); +is( $hsv->[0], 359, 'rotated up (H) value and removed decimals'); +is( $hsv->[1], 0, 'clamped up too small (S) value'); +is( $hsv->[2], 100, 'clamped down too large (V) value');; -@hsv = $def->deconvert( [0.5, 0.5, 0.5], 'RGB'); -is( int @hsv, 3, 'converted color grey has three hsv values'); -is( $hsv[0], 0, 'converted color grey has computed right hue value'); -is( $hsv[1], 0, 'converted color grey has computed right saturation'); -is( $hsv[2], 0.5, 'converted color grey has computed right value'); +$hsv = $def->deconvert( [0.5, 0.5, 0.5], 'RGB'); +is( int @$hsv, 3, 'converted color grey has three hsv values'); +is( $hsv->[0], 0, 'converted color grey has computed right hue value'); +is( $hsv->[1], 0, 'converted color grey has computed right saturation'); +is( $hsv->[2], 0.5, 'converted color grey has computed right value'); -my @rgb = $def->convert( [0, 0, 0.5], 'RGB'); -is( int @rgb, 3, 'converted back color grey has three rgb values'); -is( $rgb[0], 0.5, 'converted back color grey has right red value'); -is( $rgb[1], 0.5, 'converted back color grey has right green value'); -is( $rgb[2], 0.5, 'converted back color grey has right blue value'); +my $rgb = $def->convert( [0, 0, 0.5], 'RGB'); +is( int @$rgb, 3, 'converted back color grey has three rgb values'); +is( $rgb->[0], 0.5, 'converted back color grey has right red value'); +is( $rgb->[1], 0.5, 'converted back color grey has right green value'); +is( $rgb->[2], 0.5, 'converted back color grey has right blue value'); -@rgb = $def->convert( [0.972222222, 0.9, 0.78], 'RGB'); -is( int @rgb, 3, 'converted red color into tripled'); -is( $rgb[0], 0.78, 'right red value'); -is( $rgb[1], 0.078, 'right green value'); -is( close_enough($rgb[2], 0.196), 1, 'right blue value'); +$rgb = $def->convert( [0.972222222, 0.9, 0.78], 'RGB'); +is( int @$rgb, 3, 'converted red color into tripled'); +is( $rgb->[0], 0.78, 'right red value'); +is( $rgb->[1], 0.078, 'right green value'); +is( close_enough($rgb->[2], 0.196), 1, 'right blue value'); -@hsv = $def->deconvert( [0.78, 0.078, 0.196078431], 'RGB'); -is( int @hsv, 3, 'converted nice blue has three hsv values'); -is( close_enough($hsv[0], 0.97222), 1, 'converted nice blue has computed right hue value'); -is( $hsv[1], .9, 'converted nice blue has computed right saturation'); -is( $hsv[2], .78, 'converted nice blue has computed right value'); +$hsv = $def->deconvert( [0.78, 0.078, 0.196078431], 'RGB'); +is( int @$hsv, 3, 'converted nice blue has three hsv values'); +is( close_enough($hsv->[0], 0.97222), 1, 'converted nice blue has computed right hue value'); +is( $hsv->[1], .9, 'converted nice blue has computed right saturation'); +is( $hsv->[2], .78, 'converted nice blue has computed right value'); -@rgb = $def->convert( [0.76666, .83, .24], 'RGB'); -is( int @rgb, 3, 'converted red color into tripled'); -is( close_enough($rgb[0], 0.156862), 1, 'right red value'); -is( close_enough($rgb[1], 0.03921), 1, 'right green value'); -is( close_enough($rgb[2], 0.2352), 1, 'right blue value'); +$rgb = $def->convert( [0.76666, .83, .24], 'RGB'); +is( int @$rgb, 3, 'converted red color into tripled'); +is( close_enough($rgb->[0], 0.156862), 1, 'right red value'); +is( close_enough($rgb->[1], 0.03921), 1, 'right green value'); +is( close_enough($rgb->[2], 0.2352), 1, 'right blue value'); -@hsv = $def->deconvert( [40/255, 10/255, 60/255], 'RGB'); -is( int @hsv, 3, 'converted nice blue has three hsv values'); -is( close_enough($hsv[0], 0.766666), 1, 'converted nice blue has computed right hue value'); -is( close_enough($hsv[1], .83), 1, 'converted nice blue has computed right saturation'); -is( close_enough($hsv[2], .24), 1, 'converted nice blue has computed right value'); +$hsv = $def->deconvert( [40/255, 10/255, 60/255], 'RGB'); +is( int @$hsv, 3, 'converted nice blue has three hsv values'); +is( close_enough($hsv->[0], 0.766666), 1, 'converted nice blue has computed right hue value'); +is( close_enough($hsv->[1], .83), 1, 'converted nice blue has computed right saturation'); +is( close_enough($hsv->[2], .24), 1, 'converted nice blue has computed right value'); exit 0; diff --git a/t/15_hsb_space.t b/t/15_hsb_space.t index 3cf073b..2b044a1 100644 --- a/t/15_hsb_space.t +++ b/t/15_hsb_space.t @@ -15,73 +15,71 @@ is( not($@), 1, 'could load the module'); is( ref $def, 'Graphics::Toolkit::Color::Space', 'got tight return value by loading module'); is( $def->name, 'HSB', 'color space has right name'); is( $def->dimensions, 3, 'color space has 3 dimensions'); +is( ref $def->in_range([0, 0, 0]), 'ARRAY', 'check HSB values works on lower bound values'); +is( ref $def->in_range([360,100,100]), 'ARRAY', 'check HSB values works on upper bound values'); +is( ref $def->in_range([0,0]), '', "HSB got too few values"); +is( ref $def->in_range([0, 0, 0, 0]), '', "HSB got too many values"); +is( ref $def->in_range([-1, 0, 0]), '', "hue value is too small"); +is( ref $def->in_range([1.1, 0, 0]), '', "hue is not integer"); +is( ref $def->in_range([361, 0, 0]), '', "hue value is too big"); +is( ref $def->in_range([0, -1, 0]), '', "saturation value is too small"); +is( ref $def->in_range([0, 1.1, 0]), '', "saturation value is not integer"); +is( ref $def->in_range([0, 101, 0]), '', "saturation value is too big"); +is( ref $def->in_range([0, 0, -1 ] ), '', "brightness value is too small"); +is( ref $def->in_range([0, 0, 1.1] ), '', "brightness value is not integer"); +is( ref $def->in_range([0, 0, 101] ), '', "brightness value is too big"); -ok( !$def->check([0,0,0]), 'check hsb values works on lower bound values'); -ok( !$def->check([360,100,100]), 'check hsb values works on upper bound values'); -warning_like {$def->check([0,0])} {carped => qr/needs 3 values/}, "check cmy got too few values"; -warning_like {$def->check([0, 0, 0, 0])} {carped => qr/needs 3 values/}, "check cmy got too many values"; +my $hsb = $def->clamp([]); +is( int @$hsb, 3, 'clamp added three missing values as zero'); +is( $hsb->[0], 0, 'default color is black (H)'); +is( $hsb->[1], 0, 'default color is black (S)'); +is( $hsb->[2], 0, 'default color is black (B)'); +$hsb = $def->clamp([0,100]); +is( int @$hsb, 3, 'added one missing value'); +is( $hsb->[0], 0, 'carried first min value'); +is( $hsb->[1], 100, 'carried second max value'); +is( $hsb->[2], 0, 'set missing color value to zero (B)'); +$hsb = $def->clamp([-1.1,-1,101,4]); +is( int @$hsb, 3, 'removed superfluous value'); +is( $hsb->[0], 359, 'rotated up (H) value and removed decimals'); +is( $hsb->[1], 0, 'clamped up too small (S) value'); +is( $hsb->[2], 100, 'clamped down too large (B) value');; -warning_like {$def->check([-1, 0, 0])} {carped => qr/hue value/}, "hue value is too small"; -warning_like {$def->check([0.5, 0,0])} {carped => qr/hue value/}, "hue value is not integer"; -warning_like {$def->check([361, 0,0])} {carped => qr/hue value/}, "hue value is too big"; -warning_like {$def->check([0, -1, 0])} {carped => qr/saturation value/}, "saturation value is too small"; -warning_like {$def->check([0, 0.5,0])} {carped => qr/saturation value/}, "saturation value is not integer"; -warning_like {$def->check([0, 101,0])} {carped => qr/saturation value/}, "saturation value is too big"; -warning_like {$def->check([0,0, -1 ])} {carped => qr/brightness value/}, "value value is too small"; -warning_like {$def->check([0,0, 0.5])} {carped => qr/brightness value/}, "value value is not integer"; -warning_like {$def->check([0,0, 101])} {carped => qr/brightness value/}, "value value is too big"; -my @hsb = $def->clamp([]); -is( int @hsb, 3, 'clamp added three missing values as zero'); -is( $hsb[0], 0, 'default color is black (H)'); -is( $hsb[1], 0, 'default color is black (S)'); -is( $hsb[2], 0, 'default color is black (B)'); -@hsb = $def->clamp([0,100]); -is( int @hsb, 3, 'added one missing value'); -is( $hsb[0], 0, 'carried first min value'); -is( $hsb[1], 100, 'carried second max value'); -is( $hsb[2], 0, 'set missing color value to zero (B)'); -@hsb = $def->clamp([-1.1,-1,101,4]); -is( int @hsb, 3, 'removed superfluous value'); -is( $hsb[0], 359, 'rotated up (H) value and removed decimals'); -is( $hsb[1], 0, 'clamped up too small (S) value'); -is( $hsb[2], 100, 'clamped down too large (B) value');; +$hsb = $def->deconvert( [0.5, 0.5, 0.5], 'RGB'); +is( int @$hsb, 3, 'converted color grey has three hsb values'); +is( $hsb->[0], 0, 'converted color grey has computed right hue value'); +is( $hsb->[1], 0, 'converted color grey has computed right saturation'); +is( $hsb->[2], 0.5, 'converted color grey has computed right brightness'); +my $rgb = $def->convert( [0, 0, 0.5], 'RGB'); +is( int @$rgb, 3, 'converted back color grey has three rgb values'); +is( $rgb->[0], 0.5, 'converted back color grey has right red value'); +is( $rgb->[1], 0.5, 'converted back color grey has right green value'); +is( $rgb->[2], 0.5, 'converted back color grey has right blue value'); -@hsb = $def->deconvert( [0.5, 0.5, 0.5], 'RGB'); -is( int @hsb, 3, 'converted color grey has three hsb values'); -is( $hsb[0], 0, 'converted color grey has computed right hue value'); -is( $hsb[1], 0, 'converted color grey has computed right saturation'); -is( $hsb[2], 0.5, 'converted color grey has computed right brightness'); +$rgb = $def->convert( [0.972222222, 0.9, 0.78], 'RGB'); +is( int @$rgb, 3, 'converted red color into tripled'); +is( $rgb->[0], 0.78, 'right red value'); +is( $rgb->[1], 0.078, 'right green value'); +is( close_enough($rgb->[2], 0.196), 1, 'right blue value'); -my @rgb = $def->convert( [0, 0, 0.5], 'RGB'); -is( int @rgb, 3, 'converted back color grey has three rgb values'); -is( $rgb[0], 0.5, 'converted back color grey has right red value'); -is( $rgb[1], 0.5, 'converted back color grey has right green value'); -is( $rgb[2], 0.5, 'converted back color grey has right blue value'); +$hsb = $def->deconvert( [0.78, 0.078, 0.196078431], 'RGB'); +is( int @$hsb, 3, 'converted nice blue has three hsb values'); +is( close_enough($hsb->[0], 0.97222), 1, 'converted nice blue has computed right hue value'); +is( $hsb->[1], .9, 'converted nice blue has computed right saturation'); +is( $hsb->[2], .78, 'converted nice blue has computed right brightness'); -@rgb = $def->convert( [0.972222222, 0.9, 0.78], 'RGB'); -is( int @rgb, 3, 'converted red color into tripled'); -is( $rgb[0], 0.78, 'right red value'); -is( $rgb[1], 0.078, 'right green value'); -is( close_enough($rgb[2], 0.196), 1, 'right blue value'); +$rgb = $def->convert( [0.76666, .83, .24], 'RGB'); +is( int @$rgb, 3, 'converted red color into tripled'); +is( close_enough($rgb->[0], 0.156862), 1, 'right red value'); +is( close_enough($rgb->[1], 0.03921), 1, 'right green value'); +is( close_enough($rgb->[2], 0.2352), 1, 'right blue value'); -@hsb = $def->deconvert( [0.78, 0.078, 0.196078431], 'RGB'); -is( int @hsb, 3, 'converted nice blue has three hsb values'); -is( close_enough($hsb[0], 0.97222), 1, 'converted nice blue has computed right hue value'); -is( $hsb[1], .9, 'converted nice blue has computed right saturation'); -is( $hsb[2], .78, 'converted nice blue has computed right brightness'); - -@rgb = $def->convert( [0.76666, .83, .24], 'RGB'); -is( int @rgb, 3, 'converted red color into tripled'); -is( close_enough($rgb[0], 0.156862), 1, 'right red value'); -is( close_enough($rgb[1], 0.03921), 1, 'right green value'); -is( close_enough($rgb[2], 0.2352), 1, 'right blue value'); - -@hsb = $def->deconvert( [40/255, 10/255, 60/255], 'RGB'); -is( int @hsb, 3, 'converted nice blue has three hsb values'); -is( close_enough($hsb[0], 0.766666), 1, 'converted nice blue has computed right hue value'); -is( close_enough($hsb[1], .83), 1, 'converted nice blue has computed right saturation'); -is( close_enough($hsb[2], .24), 1, 'converted nice blue has computed right brightness'); +$hsb = $def->deconvert( [40/255, 10/255, 60/255], 'RGB'); +is( int @$hsb, 3, 'converted nice blue has three hsb values'); +is( close_enough($hsb->[0], 0.766666), 1, 'converted nice blue has computed right hue value'); +is( close_enough($hsb->[1], .83), 1, 'converted nice blue has computed right saturation'); +is( close_enough($hsb->[2], .24), 1, 'converted nice blue has computed right brightness'); exit 0;