Colors and Styles in TikZ
Master color usage, styling options, and creating reusable styles in TikZ.
Basic Colors
TikZ uses the xcolor package, which provides many predefined colors:
| Color Name | Description |
|---|---|
red | Pure red |
green | Pure green |
blue | Pure blue |
cyan | Cyan |
magenta | Magenta |
yellow | Yellow |
black | Black |
white | White |
gray | 50% gray |
orange | Orange |
purple | Purple |
brown | Brown |
\begin{tikzpicture}
\draw[red] (0,0) -- (2,0);
\draw[blue, thick] (0,-0.5) -- (2,-0.5);
\draw[green, very thick] (0,-1) -- (2,-1);
\fill[orange] (3,0) circle (0.3cm);
\fill[purple] (3,-0.5) rectangle (3.6,-1);
\end{tikzpicture}Color Mixing and Shades
Create lighter or darker shades using the ! operator:
\begin{tikzpicture}
% Lighter shades (mix with white)
\fill[red!100] (0,0) rectangle (0.8,0.8); % 100% red
\fill[red!75] (1,0) rectangle (1.8,0.8); % 75% red
\fill[red!50] (2,0) rectangle (2.8,0.8); % 50% red
\fill[red!25] (3,0) rectangle (3.8,0.8); % 25% red
% Darker shades (mix with black)
\fill[blue!100!black] (0,-1) rectangle (0.8,-0.2);
\fill[blue!75!black] (1,-1) rectangle (1.8,-0.2);
\fill[blue!50!black] (2,-1) rectangle (2.8,-0.2);
% Mix two colors
\fill[red!50!blue] (0,-2) rectangle (0.8,-1.2); % 50% red, 50% blue
\fill[green!70!yellow] (1,-2) rectangle (1.8,-1.2); % 70% green, 30% yellow
\end{tikzpicture}Defining Custom Colors
Define your own colors in the preamble:
% In preamble
\usepackage{xcolor}
% Define colors using different color models
\definecolor{myblue}{RGB}{30, 100, 200} % RGB values 0-255
\definecolor{mygreen}{rgb}{0.2, 0.7, 0.3} % rgb values 0-1
\definecolor{mygray}{gray}{0.6} % grayscale
\definecolor{mycolor}{HTML}{FF6B35} % HTML hex code
% Use in TikZ
\begin{tikzpicture}
\fill[myblue] (0,0) circle (0.5cm);
\fill[mygreen] (1.5,0) circle (0.5cm);
\fill[mycolor] (3,0) circle (0.5cm);
\end{tikzpicture}Fill and Stroke
Control both the fill color and border (stroke) color:
\begin{tikzpicture}
% Just stroke (border)
\draw[blue] (0,0) circle (0.5cm);
% Just fill (no border)
\fill[red] (1.5,0) circle (0.5cm);
% Both fill and stroke
\draw[fill=yellow] (3,0) circle (0.5cm);
% Different fill and stroke colors
\draw[blue, fill=yellow] (4.5,0) circle (0.5cm);
% Using filldraw
\filldraw[draw=green, fill=green!20] (6,0) circle (0.5cm);
\end{tikzpicture}Opacity and Transparency
Make shapes transparent using opacity:
\begin{tikzpicture}
% Background shape
\fill[blue] (0,0) rectangle (3,2);
% Overlapping transparent circles
\fill[red, opacity=0.7] (1,1) circle (0.8cm);
\fill[green, opacity=0.5] (2,1) circle (0.8cm);
% Separate fill and draw opacity
\draw[thick, draw opacity=1, fill=yellow, fill opacity=0.3]
(5,1) circle (0.8cm);
\end{tikzpicture}Gradients
Create gradient fills (requires no additional libraries for basic gradients):
\begin{tikzpicture}
% Left to right gradient
\shade[left color=red, right color=blue] (0,0) rectangle (2,1);
% Top to bottom gradient
\shade[top color=green, bottom color=white] (3,0) rectangle (5,1);
% Radial gradient (inner to outer)
\shade[inner color=yellow, outer color=orange] (6.5,0.5) circle (0.7cm);
% Ball shading (3D effect)
\shade[ball color=blue] (8.5,0.5) circle (0.5cm);
\end{tikzpicture}Patterns
Fill shapes with patterns (requires patterns library):
\usetikzlibrary{patterns}
\begin{tikzpicture}
% Horizontal lines
\draw[pattern=horizontal lines] (0,0) rectangle (1.5,1);
% Vertical lines
\draw[pattern=vertical lines] (2,0) rectangle (3.5,1);
% North east lines (diagonal)
\draw[pattern=north east lines] (4,0) rectangle (5.5,1);
% Crosshatch
\draw[pattern=crosshatch] (6,0) rectangle (7.5,1);
% Dots
\draw[pattern=dots] (8,0) rectangle (9.5,1);
% Colored pattern
\draw[pattern=north west lines, pattern color=blue] (0,-1.5) rectangle (1.5,-0.5);
\end{tikzpicture}Creating Reusable Styles
Define styles once and reuse them throughout your document:
% Global styles (in preamble or before tikzpicture)
\tikzset{
mybox/.style={
draw=blue,
fill=blue!20,
thick,
rounded corners=3pt,
minimum width=2cm,
minimum height=1cm
},
myarrow/.style={
->,
thick,
red
},
highlight/.style={
fill=yellow!50,
draw=orange,
very thick
}
}
\begin{tikzpicture}
% Use defined styles
\node[mybox] at (0,0) {Box 1};
\node[mybox] at (3,0) {Box 2};
\draw[myarrow] (1.2,0) -- (1.8,0);
% Override parts of a style
\node[mybox, fill=green!20] at (6,0) {Green Box};
\end{tikzpicture}Style Inheritance
Build complex styles by extending simpler ones:
\tikzset{
base/.style={
draw,
thick,
minimum width=1.5cm,
minimum height=0.8cm
},
process/.style={
base,
fill=blue!20,
rounded corners
},
decision/.style={
base,
fill=yellow!30,
diamond,
aspect=2
},
terminal/.style={
base,
fill=green!20,
rounded corners=10pt
}
}
\begin{tikzpicture}
\node[terminal] at (0,0) {Start};
\node[process] at (0,-1.5) {Process};
\node[decision] at (0,-3) {Test?};
\end{tikzpicture}Scope-Based Styles
Apply styles to a group of elements using scope:
\begin{tikzpicture}
% Default styling
\draw (0,0) -- (1,0);
% Scope with specific styles
\begin{scope}[thick, blue, ->]
\draw (0,-0.5) -- (1,-0.5);
\draw (0,-1) -- (1,-1);
\draw (0,-1.5) -- (1,-1.5);
\end{scope}
% Another scope
\begin{scope}[xshift=2cm, red, dashed]
\draw (0,0) rectangle (1,-1);
\draw (0.5,0) -- (0.5,-1);
\end{scope}
\end{tikzpicture}Next Steps
Continue learning TikZ:
- Coordinates - Positioning elements precisely
- Nodes and Labels - Adding styled text
- Flowcharts - Apply styles to diagrams
Colors and Styles in TikZ
Master color usage, styling options, and creating reusable styles in TikZ.
Basic Colors
TikZ uses the xcolor package, which provides many predefined colors:
| Color Name | Description |
|---|---|
red | Pure red |
green | Pure green |
blue | Pure blue |
cyan | Cyan |
magenta | Magenta |
yellow | Yellow |
black | Black |
white | White |
gray | 50% gray |
orange | Orange |
purple | Purple |
brown | Brown |
\begin{tikzpicture}
\draw[red] (0,0) -- (2,0);
\draw[blue, thick] (0,-0.5) -- (2,-0.5);
\draw[green, very thick] (0,-1) -- (2,-1);
\fill[orange] (3,0) circle (0.3cm);
\fill[purple] (3,-0.5) rectangle (3.6,-1);
\end{tikzpicture}Color Mixing and Shades
Create lighter or darker shades using the ! operator:
\begin{tikzpicture}
% Lighter shades (mix with white)
\fill[red!100] (0,0) rectangle (0.8,0.8); % 100% red
\fill[red!75] (1,0) rectangle (1.8,0.8); % 75% red
\fill[red!50] (2,0) rectangle (2.8,0.8); % 50% red
\fill[red!25] (3,0) rectangle (3.8,0.8); % 25% red
% Darker shades (mix with black)
\fill[blue!100!black] (0,-1) rectangle (0.8,-0.2);
\fill[blue!75!black] (1,-1) rectangle (1.8,-0.2);
\fill[blue!50!black] (2,-1) rectangle (2.8,-0.2);
% Mix two colors
\fill[red!50!blue] (0,-2) rectangle (0.8,-1.2); % 50% red, 50% blue
\fill[green!70!yellow] (1,-2) rectangle (1.8,-1.2); % 70% green, 30% yellow
\end{tikzpicture}Defining Custom Colors
Define your own colors in the preamble:
% In preamble
\usepackage{xcolor}
% Define colors using different color models
\definecolor{myblue}{RGB}{30, 100, 200} % RGB values 0-255
\definecolor{mygreen}{rgb}{0.2, 0.7, 0.3} % rgb values 0-1
\definecolor{mygray}{gray}{0.6} % grayscale
\definecolor{mycolor}{HTML}{FF6B35} % HTML hex code
% Use in TikZ
\begin{tikzpicture}
\fill[myblue] (0,0) circle (0.5cm);
\fill[mygreen] (1.5,0) circle (0.5cm);
\fill[mycolor] (3,0) circle (0.5cm);
\end{tikzpicture}Fill and Stroke
Control both the fill color and border (stroke) color:
\begin{tikzpicture}
% Just stroke (border)
\draw[blue] (0,0) circle (0.5cm);
% Just fill (no border)
\fill[red] (1.5,0) circle (0.5cm);
% Both fill and stroke
\draw[fill=yellow] (3,0) circle (0.5cm);
% Different fill and stroke colors
\draw[blue, fill=yellow] (4.5,0) circle (0.5cm);
% Using filldraw
\filldraw[draw=green, fill=green!20] (6,0) circle (0.5cm);
\end{tikzpicture}Opacity and Transparency
Make shapes transparent using opacity:
\begin{tikzpicture}
% Background shape
\fill[blue] (0,0) rectangle (3,2);
% Overlapping transparent circles
\fill[red, opacity=0.7] (1,1) circle (0.8cm);
\fill[green, opacity=0.5] (2,1) circle (0.8cm);
% Separate fill and draw opacity
\draw[thick, draw opacity=1, fill=yellow, fill opacity=0.3]
(5,1) circle (0.8cm);
\end{tikzpicture}Gradients
Create gradient fills (requires no additional libraries for basic gradients):
\begin{tikzpicture}
% Left to right gradient
\shade[left color=red, right color=blue] (0,0) rectangle (2,1);
% Top to bottom gradient
\shade[top color=green, bottom color=white] (3,0) rectangle (5,1);
% Radial gradient (inner to outer)
\shade[inner color=yellow, outer color=orange] (6.5,0.5) circle (0.7cm);
% Ball shading (3D effect)
\shade[ball color=blue] (8.5,0.5) circle (0.5cm);
\end{tikzpicture}Patterns
Fill shapes with patterns (requires patterns library):
\usetikzlibrary{patterns}
\begin{tikzpicture}
% Horizontal lines
\draw[pattern=horizontal lines] (0,0) rectangle (1.5,1);
% Vertical lines
\draw[pattern=vertical lines] (2,0) rectangle (3.5,1);
% North east lines (diagonal)
\draw[pattern=north east lines] (4,0) rectangle (5.5,1);
% Crosshatch
\draw[pattern=crosshatch] (6,0) rectangle (7.5,1);
% Dots
\draw[pattern=dots] (8,0) rectangle (9.5,1);
% Colored pattern
\draw[pattern=north west lines, pattern color=blue] (0,-1.5) rectangle (1.5,-0.5);
\end{tikzpicture}Creating Reusable Styles
Define styles once and reuse them throughout your document:
% Global styles (in preamble or before tikzpicture)
\tikzset{
mybox/.style={
draw=blue,
fill=blue!20,
thick,
rounded corners=3pt,
minimum width=2cm,
minimum height=1cm
},
myarrow/.style={
->,
thick,
red
},
highlight/.style={
fill=yellow!50,
draw=orange,
very thick
}
}
\begin{tikzpicture}
% Use defined styles
\node[mybox] at (0,0) {Box 1};
\node[mybox] at (3,0) {Box 2};
\draw[myarrow] (1.2,0) -- (1.8,0);
% Override parts of a style
\node[mybox, fill=green!20] at (6,0) {Green Box};
\end{tikzpicture}Style Inheritance
Build complex styles by extending simpler ones:
\tikzset{
base/.style={
draw,
thick,
minimum width=1.5cm,
minimum height=0.8cm
},
process/.style={
base,
fill=blue!20,
rounded corners
},
decision/.style={
base,
fill=yellow!30,
diamond,
aspect=2
},
terminal/.style={
base,
fill=green!20,
rounded corners=10pt
}
}
\begin{tikzpicture}
\node[terminal] at (0,0) {Start};
\node[process] at (0,-1.5) {Process};
\node[decision] at (0,-3) {Test?};
\end{tikzpicture}Scope-Based Styles
Apply styles to a group of elements using scope:
\begin{tikzpicture}
% Default styling
\draw (0,0) -- (1,0);
% Scope with specific styles
\begin{scope}[thick, blue, ->]
\draw (0,-0.5) -- (1,-0.5);
\draw (0,-1) -- (1,-1);
\draw (0,-1.5) -- (1,-1.5);
\end{scope}
% Another scope
\begin{scope}[xshift=2cm, red, dashed]
\draw (0,0) rectangle (1,-1);
\draw (0.5,0) -- (0.5,-1);
\end{scope}
\end{tikzpicture}Next Steps
Continue learning TikZ:
- Coordinates - Positioning elements precisely
- Nodes and Labels - Adding styled text
- Flowcharts - Apply styles to diagrams