\documentclass{article}
\usepackage[pdftex]{color}

\makeatletter
\def\mymacro#1{%
  \setbox\Tempwidth=\hbox{e}%
  \hbox to 0pt{%
  \color@begingroup %
  \color{red}%
  \raise 2pt%
  \copy\Tempwidth %
  \color@endgroup %
  \hss %
  }%
  \ifnum#1=0\else %
  \hbox to 0pt{a\hss}%
  \fi %
  \penalty 10001\relax %
  \setbox\Tempwidth=\hbox{e}%
  \raise 2pt%
  \copy\Tempwidth %
  \relax %
}
\makeatother

\begin{document}

\newbox\Tempwidth

first line

a \mymacro{0}

a \mymacro{1}

end line

\end{document}
