Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.