# How to define a CMYK color using values between 0 and 255

I would like to define my own CMYK colors in LaTeX. I know that it can be done with the

\definecolor{my_color_name}{cmyk}{c,y,m,k}


command, but here, values between 0and 1 are used. The problem is that I have a color map available that gives me all colors in CMYK with values from 0 to 255 and I don't want to divide all values by 255 manually. For defining RGB-colors, LaTeX provides the commands

\definecolor{my_color_name}{rgb}{r,g,b}
\definecolor{my_color_name}{RGB}{R,G,B}


where {r,g,b} between 0 and 1 and {R,G,B} between 0 and 255. Therefore, I am looking for something similar for the CMYK scheme. Any ideas?

I don't know how you receive your data, but this may give you an idea of how to proceed:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}

\newcommand{\hexcmyk}[5]{%
\pgfmathsetmacro{\myc}{#1/255}%
\pgfmathsetmacro{\mym}{#2/255}%
\pgfmathsetmacro{\myy}{#3/255}%
\pgfmathsetmacro{\myk}{#4/255}%
\definecolor{testcolor}{cmyk}{\myc,\mym,\myy,\myk}%
\textcolor{testcolor}{#5}% Trivial application
}

\def\hhexcmyk(#1,#2,#3,#4)#5{%
\pgfmathsetmacro{\myc}{#1/255}%
\pgfmathsetmacro{\mym}{#2/255}%
\pgfmathsetmacro{\myy}{#3/255}%
\pgfmathsetmacro{\myk}{#4/255}%
\definecolor{testcolor}{cmyk}{\myc,\mym,\myy,\myk}%
\textcolor{testcolor}{#5}% Trivial application
}

\def\defineCMYKcolor(#1,#2,#3,#4)#5{%
\pgfmathsetmacro{\myc}{#1/255}%
\pgfmathsetmacro{\mym}{#2/255}%
\pgfmathsetmacro{\myy}{#3/255}%
\pgfmathsetmacro{\myk}{#4/255}%
\definecolor{#5}{cmyk}{\myc,\mym,\myy,\myk}%
}

\defineCMYKcolor(155,0,255,10){mygreen}

\begin{document}

\hexcmyk{0}{255}{255}{0}{Test text.}

\hhexcmyk(255,0,255,0){TEST}

\defineCMYKcolor(0,255,155,10){myred}

\textcolor{myred}{This is some more text.}

\textcolor{mygreen}{This yet more text.}

\end{document}


Produces this:

It occurred to me that I don't know how the data are presented so expanded my answer to give another solution to the problem.

• Your answer is very close to what I wanted. Let's say I would like to write in my document: \begin{document}\defineCMYKcolor{mygreen}{255,0,255,0}\color{mygreen}TEST\end{document} to keep it very close to the \definecolor command. For that, your \hhexcmyk already looks good but would need the textcolor line to be crossed out and the testcolor replaced by an argument that defines the color. Also, I think the package xcolor is not needed right? – tc88 Mar 25 '15 at 8:33
• Sorry, the \defineCMYKcolor{mygreen}{255,0,255,0} should be either in the preamble OR in the document. \definecolor also works in both. – tc88 Mar 25 '15 at 8:46
• I have altered my answer accordingly. NOTE that the argument to \defineCMYKcolor uses parentheses and not braces: \defineCMYKcolor(100,200,50,10){mycolor} – sgmoye Mar 25 '15 at 9:57

With only the need of package xcolor:

\usepackage{xcolor}

{\lccode\?=\p \lccode\!=\t  \lowercase{\gdef\ignorept#1?!{#1}}}
\def\divbyccvv#1{\expandafter\ignorept\the\dimexpr#1pt/255\relax}

\def\defineCMYKcolor#1#2{\defineCMYKcolorA{#1}#2,}
\def\defineCMYKcolorA#1#2,#3,#4,#5,{\edef\tmp{\noexpand\definecolor{#1}{cmyk}%
{\divbyccvv{#2},\divbyccvv{#3},\divbyccvv{#4},\divbyccvv{#5}}}\tmp
}

\defineCMYKcolor{my_color_name}{255,0,50,11}

\begin{document}
\color{my_color_name}test in my_color
\end{document}


Use the facilities of xparse and expl3:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\defineCMYKcolor}{mm}
{
\tceighteight_define_CMYK_color:nn { #1 } { #2 }
}

\seq_new:N \l_tceighteight_input_seq
\seq_new:N \l_tceighteight_output_seq

\cs_new_protected:Npn \tceighteight_define_CMYK_color:nn #1 #2
{
% split the input into parts
\seq_set_split:Nnn \l_tceighteight_input_seq { , } { #2 }
% convert each item to a decimal value in [0,1]
\seq_set_map:NNn \l_tceighteight_output_seq  \l_tceighteight_input_seq
{
\fp_eval:n { ##1/255 }
}
% now define the color
\definecolor{#1}{cmyk}{ \seq_use:Nn \l_tceighteight_output_seq { , } }
}
\ExplSyntaxOff

\defineCMYKcolor{test}{32,100,211,99}

\begin{document}

\textcolor{test}{ABCDEF}

\end{document}


The four numbers are stored in a sequence (one might add here some sanity checks) and then transformed into another sequence that is used in the appropriate \definecolor command.

If I ask what the color test expands to, I get

> \\color@test=macro:
->\xcolor@ {}{0.12549 0.39215 0.82745 0.38823 k 0.12549 0.39215 0.82745 0.38823
K}{cmyk}{0.12549,0.39215,0.82745,0.38823}.


and, for checking the values, I multiplied them back by 255, getting

0.12549*255 = 31.99995
0.39215*255 = 99.99825
0.82745*255 = 210.99975
0.38823*255 = 98.99865


which seems good (some small error is expected anyway).