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

Add support for RGB color model in ConTeXt #1130

Merged
merged 1 commit into from
Apr 15, 2024

Conversation

gucci-on-fleek
Copy link
Contributor

@gucci-on-fleek gucci-on-fleek commented Jan 28, 2022

Motivation for this change

According to the manual, the RGB color model should be supported in ConTeXt.

\item Specify a new color using |\definecolor|. Only the color models
|gray|, |rgb|, and |RGB| are supported\footnote{Con\TeX t users
should be aware that \texttt{\textbackslash definecolor} has a
different meaning in Con\TeX t. There is a low-level equivalent
named \texttt{\textbackslash pgfutil@definecolor} which can be
used instead.}.

However, using \pgfutil@definecolor{some-color}{RGB}{127,127,127} produces an error. A few of the pgfplots libraries use the RGB color model, so they cannot currently be used with ConTeXt.

Modifications

Plain TeX supports the RGB color model by internally converting to rgb.

\def\pgfutil@emu@RGB#1#2,#3,#4\@nil{%
\begingroup
\pgfmathdivide@{#2}{255}\let\pgfutil@emu@RGB@r\pgfmathresult
\pgfmathdivide@{#3}{255}\let\pgfutil@emu@RGB@g\pgfmathresult
\pgfmathdivide@{#4}{255}\let\pgfutil@emu@RGB@b\pgfmathresult
\edef\pgf@marshal{\def\expandafter\noexpand\csname\string\color@#1\endcsname{%
\noexpand\xcolor@{}{}{rgb}{\pgfutil@emu@RGB@r,\pgfutil@emu@RGB@g,\pgfutil@emu@RGB@b}}}%
\expandafter\endgroup\pgf@marshal
}

This PR just copies that same code over to pgfutil-context.def.

Checklist

@muzimuzhi
Copy link
Member

Just for the info, the support for RGB in Plain TeX is addressed in #772.

@hmenke
Copy link
Member

hmenke commented Feb 1, 2022

Looking around in the code the entire ConTeXt pgfutil could do with some refactoring. For example the gray colormodel just maps to rgb.

@gucci-on-fleek
Copy link
Contributor Author

I've also changed it so that the gray color model maps to grayscale and not rgb. I can split that out to a separate PR if you'd prefer, but I've bundled it in with this one since it's a 1-line change in the same file.

Manually creating a grayscale color now works, but the named gray colors still map to rgb. This test file:

\usemodule[t][tikz]

\unprotect
\pgfutil@definecolor{test}{gray}{0.5}
\protect

\startTEXpage
    \tikz\draw [test] (0, 0   ) -- (36bp, 0   );
    \tikz\draw [gray] (0, 36bp) -- (36bp, 36bp);
\stopTEXpage

produces this PDF:

10 0 obj
<<
  /Length 11 0 R
>>
stream
1 0 0 1 0.19925 2.643408 cm
q 
0 0 0 RG 
0 0 0 rg 
0.3985 w 
q 
q 
0.5 G 0.5 g % The "test" color maps to grayscale
0.0 0.0 m 
36.00046 0.0 l 
S 
0 0 0 RG 0 0 0 rg 
Q 
Q 
n 
Q 
1 0 0 1 40.293738 -35.999995 cm
q 
0 0 0 RG 
0 0 0 rg 
0.3985 w 
q 
q 
0.900 0.900 0.900 RG 0.900 0.900 0.900 rg % The "gray" color maps to rgb
0.0 36.00046 m 
36.00046 36.00046 l 
S 
0 0 0 RG 0 0 0 rg 
Q 
Q 
n 
Q 
endstream
endobj

@gucci-on-fleek
Copy link
Contributor Author

I redid the commit to (hopefully) make the "check-commits" CI pass. There weren't any code changes.

@hmenke
Copy link
Member

hmenke commented Feb 13, 2022

I'd like to have a few ConTeXt tests before merging this so it is essentially also blocked by #1116 (or rather an extension thereof).

@muzimuzhi
Copy link
Member

@hmenke Does this mean #1116 will introduce test or test suite that runs with ConTeXt (by setting l3build
variable specialformats.context)?

The `RGB` support was copied from the the Plain TeX definitions,
`tex/generic/pgf/utilities/pgfutil-plain.def:29-37`, which is from pgf-tikz#772.

The `gray` color model now maps to the grayscale and not to `rgb`.

Signed-off-by: Max Chernoff <[email protected]>
@gucci-on-fleek
Copy link
Contributor Author

Would it be possible to get this PR merged? I've rebased it so it should apply cleanly.

I know that the test suite hasn't been finished yet, but I've been applying this patch to my local installation for a two years now without any issues, so this shouldn't cause any regressions. Plus, this change only affects ConTeXt, so the impact of any possible bugs should be minimal.

@hmenke hmenke merged commit 913d955 into pgf-tikz:master Apr 15, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

3 participants