% \iffalse meta-comment
%
% Copyright (C) 2011,2012,2013 Zvi Gilboa (zgilboa@virginia.edu)
% 
% This file is part of the `rtlcolors' package.
% ---------------------------------------------
%
% \fi
\ProvidesPackage{rtlcolors}[2012/07/04 v0.1 - right-to-left color support]

% Package Options
\DeclareOption{disable}{%
	\@rtlcol@rtlcolorsdisabledfalse
}

\DeclareOption{enable}{%
	\@rtlcol@rtlcolorsdisabledtrue
}

\DeclareOption{UnifiedColorCommands}{%
    \@rtlcol@UnifiedColorCommandstrue
}

% rtl-colors: enabled/disabled: enabled by default (since disabled is false)
\newif\if@rtlcol@rtlcolorsdisabled

% rtl-colors: if enabled, then activate by default
\newif\if@rtlcol@rtlcolors\@rtlcol@rtlcolorstrue

%  rtl-colors: unified-syntax: disabled by default
\newif\if@rtlcol@UnifiedColorCommands

% process options to determine whether rtl-colors are enabled.
\ProcessOptions

\if@rtlcol@rtlcolorsdisabled
    \def\rtlcol@color#1{\empty}
    \def\rtlcol@textcolor#1#{\empty}
    \def\rtlcol@RtlColorCommand#1{\empty}
\else
    \def\rtlcol@color#1{\if@rtlcol@rtlcolors%
                            \rtlcol@xcolor@color{.}\rtlcol@xcolor@color{#1}%
                            \def\rtlcol@RtlColorCommand{%
                                            \if@rl\beginL\fi%
                                            \rtlcol@xcolor@color{#1}%
                                            \if@rl\beginR\fi}%
                        \fi}

    \def\rtlcol@textcolor#1#{\@rtlcol@textcolor{#1}}
    \def\@rtlcol@textcolor#1#2#3{%
                    \if@rl%
                        \protect\leavevmode{\rtlcol@color#1{#2}#3\rtlcol@color#1{#2}}%
                    \else
                        \protect\leavevmode{\rtlcol@color#1{#2}#3}%
                    \fi}
    % set the initial value of rtlcol@RtlColorCommand to black
    \def\rtlcol@RtlColorCommand{\if@rl\beginL\fi%
                                \rtlcol@xcolor@color{black}%
                                \if@rl\beginR\fi}
\fi

\AtBeginDocument{%
    \ifcsname color\endcsname
        % retrieve
        \ifcsname tabu@color\endcsname
            \let\rtlcol@xcolor@color=\tabu@color
        \else
            \let\rtlcol@xcolor@color=\color
        \fi
        % set
        \let\RtlColor=\rtlcol@color
        \let\RtlTextColor=\rtlcol@textcolor
        % replace if needed
        \if@rtlcol@UnifiedColorCommands
            \let\color=\rtlcol@color
            \let\textcolor=\rtlcol@textcolor
        \fi
    \else
        \def\RtlColor#1{\empty}
        \def\RtlTextColor#1#{\empty}
    \fi}

\endinput
