%%
%% ----------------------------------------------------------------
%% Copyright (C) 2023-2024 by Simon Pfahler <simon.pfahler at ur dot de>
%% ----------------------------------------------------------------
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{colorblind}[2024/12/23 Easy colorblind-safe typesetting]

\RequirePackage{xcolor}
\RequirePackage{ifthen}

\newif\iftikz
\tikztrue
\DeclareOption{no-tikz}{
	\tikzfalse
}

\newif\ifpgf
\pgffalse
\DeclareOption{pgf}{
	\pgftrue
}

\newif\ifredefinedefaults
\redefinedefaultstrue
\DeclareOption{keep-defaults}{
	\redefinedefaultsfalse
}

\newif\ifTolColors
\TolColorsfalse
\DeclareOption{Tol}{
	\TolColorstrue
	%>>> Color schemes by Paul Tol, source: https://personal.sron.nl/~pault
	%
	%>>> Qualitative color schemes
	%
	% Bright Qualitative color scheme
	\providecolorset{RGB}{T-Q-B}{}{%
		1,68,119,170;%
		2,102,204,238;%
		3,34,136,51;%
		4,204,187,68;%
		5,238,102,119;%
		6,170,51,119;%
		0,187,187,187%
	}
	%
	% High-contrast qualitative color scheme
	\providecolorset{RGB}{T-Q-HC}{}{%
		1,255,255,255;%
		2,221,170,51;%
		3,187,85,102;%
		4,0,68,136;%
		0,0,0,0%
	}
	%
	% Vibrant qualitative color scheme
	\providecolorset{RGB}{T-Q-V}{}{%
		1,0,119,187;%
		2,51,187,238;%
		3,0,153,136;%
		4,238,119,51;%
		5,204,51,17;%
		6,238,51,119;%
		0,187,187,187%
	}
	%
	% Muted qualitative color scheme
	\providecolorset{RGB}{T-Q-M}{}{%
		1,51,34,136;%
		2,136,204,238;%
		3,68,170,153;%
		4,17,119,51;%
		5,153,153,51;%
		6,221,204,119;%
		7,204,102,119;%
		8,136,34,85;%
		9,170,68,153;%
		0,221,221,221%
	}
	%
	% Medium-contrast qualitative color scheme
	\providecolorset{RGB}{T-Q-MC}{}{%
		1,255,255,255;%
		2,238,204,102;%
		3,238,153,170;%
		4,102,153,204;%
		5,153,119,0;%
		6,153,68,85;%
		7,0,68,136;%
		0,0,0,0%
	}
	%
	% Pale qualitative color scheme, for highlighting
	\providecolorset{RGB}{T-Q-PH}{}{%
		1,187,204,238;%
		2,204,238,255;%
		3,204,221,170;%
		4,238,238,187;%
		5,255,204,204;%
		0,221,221,221%
	}
	%
	% Dark qualitative color scheme, for text colors
	\providecolorset{RGB}{T-Q-DT}{}{%
		1,34,34,85;%
		2,34,85,85;%
		3,34,85,34;%
		4,102,102,51;%
		5,102,51,51;%
		0,85,85,85%
	}
	%
	% Light qualitative color scheme
	\providecolorset{RGB}{T-Q-L}{}{%
		1,119,170,221;%
		2,153,221,255;%
		3,68,187,153;%
		4,187,204,51;%
		5,170,170,0;%
		6,238,221,136;%
		7,238,136,102;%
		8,255,170,187;%
		0,221,221,221%
	}
	%
	%<<< Qualitative color schemes
	%
	%>>> Diverging color schemes
	%
	% Sunset diverging color scheme
	\providecolorset{RGB}{T-D-S}{}{%
		1,54,75,154;%
		2,74,123,183;%
		3,110,166,205;%
		4,152,202,225;%
		5,194,228,239;%
		6,234,236,204;%
		7,254,218,139;%
		8,253,179,102;%
		9,246,126,75;%
		10,221,61,45;%
		11,165,0,38;%
		0,255,255,255%
	}
	%
	% Nightfall diverging color scheme
	\providecolorset{RGB}{T-D-N}{}{%
		1,18,90,86;%
		2,0,118,123;%
		3,35,143,157;%
		4,66,167,198;%
		5,96,188,233;%
		6,157,204,239;%
		7,198,219,237;%
		8,222,230,231;%
		9,236,234,218;%
		10,240,230,178;%
		11,249,213,118;%
		12,255,185,84;%
		13,253,154,68;%
		14,245,118,52;%
		15,233,76,31;%
		16,209,24,7;%
		17,160,24,19;%
		0,255,255,255%
	}
	%
	% BuRd diverging color scheme
	\providecolorset{RGB}{T-D-BR}{}{%
		1,32,102,172;%
		2,67,147,195;%
		3,146,197,222;%
		4,209,229,240;%
		5,247,247,247;%
		6,253,219,199;%
		7,244,165,130;%
		8,214,96,77;%
		9,178,24,43;%
		0,255,238,153%
	}
	%
	% PRGn diverging color scheme
	\providecolorset{RGB}{T-D-PG}{}{%
		1,118,42,131;%
		2,153,112,171;%
		3,194,165,207;%
		4,231,212,232;%
		5,247,247,247;%
		6,217,240,211;%
		7,172,211,158;%
		8,90,174,97;%
		9,27,120,55;%
		0,255,238,153%
	}
	%
	%<<< Diverging color schemes
	%
	%>>> Sequential color schemes
	%
	% YlOrBr sequential color scheme
	\providecolorset{RGB}{T-S-YOB}{}{%
		1,255,255,229;%
		2,255,247,188;%
		3,254,227,145;%
		4,254,196,79;%
		5,251,154,41;%
		6,236,112,20;%
		7,204,76,2;%
		8,153,52,4;%
		9,102,37,6;%
		0,136,136,136%
	}
	%
	% Iridescent sequential color scheme
	\providecolorset{RGB}{T-S-IR}{}{%
		1,254,251,233;%
		2,252,247,213;%
		3,245,243,193;%
		4,234,240,181;%
		5,221,236,191;%
		6,208,231,202;%
		7,194,227,210;%
		8,181,221,216;%
		9,168,216,220;%
		10,155,210,225;%
		11,141,203,228;%
		12,129,196,231;%
		13,123,188,231;%
		14,126,178,228;%
		15,136,165,221;%
		16,147,152,210;%
		17,155,138,196;%
		18,157,125,178;%
		19,154,112,158;%
		20,144,99,136;%
		21,128,87,112;%
		22,104,73,87;%
		23,70,53,58;%
		0,153,153,153%
	}
	%
	% Incandescent sequential color scheme
	\providecolorset{RGB}{T-S-IN}{}{%
		1,206,255,255;%
		2,198,247,214;%
		3,162,244,155;%
		4,187,228,83;%
		5,213,206,4;%
		6,231,181,3;%
		7,241,153,3;%
		8,246,121,11;%
		9,249,73,2;%
		10,228,5,21;%
		11,168,0,3;%
		0,136,136,136%
	}
	%
	% Discrete rainbow color scheme
	\providecolorset{RGB}{T-S-DR}{}{%
		1,209,187,215;%
		2,174,118,163;%
		3,136,46,114;%
		4,25,101,176;%
		5,82,137,199;%
		6,123,175,222;%
		7,78,178,101;%
		8,144,201,135;%
		9,202,224,171;%
		10,247,240,86;%
		11,246,193,65;%
		12,241,147,45;%
		13,232,96,28;%
		14,220,5,12;%
		0,119,119,119%
	}
	%
	% Smooth rainbow color scheme
	\providecolorset{RGB}{T-S-SR}{}{%
		1,232,236,251;%
		2,221,216,239;%
		3,209,193,225;%
		4,195,168,209;%
		5,181,143,194;%
		6,167,120,180;%
		7,155,98,167;%
		8,140,78,153;%
		9,111,76,155;%
		10,96,89,169;%
		11,85,104,184;%
		12,78,121,197;%
		13,77,138,198;%
		14,78,150,188;%
		15,84,158,179;%
		16,89,165,169;%
		17,96,171,158;%
		18,105,177,144;%
		19,119,183,125;%
		20,140,188,104;%
		21,166,190,84;%
		22,190,188,72;%
		23,209,181,65;%
		24,221,170,60;%
		25,228,156,57;%
		26,231,140,53;%
		27,230,121,50;%
		28,228,99,45;%
		29,223,72,40;%
		30,218,34,34;%
		31,184,34,30;%
		32,149,33,27;%
		33,114,30,23;%
		34,82,26,19;%
		0,102,102,102%
	}
	%
	%<<< Sequential color schemes
	%
	%<<< Color schemes by Paul Tol, source: https://personal.sron.nl/~pault
}

\DeclareOption{OkabeIto}{
	%>>> Qualitative color scheme by Okabe and Ito
	\providecolorset{RGB}{OI}{}{%
		1,230,159,0;%
		2,86,180,233;%
		3,0,158,115;%
		4,240,228,66;%
		5,0,114,178;%
		6,213,94,0;%
		7,204,121,167;%
		0,0,0,0%
	}
	%<<< Qualitative color scheme by Okabe and Ito
}

\DeclareOption*{\PackageWarning{colorblind}{Unknown '\CurrentOption'}}
\ProcessOptions\relax

\ifredefinedefaults
	% This redefines the default colors by removing most of them and setting
	% the remaining to colors from the qualitative Vibrant color scheme by Tol.
	\xdefinecolor{blue}{RGB}{68,119,170}
	\xdefinecolor{cyan}{RGB}{102,204,238}
	\xdefinecolor{green}{RGB}{34,136,51}
	\xdefinecolor{yellow}{RGB}{204,187,68}
	\xdefinecolor{red}{RGB}{238,102,119}
	\xdefinecolor{violet}{RGB}{170,51,119}
	\xdefinecolor{gray}{RGB}{187,187,187}
	\xdefinecolor{orange}{RGB}{0,0,0}
	\xdefinecolor{brown}{RGB}{0,0,0}
	\xdefinecolor{darkgray}{RGB}{0,0,0}
	\xdefinecolor{lightgray}{RGB}{0,0,0}
	\xdefinecolor{lime}{RGB}{0,0,0}
	\xdefinecolor{magenta}{RGB}{0,0,0}
	\xdefinecolor{olive}{RGB}{0,0,0}
	\xdefinecolor{pink}{RGB}{0,0,0}
	\xdefinecolor{purple}{RGB}{0,0,0}
	\xdefinecolor{teal}{RGB}{0,0,0}
\fi

% define continuous colormaps and command to plot them if `pgf` option is given
\ifpgf
	% do nothing if option `Tol` was not provided, since then no applicable color schemes exist
	\ifTolColors
		\RequirePackage{pgfplots}
		\pgfplotsset{compat=1.18}
		% Sunset diverging colormap
		\pgfplotsset{
			colormap={T-D-S}{color(0)=(T-D-S1) color(1)=(T-D-S2) color(2)=(T-D-S3) color(3)=(T-D-S4) color(4)=(T-D-S5) color(5)=(T-D-S6) color(6)=(T-D-S7) color(7)=(T-D-S8) color(8)=(T-D-S9) color(9)=(T-D-S10) color(10)=(T-D-S11)}
		}
		% Nightfall diverging colormap
		\pgfplotsset{
			colormap={T-D-N}{color(0)=(T-D-N1) color(1)=(T-D-N2) color(2)=(T-D-N3) color(3)=(T-D-N4) color(4)=(T-D-N5) color(5)=(T-D-N6) color(6)=(T-D-N7) color(7)=(T-D-N8) color(8)=(T-D-N9) color(9)=(T-D-N10) color(10)=(T-D-N11) color(11)=(T-D-N12) color(12)=(T-D-N13) color(13)=(T-D-N14) color(14)=(T-D-N15) color(15)=(T-D-N16) color(16)=(T-D-N17)}
		}
		% BuRd diverging colormap
		\pgfplotsset{
			colormap={T-D-BR}{color(0)=(T-D-BR1) color(1)=(T-D-BR2) color(2)=(T-D-BR3) color(3)=(T-D-BR4) color(4)=(T-D-BR5) color(5)=(T-D-BR6) color(6)=(T-D-BR7) color(7)=(T-D-BR8) color(8)=(T-D-BR9)}
		}
		% PRGn diverging colormap
		\pgfplotsset{
			colormap={T-D-PG}{color(0)=(T-D-PG1) color(1)=(T-D-PG2) color(2)=(T-D-PG3) color(3)=(T-D-PG4) color(4)=(T-D-PG5) color(5)=(T-D-PG6) color(6)=(T-D-PG7) color(7)=(T-D-PG8) color(8)=(T-D-PG9)}
		}
		% YlOrBr sequential colormap
		\pgfplotsset{
			colormap={T-S-YOB}{color(0)=(T-S-YOB1) color(1)=(T-S-YOB2) color(2)=(T-S-YOB3) color(3)=(T-S-YOB4) color(4)=(T-S-YOB5) color(5)=(T-S-YOB6) color(6)=(T-S-YOB7) color(7)=(T-S-YOB8) color(8)=(T-S-YOB9)}
		}
		% Iridescent sequential colormap
		\pgfplotsset{
			colormap={T-S-IR}{color(0)=(T-S-IR1) color(1)=(T-S-IR2) color(2)=(T-S-IR3) color(3)=(T-S-IR4) color(4)=(T-S-IR5) color(5)=(T-S-IR6) color(6)=(T-S-IR7) color(7)=(T-S-IR8) color(8)=(T-S-IR9) color(9)=(T-S-IR10) color(10)=(T-S-IR11) color(11)=(T-S-IR12) color(12)=(T-S-IR13) color(13)=(T-S-IR14) color(14)=(T-S-IR15) color(15)=(T-S-IR16) color(16)=(T-S-IR17) color(17)=(T-S-IR18) color(18)=(T-S-IR19) color(19)=(T-S-IR20) color(20)=(T-S-IR21) color(21)=(T-S-IR22) color(22)=(T-S-IR23)}
		}
		% Incandescent sequential colormap
		\pgfplotsset{
			colormap={T-S-IN}{color(0)=(T-S-IN1) color(1)=(T-S-IN2) color(2)=(T-S-IN3) color(3)=(T-S-IN4) color(4)=(T-S-IN5) color(5)=(T-S-IN6) color(6)=(T-S-IN7) color(7)=(T-S-IN8) color(8)=(T-S-IN9) color(9)=(T-S-IN10) color(10)=(T-S-IN11)}
		}
		% Smooth Rainbow sequential colormap
		\pgfplotsset{
			colormap={T-S-SR}{color(0)=(T-S-SR1) color(1)=(T-S-SR2) color(2)=(T-S-SR3) color(3)=(T-S-SR4) color(4)=(T-S-SR5) color(5)=(T-S-SR6) color(6)=(T-S-SR7) color(7)=(T-S-SR8) color(8)=(T-S-SR9) color(9)=(T-S-SR10) color(10)=(T-S-SR11) color(11)=(T-S-SR12) color(12)=(T-S-SR13) color(13)=(T-S-SR14) color(14)=(T-S-SR15) color(15)=(T-S-SR16) color(16)=(T-S-SR17) color(17)=(T-S-SR18) color(18)=(T-S-SR19) color(19)=(T-S-SR20) color(20)=(T-S-SR21) color(21)=(T-S-SR22) color(22)=(T-S-SR23) color(23)=(T-S-SR24) color(24)=(T-S-SR25) color(25)=(T-S-SR26) color(26)=(T-S-SR27) color(27)=(T-S-SR28) color(28)=(T-S-SR29) color(29)=(T-S-SR30) color(30)=(T-S-SR31) color(31)=(T-S-SR32) color(32)=(T-S-SR33) color(33)=(T-S-SR34)}
		}
		%
		%>>> Function to print a continuous color scheme
		\newcommand\drawSchemeC[2][0.5\textwidth]{%
			\tikz{%
				\pgfplotscolorbardrawstandalone[colormap name=#2, colorbar horizontal, colorbar style={width=#1, ticks=none}]%
				\draw[fill=#20] (#1+0.5*\@drawSchemeOffset,-0.55cm) circle(0.5*\@drawSchemeRadius);%
			}%
		}
		%<<< Function to print a continuous color scheme
	\fi
\fi

% define command to draw discrete color schemes when tikz is enabled
\iftikz
	\RequirePackage{tikz}
	\usetikzlibrary{math}
	%>>> Function to print a discrete color scheme
	\newcommand\@drawSchemeRadius{0.7cm}
	\newcommand\@drawSchemeOffset{1cm}
	\newcounter{ColorNum}
	\newcommand\drawScheme[1]{%
		\tikz{%
			\node[inner sep=1, fill=red] at (-1.2*\@drawSchemeRadius,2.3*\@drawSchemeRadius) {R};
			\node[inner sep=1, fill=green] at (-1.2*\@drawSchemeRadius, 1.8*\@drawSchemeRadius) {G};
			\node[inner sep=1, fill=blue] at (-1.2*\@drawSchemeRadius,1.3*\@drawSchemeRadius) {B};
			\setcounter{ColorNum}{1}%
			\whiledo{%
				\@ifundefined{\string\color@#1\theColorNum}{\boolean{false}}{\boolean{true}}%
			}{%
				\extractcolorspecs{#1\the\value{ColorNum}}{\modelspec}{\colorspec}%
				\tikzmath{
					int \r, \g, \b;
					\r = round(255*array({\colorspec},0));
					\g = round(255*array({\colorspec},1));
					\b = round(255*array({\colorspec},2));
				}%
				\node at (\value{ColorNum}*\@drawSchemeOffset-\@drawSchemeOffset,2.3*\@drawSchemeRadius) {\r};
				\node at (\value{ColorNum}*\@drawSchemeOffset-\@drawSchemeOffset,1.8*\@drawSchemeRadius) {\g};
				\node at (\value{ColorNum}*\@drawSchemeOffset-\@drawSchemeOffset,1.3*\@drawSchemeRadius) {\b};
				\draw[fill=#1\the\value{ColorNum}] (\value{ColorNum}*\@drawSchemeOffset-\@drawSchemeOffset,0) circle (\@drawSchemeRadius);%
				\node[rotate=25, anchor=north east] at (\value{ColorNum}*\@drawSchemeOffset-\@drawSchemeOffset,-1*\@drawSchemeRadius) {#1\theColorNum};%
				\stepcounter{ColorNum}%
			}%
			\extractcolorspecs{#10}{\modelspec}{\colorspec}%
			\tikzmath{
				int \r, \g, \b;
				\r = round(255*array({\colorspec},0));
				\g = round(255*array({\colorspec},1));
				\b = round(255*array({\colorspec},2));
			}%
			\node at (\value{ColorNum}*\@drawSchemeOffset-0.5*\@drawSchemeOffset,2.3*\@drawSchemeRadius) {\r};
			\node at (\value{ColorNum}*\@drawSchemeOffset-0.5*\@drawSchemeOffset,1.8*\@drawSchemeRadius) {\g};
			\node at (\value{ColorNum}*\@drawSchemeOffset-0.5*\@drawSchemeOffset,1.3*\@drawSchemeRadius) {\b};
			\draw[fill=#10] (\value{ColorNum}*\@drawSchemeOffset-0.5*\@drawSchemeOffset,0) circle(\@drawSchemeRadius);%
			\node[rotate=25, anchor=north east] at (\value{ColorNum}*\@drawSchemeOffset-0.5*\@drawSchemeOffset, -1*\@drawSchemeRadius) {#10};%
		}%
	}
	%<<< Function to print a discrete color scheme
\fi

\endinput
