# How to specify a fill color in RGB format in a node in tikzpicture?

I am just trying to get a rounded corner box with some text in it. The text color will be white and the box will be filled with a particular color.

The preamble includes:

\usepackage{tikz}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%


The code I have used is:

\begin{tikzpicture}
{\textcolor{white}{TEST}};
\end{tikzpicture}


How can I specify an arbitrary color (RGB values in a range of 0-255) in the fill option?

Also, am I using too advanced a tool (tikz) to do something simple (draw a rounded corner rectangle filled with a color and text of some other color)?

• The HTML format is quite helpful, too: \definecolor{orange}{HTML}{FF7F00} (see my blog post for examples) – Martin Thoma Apr 8 '17 at 19:28

In answer to your first question - mixing colours in (say) RGB format - you could use the notation similar to that specified in the xcolor package documentation, since TikZ recognizes this. For example, mixing/sharing proportions of black green is obtained using

\node[...,fill=black,...]
\node[...,fill=black!60!green,...]
\node[...,fill=black!30!green,...]
\node[...,fill=green,...]


and displays

Or, if you're interested in mixing certain quantities of RGB colours, you can use a part-wise mix as follows:

\node[...,fill={rgb:red,4;green,2;yellow,1},...]
\node[...,fill={rgb:red,1;green,2;blue,5},...]
\node[...,fill={rgb:orange,1;yellow,2;pink,5},...]
\node[...,fill={rgb:black,1;white,2},...]


which outputs

For answering your second question - dealing with boxes - you can use the fancybox package or even PStricks. Here is an example using the latter:

\documentclass{article}
\usepackage{pstricks}
\begin{document}
\begin{pspicture}(5,5)
\psset{fillstyle=solid,framearc=.3}
\psframebox[fillcolor=red]{\textcolor{white}{TEST}}
\psframebox[fillcolor=green!50!red]{\textcolor{white}{TEST}}
\psframebox[fillcolor=black!50]{\textcolor{white}{TEST}}
\psframebox[fillcolor={rgb:red,1;green,2;blue,3}]{\textcolor{white}{TEST}}
\end{pspicture}
\end{document}​


• Cool, I didn't know before about the rgb:... syntax! – Daniel Jul 2 '12 at 21:24
• About the notation rgb:red,4;green,2;yellow,1, what are the units? Seems not to be percentage... – kebs Dec 11 '17 at 14:24
• @kebs: I think the units are all converted to shares of the total (red + green + yellow; perhaps within some range, either [0,1] or [0,255]), since this code produces the same output for all \psframeboxs. – Werner Dec 12 '17 at 16:29

One approach is to predefine in the preamble all colors one wants to use, for instance

\definecolor{mycolor}{rgb}{1,0.2,0.3}


with values in the interval (0,1) or

\definecolor{mycolor}{RGB}{255,51,76}


with integer values in the range 0–255 (use a more sensible name than mycolor) and then mycolor can be used in all color specifications, for instance

\node[drop shadow,fill=mycolor,draw,rounded corners]


In this way you can control the colors and also change them just by modifying their definition.

• It is also possible to define the colors right before you use them. You can even redefine them later if you want to use different values for them. I did that once when I was generating a plot with a lot of different colors in it. I have created a new answer for this alternative approach. – StrawberryFieldsForever Aug 4 '12 at 0:38
• @StrawberryFieldsForever I don't think that changing the meaning of a color name is a good way to proceed. – egreg Aug 4 '12 at 11:08
• Why not? It will be like any other variable. Of course you shouldn't call the color "red" or something like that if you intend to change its value because you would expect that color to be constant, but if you call it "mycolor" or "tempcolor" I see no problem with it. – StrawberryFieldsForever Aug 4 '12 at 15:07

Werner has shown the rgb-syntax to mix colors, egreg has shown how to define a color in RGB and then use it. If you want to specify a color with RGB values [0-255] without defining it before, you can use the rgb-syntax Werner has shown by setting the optional div-parameter to 255 (see xcolor package documentation, pages 13 and 16).

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikz\node[rounded corners, fill={rgb,255:red,21; green,66; blue,128}, text=white, draw=black] {hello world};
\end{document}


Another approach, that builds on egregs answer, is to predefine the color one wants to use right before it is used. In that way one doesn't have to be so creative when coming up with the names for all colors, which is especially cumbersome if a lot of different colors have to be defined. The color can then be used in all color specifications (just like colors defined in the preamble), for instance

...
\definecolor{tempcolor}{rgb}{1,0.2,0.3}
...


with values in the interval (0,1) or

...
\definecolor{tempcolor}{RGB}{255,51,76}
...


with integer values in the range 0–255 (the name "tempcolor" is just a suggestion).

If you want to make sure you're not overriding any earlier defined color named tempcolor, you can always define the color in a separate scope:

...
{
\definecolor{tempcolor}{rgb}{1,0.2,0.3}