[Tex/LaTex] Modify xcolor package for light and dark

color

Can anyone write a macro that will modify the xcolor package so that using !x will darken colors when x < 100 and lighten colors when x is between 100 and 200?

e.g., I would like to be able to do
red!50 and red!150 to get a dark and light red color respectively instead of having to do red!50!black.

Best Answer

Here a suggestion. I defined a command \mycolorcmd which behaves like \color but with the test if the number is greater than 100. The following output is created by:

\mycolorcmd{blue} Text Text

\mycolorcmd{red!140} Text Text

\mycolorcmd{blue!40} Text Text

enter image description here

\documentclass{article}
\usepackage{xcolor}
\makeatletter
\def\mycolorcmd#1{\@mycolorcmd#1!\@nil}
\def\@mycolorcmd#1!#2\@nil{%
 \ifx\relax#2\relax
%    \color{#1}%
 \else%
   \edef\@tempa##1!{ \@tempcnta=##1}%
    \@tempa #2\relax
    \ifnum \@tempcnta > 100\relax%
      \advance \@tempcnta by -100\relax%
         \color{#1!\the\@tempcnta!black}%
   \else%
      \color{#1!\the\@tempcnta}%
   \fi
\fi%
}
\makeatletter
\begin{document}
\mycolorcmd{blue} Text Text

\mycolorcmd{red!140} Text Text

\mycolorcmd{blue!40} Text Text
\end{document}