Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved \color parsing and scaling for custom RGB directives #2223

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/LaTeXML/Common/Color.pm
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ sub multiply {

__END__

=pod
=pod

=head1 NAME

Expand Down Expand Up @@ -251,4 +251,3 @@ Public domain software, produced as part of work done by the
United States Government & not subject to copyright in the US.

=cut

12 changes: 7 additions & 5 deletions lib/LaTeXML/Common/Color/rgb.pm
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ sub hsb {
elsif ($i == 6) { return Phi($r, $g, $b, 1, -1); }
elsif ($i == 7) { return LaTeXML::Common::Color->new('hsb', 0, 0, $b); } }

my @hex = qw(0 1 2 3 4 5 6 7 8 9 A B C D E F); # [CONSTANT]

sub hex2 {
my ($n) = @_;
my $nn = LaTeXML::Common::Number::roundto($n * 255, 0);
return $hex[int($nn / 16)] . $hex[$nn % 16]; }
my ($n) = @_;
my $nn = LaTeXML::Common::Number::roundto($n * 255, 0);
my $h_full = sprintf("%.2X", $nn);
# the precision doesn't quite get enforced on larger values,
# so let's try to substring explicitly
my $h2 = substr($h_full, 0, 2);
return $h2; }

sub toHex {
my ($self) = @_;
Expand Down
10 changes: 7 additions & 3 deletions lib/LaTeXML/Package/xcolor.sty.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ sub DecodeColor {
my $dec_re = qr/[+-]*(?:\d*\.?\d*)/;
# <ext_expr> : <core_model>,<div>:<expr1><dec1>;...;<exprk><deck>
# | <core_model>:<expr1><dec1>;...;<exprk><deck>
my $ext_expr_re = qr/($core_model_re)(,($div_re))?:\s*
(($expr_re|$name_re),$dec_re(?:;\s*(?:$expr_re|$name_re),$dec_re)*)/x;
my $ext_expr_re = qr/($core_model_re)(,\s*($div_re))?:\s*
(($expr_re|$name_re),\s*$dec_re(?:;\s*(?:$expr_re|$name_re),\s*$dec_re)*)/x;
# <color_expr> : <name> | <expr> | <ext_expt>
my $color_expr_re = qr/$expr_re|$ext_expr_re/;
my $function_re = qr/wheel|twheel/;
Expand All @@ -305,13 +305,17 @@ sub DecodeColor {
if (defined $core_model) { # Extended color expression: combine colors as on a pallete
$color = Black->convert($core_model);
my $dectot = 0;
while ($exprs =~ s/($expr_re),($dec_re)//) {
while ($exprs =~ s/($expr_re),\s*($dec_re)//) {
my $dec = $6; $dec =~ s/--//g;
next if !$dec || $dec eq '.'; # the contribution is 0!
$dectot += $dec;
push(@pallete, [DecodeColor($1), $dec]); }
$div = $dectot unless $div;
foreach my $cp (@pallete) {
# IMPORTANT: if we need to scale, first convert to RGB,
# or we risk losing scaling precision on any zero-valued components.
$$cp[0] = $$cp[0]->convert($color->model) unless $color->model eq $$cp[0]->model;
# now it is safe to scale:
$color = $color->add($$cp[0]->scale($$cp[1] / $div)); } }
else { # Standard Color Expression: <prefix><name><mix_expr><postfix>
$color = ($postfix && ($postfix =~ /!!\[(\d+)\]/) # Note "out-of-order" effect!
Expand Down
Binary file modified t/graphics/xcolors.pdf
Binary file not shown.
18 changes: 18 additions & 0 deletions t/graphics/xcolors.tex
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,22 @@ \section{Boxes}
\copy\mybox
\setbox\mybox=\hbox{Should be Red!}
{\color{red}\copy\mybox}

\section{Custom rgb directives}
\color{rgb:red,4;green,2;yellow,1} Brown

\color{rgb:red,1;green,2;blue,5} Dark blue

\color{rgb:orange,1;yellow,2;pink,5} Light orange

\color{rgb:black,1;white,2} Gray

\color{rgb, 255:red, 208; green, 2; blue, 27} Brick-like Red

\color{rgb, 255:red, 74; green, 144; blue, 226} Ocean blue

\color{rgb, 255:red, 0; green, 116; blue, 201} Sky blue

\color{rgb, 255:red, 0; green, 0; blue, 0} Black

\end{document}
32 changes: 32 additions & 0 deletions t/graphics/xcolors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,36 @@ A=100*13 = 1300pt;</p>
<text color="#FF0000">Should be Red!</text></p>
</para>
</section>
<section inlist="toc" xml:id="S12">
<tags>
<tag>12</tag>
<tag role="refnum">12</tag>
<tag role="typerefnum">§12</tag>
</tags>
<title><tag close=" ">12</tag>Custom rgb directives</title>
<para xml:id="S12.p1">
<p><text color="#B66D00">Brown</text></p>
</para>
<para xml:id="S12.p2">
<p><text color="#20409F">Dark blue</text></p>
</para>
<para xml:id="S12.p3">
<p><text color="#FFC778">Light orange</text></p>
</para>
<para xml:id="S12.p4">
<p><text color="#AAAAAA">Gray</text></p>
</para>
<para xml:id="S12.p5">
<p><text color="#D0021B">Brick-like Red</text></p>
</para>
<para xml:id="S12.p6">
<p><text color="#4A90E2">Ocean blue</text></p>
</para>
<para xml:id="S12.p7">
<p><text color="#0074C9">Sky blue</text></p>
</para>
<para xml:id="S12.p8">
<p><text color="#000000">Black</text></p>
</para>
</section>
</document>
Loading