Skip to content

Commit

Permalink
avoid using grayscale base when mixing richer colors (#2408)
Browse files Browse the repository at this point in the history
* avoid using grayscale base when mixing richer colors

* avoid needless recursion in color mixing/adding
  • Loading branch information
dginev committed Sep 17, 2024
1 parent dfc3bc9 commit 2373c01
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions lib/LaTeXML/Common/Color.pm
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,35 @@ sub complement {

# Mix $self*$fraction + $color*(1-$fraction)
sub mix {
my ($self, $color, $fraction) = @_;
$color = $color->convert($self->model) unless $self->model eq $color->model;
my @a = $self->components;
my @b = $color->components;
return $self->new(map { $fraction * $a[$_] + (1 - $fraction) * $b[$_] } 0 .. $#a); }
my ($self, $other, $fraction) = @_;
my $base = $self;
my $base_model = $base->model;
my $mix_model = $other->model;
if ($base_model ne $mix_model) {
# mixing 'rgb|cmy' with 'gray' should yield 'rgb|cmy'.
if ($base_model eq 'gray') {
$base = $base->convert($mix_model);
} else {
$other = $other->convert($base_model);
} }
my @a = $base->components;
my @b = $other->components;
return $base->new(map { $fraction * $a[$_] + (1 - $fraction) * $b[$_] } 0 .. $#a); }

sub add {
my ($self, $color) = @_;
$color = $color->convert($self->model) unless $self->model eq $color->model;
my @a = $self->components;
my @b = $color->components;
return $self->new(map { $a[$_] + $b[$_] } 0 .. $#a); }
my ($self, $other) = @_;
my $base = $self;
my $base_model = $base->model;
my $mix_model = $other->model;
if ($base_model ne $mix_model) {
# mixing 'rgb|cmy' with 'gray' should yield 'rgb|cmy'.
if ($base_model eq 'gray') {
$base = $base->convert($mix_model);
} else {
$other = $other->convert($base_model); } }
my @a = $base->components;
my @b = $other->components;
return $base->new(map { $a[$_] + $b[$_] } 0 .. $#a); }

# The next 2 methods multiply the components of a color by some value(s)
# This assumes that such a thing makes sense in the given model, for some purpose.
Expand Down

0 comments on commit 2373c01

Please sign in to comment.