0

I recently asked how to test if an argument is an integer using LaTeX3 (here). I am wondering how to use the same approach to test if a string correspond to a HTML color-code. Currently, I use the following:

\documentclass[varwidth = true]{standalone}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\ishtmlcolor}{>{\TrimSpaces} m m m}{
    \is_html_color:eTF{#1}{#2}{#3}
}
\regex_new:N \is_html_color_regex 
\regex_set:Nn \is_html_color_regex{\A\s*[0-9a-fA-F]{6}\s*\Z}
\cs_new:Nn \is_html_color:nTF{
    \regex_match:NnTF \is_html_color_regex{#1}{#2}{#3}
}
\cs_generate_variant:Nn \is_html_color:nTF{eTF}
\ExplSyntaxOff

\begin{document}
\ishtmlcolor{00FF00}{TRUE}{FALSE}
\ishtmlcolor{00AABB}{TRUE}{FALSE}
\ishtmlcolor{yellow}{TRUE}{FALSE}
\end{document}

QUESTION: How to replace the regex by something way faster in terms of compile-time?

Remark: I know that some 6-letter words in English only contain A-F, like "facade" and I'm OK detecting these as HTML color codes.

5
  • Not really on topic: more words than you think turn out to be valid html colors. stackoverflow.com/q/8318911/2336725
    – Teepeemm
    Commented Sep 2 at 17:05
  • I only want the equivalent of the current regex to work.
    – Vincent
    Commented Sep 2 at 17:25
  • Do you have something that shows the current regex is slow? This doesn't seem all that slow to me. I would think \TrimSpaces would mean you don't need the \s*. You could also try upper (or lower) casing #1, which would simplify your character class.
    – Teepeemm
    Commented Sep 2 at 19:14
  • Why would you ever need this? Is there anywhere that you could use an html hex color but not a decimal rgb one? Commented Sep 2 at 19:27
  • A subsequent version of this question: tex.stackexchange.com/q/725776/107497
    – Teepeemm
    Commented Sep 4 at 13:32

0

You must log in to answer this question.

Browse other questions tagged .