# Color merge from RGB specification

With the xcolor package one can define colors in many ways for example, using standard names,

\colorbox{red}{...}

Merging colors

\colorbox{red!25}{...}

\colorbox{red!25!blue}{...}

Or from the RGB or CYMK values

\colorbox[rgb]{0.1,0.2,0.3}{...}

Is it possible to combine inline the RGB and merging approaches? For example

\colorbox[rgb]{0.1,0.2,0.3 ! 25}{...} %invalid code

\colorbox{\color[rgb]{0.70,0.05,0.65}!25}{...} %also invalid

To make a given RGB color lighter. The context is that for a given element I know the RGB value but I want to make it lighter by merging with white.

MWE:

\documentclass[]{article}
\usepackage[]{xcolor}
\begin{document}
\colorbox[rgb]{0.70,0.05,0.65}{A} % this turns to be dark violet, how to get something like {violet!25}
\end{document}


Sure you can do it. The xcolor package offers a very sofisticated extended syntax for mixing colors (refer to pages 14-16 of the xcolor documentation for details):

\documentclass[]{article}
\usepackage[]{xcolor}
\begin{document}
\colorbox{rgb:red,0.70;green,0.05;blue,0.65}{A} % this turns to be dark violet, how to

\colorbox{rgb:red!40,0.70;green!40,0.05;blue!40,0.65}{A}

\colorbox{rgb:red!10,0.70;green!10,0.05;blue!10,0.65}{A}
\end{document}


\documentclass{article}
\usepackage[]{xcolor}
\usepackage{pgffor}

\begin{document}
\noindent
\colorbox{rgb:red,0.70;green,0.05;blue,0.65}{A}\quad % this turns to be dark violet, how to
\foreach \Valor in {90,80,...,10}
{%
}
\end{document}


In fact, you can produce many variations using this method and, more complexes mixes or, for example, the complement operator -. A little example showing "in-line" variations of your initial color:

\documentclass[]{article}
\usepackage[]{xcolor}
\begin{document}
\colorbox{rgb:red,0.70;green,0.05;blue,0.65}{A} % this turns to be dark violet, how to

\colorbox{rgb:red!40,0.70;green!40,0.05;blue!40,0.65}{A}

\colorbox{rgb:red!10,0.70;green!10,0.05;blue!10,0.65}{A}

\colorbox{rgb:red!10!cyan,0.70;green!60!cyan,0.05;blue!10,0.65}{A}

\colorbox{rgb:-red!10!cyan,0.70;green!60!cyan,0.05;blue!10,0.65}{A}

\colorbox{rgb:red!10!cyan,0.70;-green!60!cyan,0.05;blue!10!orange,0.65}{A}

\colorbox{rgb:-red!10!cyan,0.70;-green!60!cyan,0.05;blue!10!orange,0.65}{A}
\end{document}


• This answers the question perfectly. Do you know what is the logic behind the rgb: if after that one can put any color names? – alfC Sep 2 '15 at 0:13
• @alfC rgb selects the color model; the color represented by red for example depends on the chosen model. Compare these two (the only difference is the model): \colorbox{rgb:red,0.70;green,0.05;blue,0.65}{A} \colorbox{cmyk:red,0.70;green,0.05;blue,0.65}{A} – Gonzalo Medina Sep 2 '15 at 0:36

Typically this is done by defining the colour first, after which you can shade (or merge it):

\documentclass{article}
\usepackage{xcolor}
\definecolor{violet}{rgb}{0.70,0.05,0.65}
\begin{document}
\colorbox[rgb]{0.70,0.05,0.65}{A} % this turns to be dark violet, how to get something like {violet!25}

\colorbox{violet}{A}

\colorbox{violet!25}{A}
\end{document}

• It is most likely possible to define colours based on what you supply. However, then one would have to compete with the syntax of merged/mixed colours: colour1!<num>!colour2!<num>!colour3!<num>!colour4!... – Werner Sep 1 '15 at 22:14
• So, no chance to have it inline? I have to color many elements with unique colors and it seems a waste to define a color that will be used only once. – alfC Sep 1 '15 at 22:20
• I never was able to "merge" more than two colors, for example red!70!green!5!blue!65 produces a mostly pure blue. Otherwise you are right, I could add something at the end, like !white. – alfC Sep 1 '15 at 22:25
• @alfC: You can use \definecolor even on an existing colour. So, you don't have to create a new colour each time. Create the colours for the display and re-use the name. – Werner Sep 1 '15 at 22:35

You could define a wrapper command e.g. \rgbbox[<optional proportion>]{<rgb specification>}{<contents>}. For example:

\documentclass{article}
\usepackage{xcolor}
\newcommand\rgbbox[3][100]{%
\definecolor{rgbboxcolour}{rgb}{#2}%
\colorbox{rgbboxcolour!#1}{#3}%
}
\begin{document}
\colorbox[rgb]{0.70,0.05,0.65}{A} % this turns to be dark violet, how to get something like {violet!25}

\rgbbox[25]{0.70,0.05,0.65}{B}

\rgbbox[50]{.25,.75,.75}{C}

\rgbbox{.25,.75,.75}{D}
\end{document}


Note that if the optional argument is not specified, you get no mixing i.e. 100% of the colour given in the first mandatory argument.