From ccf72d5cf400fcdb942d9cd07ede3a2688e2c5aa Mon Sep 17 00:00:00 2001 From: Deyan Ginev Date: Mon, 16 Sep 2024 22:32:55 -0400 Subject: [PATCH] avoid using grayscale base when mixing richer colors (#2408) * avoid using grayscale base when mixing richer colors * avoid needless recursion in color mixing/adding --- lib/LaTeXML/Common/Color.pm | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/LaTeXML/Common/Color.pm b/lib/LaTeXML/Common/Color.pm index 623101fd3..f0a234fb2 100644 --- a/lib/LaTeXML/Common/Color.pm +++ b/lib/LaTeXML/Common/Color.pm @@ -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.