Good morning. In my course, and in order to explain the mean value of a function, I need to reproduce this photo using tikz
My problem is the superposed colours. In fact, I would like to keep both of them visible. Any help is very appreciated Here is my code :
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[help lines, color=gray, dashed] (-2,-1) grid (8,5);
\draw[->, thick] (-2,0)--(8,0) node[right]{$x$};
\draw[->, thick] (0,-1)--(0,5) node[above]{$y$};
\begin{scope}
\clip (-0.3,3.7) .. controls (2.3,0.9) and (3.1,3.9) .. (5.1,4) ..
controls (6.5,4.1) and (6.5,3) .. (8.2,2)--(6,-1)--(-.3,-1)--cycle;
\fill[pink] (0.5,0)node [below=5pt, black] {$a$}--(6,0)node [below=5pt,
black] {$b$}--(6,5)--(0.5,5)--(0.5,0)--cycle ;
\draw[thick, red, dashed] (0.5,0)--(0.5,5);
\draw[thick, red, dashed] (6,0)--(6,5);
\node at (3.2,1.5) {$\Huge S$};
\fill[green] (0,0)node [below=5pt, black] {$a$}--(6,0)node [below=5pt,
black] {$b$}--(6,2)--(0,2)--(0,0)--cycle ;
\end{scope}
\draw[ultra thick, red] (-0.3,3.7) .. controls (2.3,0.9) and (3.1,3.9) .. (5.1,4) .. controls (6.5,4.1) and (6.5,3) .. (8.2,2)node[black, above=15pt,pos=.9]{$f(x)$};
\draw[ultra thick, blue] (0,2) .. controls (2,2) and (4,2) .. (6,2)node[black, above=15pt,pos=.9]{$f(x)$};
\end{tikzpicture}
\end{document}
opacity
options for fill.