Skip to content

Commit

Permalink
document Ncol space
Browse files Browse the repository at this point in the history
  • Loading branch information
lichtkind committed Apr 29, 2024
1 parent 5f1f655 commit b291ba3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
54 changes: 33 additions & 21 deletions lib/Graphics/Toolkit/Color/Space/Hub.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sub is_space { (defined $_[0] and ref get_space($_[0])) ? 1 : 0 }
sub base_space { $space_lookup{ $base_package } }
sub space_names { keys %{%space_lookup} }

########################################################################
#### space API #########################################################

sub add_space {
my $space = shift;
Expand Down Expand Up @@ -47,15 +47,16 @@ sub _check_values_and_space {
: 'need an ARRAY ref with '.$space->dimensions." $space_name values as first argument of $sub_name";
}

########################################################################
#### value API #########################################################

sub partial_hash_deformat { # convert partial hash into
my ($value_hash) = @_;
return unless ref $value_hash eq 'HASH';
for my $space_name (space_names()) {
my $color_space = get_space( $space_name );
my $pos_hash = $color_space->basis->deformat_partial_hash( $value_hash );
return $pos_hash, $color_space->name if ref $pos_hash eq 'HASH';
next unless ref $pos_hash eq 'HASH';
return wantarray ? ($pos_hash, $color_space->name) : $pos_hash;
}
return undef;
}
Expand All @@ -79,24 +80,6 @@ sub format { # @tuple --> % | % |~ ...
return @values == 1 ? $values[0] : @values;
}

sub deconvert { # @... --> @RGB (base color space) # normalized values only
my ($values, $space_name) = @_;
my $space = _check_values_and_space( 'deconvert', $values, $space_name );
return unless ref $space;
$values = $space->clamp( $values, 'normal', -1);
return $values if $space->name eq base_space->name;
$space->convert( $values, $base_package);
}

sub convert { # @RGB --> $@...|!~ # normalized values only
my ($values, $space_name) = @_;
my $space = _check_values_and_space( 'convert', $values, $space_name );
return $space unless ref $space;
$values = base_space->clamp( $values, 'normal', -1);
return $values if $space->name eq base_space->name;
$space->deconvert( $values, $base_package);
}

sub denormalize { # result clamped, alway in space
my ($values, $space_name, $range) = @_;
my $space = _check_values_and_space( 'denormalize', $values, $space_name );
Expand All @@ -114,6 +97,23 @@ sub normalize {
$space->normalize( $values, $range);
}

sub deconvert { # @... --> @RGB (base color space) # normalized values only
my ($values, $space_name) = @_;
my $space = _check_values_and_space( 'deconvert', $values, $space_name );
return unless ref $space;
$values = $space->clamp( $values, 'normal', -1);
return $values if $space->name eq base_space->name;
$space->convert( $values, $base_package);
}

sub convert { # @RGB --> $@...|!~ # normalized values only
my ($values, $space_name) = @_;
my $space = _check_values_and_space( 'convert', $values, $space_name );
return $space unless ref $space;
$values = base_space->clamp( $values, 'normal', -1);
return $values if $space->name eq base_space->name;
$space->deconvert( $values, $base_package);
}

1;

Expand Down Expand Up @@ -211,6 +211,17 @@ B<whiteness> and B<blackness>, desribing how much white or black are mixed in.
If both are zero, than we have a pure color. I<whiteness> of 100 always
leads to pure white and I<blackness> of 100 always leads to pure black.
=head2 NCol
Is a human readable derivation of the HWB space with altered B<hue> axis
that consicts of a letter and nwo digits. The letter demarks one of the
six areas around the rainbow B<R> (I<Red>), B<Y> (I<Yellow>), B<G> (I<Green),
B<C> (I<Cyan>), B<B> (I<Blue>), B<M> (I<Magenta). The two digits after this
letter are a percentual value, pointing to a position on the rainbow,
between the stated color by the letter and the next. The B<whiteness> and
B<blackness> axis have values with the suffix I<%>, since they are
percentual values as well.
=head2 YIQ
Has three linear dimensions:
Expand Down Expand Up @@ -244,6 +255,7 @@ Their ranges are 0 .. 100, -500 .. 500 and -200 .. 200.
Has three linear real valued dimension named B<luminance>, B<croma> and B<hue>.
Their ranges are 0 .. 100, -500 .. 500 and -200 .. 200.
=head1 RANGES
As pointed out in the previous paragraph, each dimension of color space has
Expand Down
5 changes: 2 additions & 3 deletions lib/Graphics/Toolkit/Color/Space/Shape.pm
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ sub denormalize {
$range = $self->_range( $range );
return "bad range definition" unless ref $range;

my @val = map { ($self->axis_is_numeric( $_ )) ? ($values->[$_] * ($range->[$_][1]-$range->[$_][0]) + $range->[$_][0])
: $values->[$_] } $self->basis->iterator;
return $self->round( \@val, $precision );
return [ map { ($self->axis_is_numeric( $_ )) ? ($values->[$_] * ($range->[$_][1]-$range->[$_][0]) + $range->[$_][0])
: $values->[$_] } $self->basis->iterator ];
}

sub denormalize_delta {
Expand Down
2 changes: 1 addition & 1 deletion lib/Graphics/Toolkit/Color/Values.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ sub distance { # _c1 _c2 -- ~space ~select @range --> +
my @values1 = $self->get( $space_name, 'list', 'normal' );
my @values2 = $c2->get( $space_name, 'list', 'normal' );
return unless defined $values1[0] and defined $values2[0];
my $delta = $space->delta( \@values1, \@values2 );

my $delta = $space->delta( \@values1, \@values2 );
$delta = $space->denormalize_range( $delta, $range);
return unless ref $delta and @$delta == $space->dimensions;

Expand Down
4 changes: 2 additions & 2 deletions t/10_rgb_space.t
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ is( $d->[2], -256, 'delta in B component');

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

exit 0;

0 comments on commit b291ba3

Please sign in to comment.