Mojca Miklavec wrote:
On 2/4/07, Hans Hagen
wrote: Mojca Miklavec wrote:
On 2/4/07, Hans Hagen
wrote: Peter Rolf wrote:
But to my surprise ConTeXt automatically converts my RGB colors (even the MP defined ones), except that the final colors are in the CMY instead of CMYK color space. All I want is a true RGBtoCMYK conversion. I know about the calculating limitations in TeX, so I can wait for an implemetation of that macro in LuaTeX.
but even then ... we need a formula ...
Most pages on the internet list the following simple conversion formula:
C' = 1 - (R/range) C = (C' - K') / (1 - K') M' = 1 - (G/range) M = (M' - K') / (1 - K') Y' = 1 - (B/range) Y = (Y' - K') / (1 - K') K' = MIN(C',Y',M') K = K'
In the special case of K'=1, use (0,0,0,1)
(I'm not a TeX guru, but I assume that conversion with current macros should not be much more difficult than conversion with luaTeX.)
this is what we have now
\def\doconvertRGBtoCMYK#1\to#2% {\colordimen#1\points \multiply\colordimen \plusthousand \colorcount\colordimen \advance\colorcount \medcard \divide\colorcount \maxcard \colorcount-\colorcount \advance\colorcount \plusthousand \edef#2{\realcolorvalue\colorcount}}
\def\convertRGBtoCMYK#1#2#3% {\doconvertRGBtoCMYK#1\to\@@cl@@c \doconvertRGBtoCMYK#2\to\@@cl@@m \doconvertRGBtoCMYK#3\to\@@cl@@y \let\@@cl@@k\@@cl@@z}
well, you can spent the rest of the evening writing an alternative
Since writing reports can sometimes be really boring, you can test if the following works, but please don't ask me what it does ;)
I doubt that writing such macros is a better activity ;) Thanks to you Mojca for this nice and clean structured solution. The conversion works ok, but the accuracy is not good enough. I have translated some parts into etex (better accuracy in divisions using dim|numexpr), but it's still lacks of accuracy. I will try to use a special macro (blow up numerator) for all divisions.
I copy-pasted the code (which should be better written with e-TeX, I suppose, or well ... lua is not that bad after all ;).
:)) Best, Peter \beginETEX \def\doconvertRGBtoCMY#1\to#2% {\colordimen#1\points \multiply\colordimen \plusthousand \colorcount\numexpr-\numexpr\colordimen+\medcard\relax/\maxcard+\plusthousand\relax \edef#2{\realcolorvalue\colorcount}} \def\doconvertCMYtoCMYK#1\k#2\to#3% {\colorcount\dimexpr#1\thousandpoint\relax \colordimen#2\thousandpoint \ifdim\colordimen<\thousandpoint \advance\colorcount-\colordimen \colordimen\dimexpr-\colordimen/\plusthousand+\onepoint\relax \colorcount\numexpr\colorcount/\colordimen\relax \edef#3{\realcolorvalue\colorcount}% \else \let#3\@@cl@@z \fi} \def\convertCMYtoCMYK#1#2#3% {\edef\@@cl@@c{#1}\edef\@@cl@@m{#2}\edef\@@cl@@y{#3}% \colordimen\onepoint \ifdim\colordimen>\@@cl@@c\points \colordimen\@@cl@@c\points\fi \ifdim\colordimen>\@@cl@@m\points \colordimen\@@cl@@m\points\fi \ifdim\colordimen>\@@cl@@y\points \colordimen\@@cl@@y\points\fi \multiply\colordimen \plusthousand \colorcount\numexpr\numexpr\colordimen+\medcard\relax/\maxcard\relax \edef\@@cl@@k{\realcolorvalue\colorcount}% \doconvertCMYtoCMYK#1\k\@@cl@@k\to\@@cl@@c \doconvertCMYtoCMYK#2\k\@@cl@@k\to\@@cl@@m \doconvertCMYtoCMYK#3\k\@@cl@@k\to\@@cl@@y } \endETEX
% RGB -> CMYK % c' = 1 - r % m' = 1 - g % y' = 1 - b % k' = MIN(c',y',m')
% c = (c' - k') / (1 - k') % m = (m' - k') / (1 - k') % y = (y' - k') / (1 - k') % k = k'
% CMYK -> CMY % c = (c * (1 - k) + k) % m = (m * (1 - k) + k) % y = (y * (1 - k) + k)
% CMY -> CMYK % k' = min(c,m,y) % if (k==1) % (0,0,0,1) % else % c = (c' - k') / (1 - k') % m = (m' - k') / (1 - k') % y = (y' - k') / (1 - k') % k = k'
\unprotect
\def\doconvertRGBtoCMY#1\to#2% {\colordimen#1\points \multiply\colordimen \plusthousand \colorcount\colordimen \advance\colorcount \medcard \divide\colorcount \maxcard \colorcount-\colorcount \advance\colorcount \plusthousand \edef#2{\realcolorvalue\colorcount}}
\def\doconvertCMYtoCMYK#1\k#2\to#3%% {\colordimen#1\thousandpoint % \colorcount = 1000c \colorcount\colordimen % check if k<1 \colordimen#2\thousandpoint \ifdim\colordimen<\thousandpoint % #3 = (#1-#2)/(1-#2) \advance\colorcount-\colordimen % \colorcount = 1000(c-k) \colordimen-\colordimen % \colordimen = -1000k \divide\colordimen \plusthousand % \colordimen = -k \advance\colordimen \onepoint % \colordimen = 1-k \divide\colorcount \colordimen % \colorcount = 1000(c-k)/(1-k) % TODO: rounding error! \edef#3{\realcolorvalue\colorcount}% \else % if k==1, the component is zero \let#3\@@cl@@z% \fi}
\def\convertRGBtoCMY#1#2#3% {\doconvertRGBtoCMY#1\to\@@cl@@c \doconvertRGBtoCMY#2\to\@@cl@@m \doconvertRGBtoCMY#3\to\@@cl@@y \let\@@cl@@k\@@cl@@z}
\def\convertRGBtoCMYK#1#2#3% {\edef\@@cl@@r{#1}\edef\@@cl@@g{#2}\edef\@@cl@@b{#3}% \convertRGBtoCMY\@@cl@@r\@@cl@@g\@@cl@@b% \convertCMYtoCMYK\@@cl@@c\@@cl@@m\@@cl@@y}
\def\convertCMYtoCMYK#1#2#3% {\edef\@@cl@@c{#1}\edef\@@cl@@m{#2}\edef\@@cl@@y{#3}% % calculate k = min(c,m,y) % k = 1 \colordimen\onepoint \colorcount\colordimen % if (k>c): k=c \ifdim\colordimen>#1\points% \colordimen#1\points \fi % if (k>m): k=m \ifdim\colordimen>#2\points% \colordimen#2\points \fi % if (k>y): k=y \ifdim\colordimen>#3\points% \colordimen#3\points \fi \multiply\colordimen \plusthousand \colorcount\colordimen \advance\colorcount \medcard \divide\colorcount \maxcard % z = min(c,m,y) \edef\@@cl@@k{\realcolorvalue\colorcount}% % \doconvertCMYtoCMYK#1\k\@@cl@@k\to\@@cl@@c \doconvertCMYtoCMYK#2\k\@@cl@@k\to\@@cl@@m \doconvertCMYtoCMYK#3\k\@@cl@@k\to\@@cl@@y }
\protect
\setupcolors[state=start, cmyk=yes, mpcmyk=yes, rgb=no, ]
\pdfcompresslevel=0
\starttext
\startMPcode fill unitsquare scaled 3cm withcolor (223/255,223/255,227/255); % CMY(0.12548, 0.12547, 0.10980) % CMYK(0.01762, 0.01760, 0, 0.10980) % results in (0.016, 0.016, 0, 0.110) \stopMPcode
\stoptext
Mojca _______________________________________________ ntg-context mailing list ntg-context@ntg.nl http://www.ntg.nl/mailman/listinfo/ntg-context