summaryrefslogtreecommitdiff
path: root/tex/context/base/colo-hex.tex
blob: 2eddcf19ba2237bbf65060ebbbc3bbf0bba474b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
%D \module
%D   [       filefile=colo-hex,
%D        version=2004.06.23,
%D          title=\CONTEXT\ Color Macros,
%D       subtitle=Hex Colors,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\ifx\dodododefinecolor\undefined
  \beginTEX \endinput \endTEX
\else
  \endinput
\fi

\writestatus{loading}{Context Color Macros / hexadecimal}

% \edef\testcolor{\string#FFC0C0}
% \edef\testcolor{\string#55}
%
% \setupcolors[state=start]
%
% \expanded{\definecolor[thehexcolor][\hexcolorspec\testcolor]}
%
% \checkhexcolor[\testcolor]
%
% \definecolor[thehexcolor][\testcolor]
%
% \starttext
%
% test \color[thehexcolor]{rood}
% test \color[red]{rood}
% test \color[\testcolor]{rood}
%
% \stoptext

\unprotect

\newdimen\hexcolorfraction \hexcolorfraction=\dimexpr(1pt/256)

\chardef\hexcolorprefix=`#

\def\hexcolorspec   #1{\expandafter\dohexcolorspec   #1\empty\empty\empty\empty\relax}
\def\hexcolorpattern#1{\expandafter\dohexcolorpattern#1\empty\empty\empty\empty\relax}

\ifx\dohexstringtonumber\undefined \def\dohexstringtonumber{"} \fi

\def\hexcolorcomponent#1#2%
  {\ifnum\dohexstringtonumber#1#2=\zerocount0\else\ifnum\dohexstringtonumber#1#2=\plusone1\else
     \expandafter\withoutpt\the\dimexpr(\dohexstringtonumber#1#2\hexcolorfraction)%
   \fi\fi}

\def\dohexcolorspec#1#2#3#4#5#6#7#8\relax
  {\ifx#4\empty
     s=\hexcolorcomponent#2#3%
   \else
     r=\hexcolorcomponent#2#3,g=\hexcolorcomponent#4#5,b=\hexcolorcomponent#6#7%
   \fi}

\def\dohexcolorpattern#1#2#3#4#5#6#7#8\relax
  {0\ifx#4\empty
     S:\hexcolorcomponent#2#3%
   \else
     R:\hexcolorcomponent#2#3:\hexcolorcomponent#4#5:\hexcolorcomponent#6#7%
   \fi:0:0}

\def\doifhexcolorelse#1%
  {\expandafter\dodoifhexcolorelse#10\od} % 0 is a dirty trick to catch an empty #1

\def\dodoifhexcolorelse#1#2\od
  {\ifnum`#1=\hexcolorprefix
     \expandafter\firstoftwoarguments
   \else
     \expandafter\secondoftwoarguments
   \fi}

\def\docheckhexcolor#1%
  {\doifhexcolorelse{#1}{\doifundefined{#1}{\setxvalue{\??cr#1}{\hexcolorpattern{#1}}}}\donothing}

\def\checkhexcolor[#1]%
  {\expanded{\docheckhexcolor{#1}}}

\def\colorHpattern{\@EA\hexcolorpattern\@EA{\@EA*\@@cl@@h}} % * == dummy placeholder

\let\dodododefinecolor\dododefinecolor % we will overload this one

\def\dododefinecolor#1#2#3#4[#5][#6]%
  {\doifhexcolorelse{#6}
     {\setxvalue{\??cr#5}{\hexcolorpattern{#6}}}
     {\dodododefinecolor#1#2#3#4[#5][#6]}}

\protect \endinput