155

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,%
matrix,shapes.symbols,plotmarks,decorations.markings,shadows}

The code I have used is:

\begin{tikzpicture}
\node[drop shadow,fill=black,draw,rounded corners]
{\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)?

1
  • 3
    The HTML format is quite helpful, too: \definecolor{orange}{HTML}{FF7F00} (see my blog post for examples) Commented Apr 8, 2017 at 19:28

4 Answers 4

174

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

Nodes with different colour fills (sharing between black and green)

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

Part-wise mix of colours

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}​

PStricks rounded/coloured boxes

5
  • 9
    Cool, I didn't know before about the rgb:... syntax!
    – Daniel
    Commented Jul 2, 2012 at 21:24
  • 3
    About the notation rgb:red,4;green,2;yellow,1, what are the units? Seems not to be percentage...
    – kebs
    Commented Dec 11, 2017 at 14:24
  • 1
    @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
    Commented Dec 12, 2017 at 16:29
  • 4
    Hmmmm... suspicious. {rgb:red,1;green,1:blue,1} seems to be some kind of subtractive mixing since I get an ugly brown and not white.
    – Jason S
    Commented Sep 28, 2020 at 20:59
  • 1
    @JasonS Well, each of the three values is divided by the sum of all values, so you get .333,.333,.333 in "standard" notation which is some darker gray. If you specify the divisor explicitly using {rgb,1:red,1;green,1:blue,1} you would get white. Commented Aug 11, 2022 at 11:48
144

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.

4
  • 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. Commented Aug 4, 2012 at 0:38
  • @StrawberryFieldsForever I don't think that changing the meaning of a color name is a good way to proceed.
    – egreg
    Commented Aug 4, 2012 at 11:08
  • 1
    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. Commented Aug 4, 2012 at 15:07
  • 2
    Notably this requires you to \usepackage{xcolor}
    – Migwell
    Commented May 27, 2021 at 14:27
37

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}
21

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}
\node[drop shadow,fill=tempcolor,draw,rounded corners]
...

with values in the interval (0,1) or

...
\definecolor{tempcolor}{RGB}{255,51,76}
\node[drop shadow,fill=tempcolor,draw,rounded corners]
...

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}
    \node[drop shadow,fill=tempcolor,draw,rounded corners]
}
...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .