Let $f:{\mathbb R}\rightarrow{\mathbb R}$ be measurable and such that $$\int_{-\infty}^0(f(x)-a)^2dx+\int_0^{+\infty}(f(x)-b)^2dx<+\infty.$$ This, denoted as $E(a,b)$, is an affine space: $E(a,b):=f_0+L^2({\mathbb R})$ for any $f_0\in E(a,b)$. I am interested in the $L^2$-projection $\pi f$ of a given $f$ onto the close convex subset $K(a,b)$ which consists in non-decreasing function: $\pi f\in K(a,b)$ and $$\forall g\in K(a,b),\qquad \int_{\mathbb R}(\pi f-f)^2dx\le\int_{\mathbb R}(g-f)^2dx.$$
My guess (or a vague souvenir ?) is that $\pi f$ is the derivative of $\phi$, the convex enveloppe of a primitive $F$ of $f$.
This is probably well-known, and I should be delighted to receive a reference. Unless my guess is incorrect, in which case any help is welcome.