samcarter
I occasionally convert svg images to tikz with the help of [svg2tikz](https://github.com/kjellmf/svg2tikz). The resulting tikz code uses hex colour names in the format of crrggbb in which r, g and b are hexadecimal figures, e.g. cffa500.

To use this code I normally either have to add suitable definitions in my document, e.g. \definecolour{cffa500}{HTML}{ffa500} or I have to replace all the colours by defined colours. Both is a bit laborious if I just want to have a quick look at the image.

That's why I am wondering if one might hack xcolor so that instead of throwing an undefined colour error, it tests if the missing colour starts with c and if yes defines this missing colour on the fly. (This will cause strange problems if there are other undefined colours starting with c, but I'm willing to take this risk)

The following code seems to work for commands like \color and \textcolor, but if I try to use it with tikz, it fails with ! Package xcolor Error: Undefined color cffa500..


\documentclass{article}

\usepackage{xcolor}
\usepackage{xstring}
\usepackage{xpatch}
\usepackage{tikz}

\makeatletter
\xpatchcmd{\XC@split@viii}{\c@lor@error{\@@nam'}\def\@@nam{black}}{%
\def\colorprefix{c}%
\StrSplit{#1}{1}{\firstletter}{\restletter}%
\if\firstletter\colorprefix
\definecolor{#1}{HTML}{\restletter}%
\def\@@nam{#1}%
\else
\c@lor@error{\@@nam'}\def\@@nam{black}%
\fi
}{\typeout{patching xcolor sucess}}{\typeout{patching xcolor failure}}
\makeatother

\begin{document}

\textcolor{cffa500}{\rule{2cm}{2cm}}
\textcolor{c10a500}{\rule{2cm}{2cm}}

\end{document}


P.S. in case someone is wondering why not simply define all possible hex colours? An input file with a complete list of them is about 600 MB and I have yet to find out how long it takes to input this list into a document because the job is still running ... either it takes a really long time which is impractical or it hangs indefinitely
marmot
I think that the patch is not complete, for instance \colorlet also leads to an error. So I am wondering if defining your own fill may be an option.


\documentclass{article}

\usepackage{xcolor}
\usepackage{xstring}
\usepackage{xpatch}
\usepackage{tikz}

\makeatletter
\xpatchcmd{\XC@split@viii}{\c@lor@error{\@@nam'}\def\@@nam{black}}{%
\def\colorprefix{c}%
\StrSplit{#1}{1}{\firstletter}{\restletter}%
\if\firstletter\colorprefix
\definecolor{#1}{HTML}{\restletter}%
\def\@@nam{#1}%
\else
\c@lor@error{\@@nam'}\def\@@nam{black}%
\fi
}{\typeout{patching xcolor sucess}}{\typeout{patching xcolor failure}}
\makeatother

\begin{document}

\textcolor{cffa500}{\rule{2cm}{2cm}}
\textcolor{c10a500}{\rule{2cm}{2cm}}
%\colorlet{mycolor}{cffa500} %<also fails

\tikzset{Fill/.code={\StrSplit{#1}{1}{\firstletter}{\restletter}%
\definecolor{#1}{HTML}{\restletter}%
\tikzset{fill=#1}
}}

\end{document}


![Screen Shot 2020-01-23 at 10.16.53 AM.png](/image?hash=dee8d749e53737ed737a1965fafc11b44f3584eb32fa73085a5c38d6c00afa66)
samcarter
@marmots great answer made me realize that I was totally barking up the wrong tree. Instead of trying to hack xcolor, one can tackle the problem from the tikz side.

One possibility (probably not the best) is to hook into the definition of \pgfsetfillcolor


\documentclass{article}

\usepackage{xstring}
\usepackage{tikz}
\usepackage{tikzlings}

\makeatletter
\def\pgfsetfillcolor#1{%
\def\colorprefix{c}%
\StrLen{#1}[\colorlenght]%
\StrSplit{#1}{1}{\firstletter}{\restletter}%
\if\firstletter\colorprefix%
\ifnum\colorlenght=7%
\definecolor{#1}{HTML}{\restletter}%
\fi%
\fi%
\pgfutil@colorlet{pgffillcolor}{#1}%
\pgfutil@ifundefined{applycolormixins}{}{\applycolormixins{pgffillcolor}}%
\expandafter\let\expandafter\pgf@temp\csname\string\color@pgffillcolor\endcsname
% for arrow tips:
\global\let\pgf@fillcolor@global=\pgf@temp
\expandafter\pgf@setfillcolor\pgf@temp
}
\makeatother

\begin{document}

\begin{tikzpicture}

\marmot

\end{tikzpicture}

\end{document}


Caveats:

- this will only influence fill colours, but similar could be done with strokes etc.

- it will run into a problem if combined with non hex colours that also start with c and also have 7 characters

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.