# Box with color gradient in header (KomaScript) on page with new section

I am trying to create a twosided document with KomaScript. I want to add an extra something to every page where a new section begins. I started with creating a new pagestyle according to this post. I tried to modify the example given there. So far, I have done this:

\documentclass[12pt,a4paper,ngerman, twoside]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage[left=3.8cm,right=2.2cm,top=3cm,bottom=3cm]{geometry}
\usepackage{xcolor}

\clearpairofpagestyles
\renewcommand*\pagemark{{\usekomafont{pagenumber}Seite\nobreakspace\thepage}}
\automark[section]{section}
\cfoot[]{}
\ofoot[]{}
\ifoot[]{}

\newpairofpagestyles{highlights}{
\makebox[\dimexpr\linewidth-2\fboxsep][l]{\color{white}%
\hfill
\pagemark}
}
}
}

\begin{document}
\section{Section 1}
Some text.
\cleardoublepage
\thispagestyle{highlights}
\section{Section 2}
Some text.
\end{document}

This results in this style of header:

I would like to achieve three things:

1. Create a custom pagestyle in the given MWE that I can activate on specific pages (where new sections begin) which contains the colorbar (which I specify in the following) and the pagenumber

2. Create the colorbar with a color gradient behind thpage numberer in the header (it would be nice if the colorbar optionally could also extend out of the header region in the inner or outer direction, depending on taste)

3. Change the textcolor of the pagemark to be white, so I have white text in front of the colorbar.

To visualize what I am after, I edited the important page of my MWE; this is what I desire to achieve:

I already tried to create a new pagestyle in the following way, which also was suggested in the above link. I don't have enough experience with tikz though to make it work:

\usepackage{tikz}
contents={\hspace*{0cm}%
\tikz{
\fill[shade,left color=blue!0,right color=blue!20,right color=blue!70] (0,0)rectangle(20cm ,{1.4cm});
\node[rotate=0,font=\bfseries] at({\textwidth-0.9cm},{0})
{\ppagemark};
}}
]{highlights.bg}
\DeclareNewPageStyleByLayers{highlights}{highlights.bg}

Also, with this approach I don't even know how I would get the page number where I want.

Maybe someone of you has done this before or understands the matter deeply enough to be able to help me out.

Unfortunately, I don't really understand what the desired result is supposed to be. But I would suggest to define a new layer for the header background:

\DeclareNewLayer[
background,
hoffset=0pt,
width=\paperwidth,
mode=picture,
contents={\putLL{\tikz{
\ifodd \value{page}
(0,0)rectangle(\the\layerwidth,\the\layerheight);
\else
(0,0)rectangle(\the\layerwidth,\the\layerheight);
\fi
}}
}
]{highlights.bg}

Then you can add this layer at the beginning of your new defined pagestyle:

\newpairofpagestyles{highlights}{
}

Example:

\documentclass[12pt,ngerman, twoside]{scrartcl}

\usepackage[T1]{fontenc}
%\usepackage[utf8]{inputenc}% needed with older TeX distributions
\usepackage{lmodern}
\usepackage{babel}
\usepackage[left=3.8cm,right=2.2cm,top=3cm,bottom=3cm]{geometry}

\clearpairofpagestyles
\renewcommand*\pagemark{{\usekomafont{pagenumber}Seite\nobreakspace\thepage}}
\automark[section]{section}

\newpairofpagestyles{highlights}{
}
\DeclareNewLayer[
background,
hoffset=0pt,
width=\paperwidth,
mode=picture,
contents={\putLL{\tikz{
\ifodd \value{page}
(0,0)rectangle(\the\layerwidth,\the\layerheight);
\else
(0,0)rectangle(\the\layerwidth,\the\layerheight);
\fi
}}
}
]{highlights.bg}

\usepackage{blindtext}% only for dummy text
\begin{document}
\section{Section 1}
Some text.

\cleardoublepage
\thispagestyle{highlights}
\blinddocument
\end{document}

But if every section in your document should start on a new odd page, then you can switch to class scrreprt with option open=right. So here is an additional suggestion assuming that pagestyle highlights uses the same layers as pagestyle plain.scrheadings and the new layer highlight.bg:

\documentclass[12pt,ngerman, twoside,open=right]{scrreprt}
\RedeclareSectionCommand[beforeskip=0pt,afterindent=false]{chapter}

\usepackage[T1]{fontenc}
%\usepackage[utf8]{inputenc}% needed with older TeX distributions
\usepackage{lmodern}
\usepackage{babel}
\usepackage[left=3.8cm,right=2.2cm,top=3cm,bottom=3cm]{geometry}

\clearpairofpagestyles
\renewcommand*\pagemark{{\usekomafont{pagenumber}Seite\nobreakspace\thepage}}
\automark[chapter]{chapter}

\DeclareNewLayer[
background,
oddpage,% not on even pages in two-sided documents
hoffset=0pt,
width=\paperwidth,
mode=picture,
contents={\putLL{\tikz{
(0,0)rectangle(\the\layerwidth,\the\layerheight);
}}
}
]{highlights.bg}
\DeclareNewPageStyleByLayers{highlights}{highlights.bg}

\renewcommand\chapterpagestyle{highlights}

\usepackage{blindtext}% only for dummy text
\begin{document}
\chapter{Chapter 1}
Some text.
\blinddocument
\end{document}

Update (regarding a comment below)

You can use options addheight, addhoffset, addvoffset, addwidth, height, hoffset, voffset and width to adjust the position and the size of the new defined layer with the background rule.

If you want to enlarge the header background rule vertically, you can use options addvoffset and addheight in the layer definition.

Example for the second MWE above:

\DeclareNewLayer[
background,
oddpage,% not on even pages in two-sided documents
addvoffset=2mm,% moves the layer 2mm down
addheight=4mm,% enlarges the height of the layer by 4mm
hoffset=0pt,
width=\paperwidth,
mode=picture,
contents={\putLL{\tikz{
(0,0)rectangle(\the\layerwidth,\the\layerheight);
}}
}
]{highlights.bg}

If the background rule should reach from the inner text to the outer paper border, do not change hoffset and adjust the width:

\DeclareNewLayer[
background,
oddpage,% not on even pages in two-sided documents
addvoffset=2mm,% moves the layer 2mm down
addheight=4mm,% enlarges the height of the layer by 4mm
%hoffset=0pt,% <- removed
width=\paperwidth-1in-\hoffset-\oddsidemargin,% <- changed
mode=picture,
contents={\putLL{\tikz{