summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/coloring/coloring.sty
blob: 95b18964ebd9f5dfb01b90b1f7dd866f3818eb7a (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
% -*- coding: utf-8 -*-

\ProvidesPackage{coloring}[2016/05/11 v0.2]

\RequirePackage{xcolor,etoolbox,xparse,l3regex}

\makeatletter

\def\colorring@hack#1{\@ifundefined{\string\color@#1}{\coloringdefine{#1}}{\relax}}

% hacking \definecolor and \colorlet in xcolor
\pretocmd{\@declaredcolor}{\colorring@hack{#1}}{}{}
\pretocmd{\XC@col@rlet}{\colorring@hack{#4}}{}{}

% hacking \define@key{beamerthc}{fg} and \define@key{beamerthc}{bg} in beamer
\catcode`\#=12
\def\coloring@hack@beamer{%
  \pretocmd{\KV@beamerthc@fg}{\colorring@hack{#1}}{}{}%
  \pretocmd{\KV@beamerthc@bg}{\colorring@hack{#1}}{}{}%
}
\catcode`\#=6
\@ifclassloaded{beamer}{\coloring@hack@beamer}{}{}

\makeatother

\ExplSyntaxOn

% local variables for gray colors
\tl_new:N \l_coloring_gray_tl

% local variables for rgb colors
\tl_new:N \l_coloring_r_tl
\tl_new:N \l_coloring_g_tl
\tl_new:N \l_coloring_b_tl

% local variables for hsb/hsv colors
\int_new:N \l_coloring_h_int
\tl_new:N \l_coloring_s_tl
\tl_new:N \l_coloring_v_tl

\clist_new:N \l_coloring_hue_name_clist
\clist_set:Nn \l_coloring_hue_name_clist
  {
    red,     % 0
    brown,   % 30
    yellow,  % 60
    olive,   % 90
    green,   % 120
    teal,    % 150
    cyan,    % 180
    azure,   % 210
    blue,    % 240
    violet,  % 270
    magenta, % 300
    purple   % 330
  }

\NewDocumentCommand \coloringdefine { m }
{
  % define gray colors
  \coloring_define_gray:n { #1 }
  % define rgb colors
  \coloring_define_rgb:n { #1 }
  % define hsb colors
  \int_set:Nn \l_coloring_h_int { 0 }
  \clist_map_inline:Nn \l_coloring_hue_name_clist
    {
      \coloring_define_hsb:nnn { \l_coloring_h_int } { ##1 } { #1 }
      \int_add:Nn \l_coloring_h_int { 30 }
    }
}

\cs_new_protected_nopar:Npn \coloring_define_gray:n #1
{
  \regex_extract_once:nnNT { \A gray([0-9x]) \Z } { #1 } \l_coloring_temp_seq
    {
      \seq_pop_right:NN \l_coloring_temp_seq \l_coloring_gray_tl
      \coloring_set_value:N \l_coloring_gray_tl
      \definecolor {#1} {gray} {\l_coloring_gray_tl}
    }
}

\cs_new_protected_nopar:Npn \coloring_define_rgb:n #1
{
  \regex_extract_once:nnNT { \A rgb([0-9x])([0-9x])([0-9x]) \Z } { #1 } \l_coloring_temp_seq
    {
      \seq_pop_right:NN \l_coloring_temp_seq \l_coloring_b_tl
      \seq_pop_right:NN \l_coloring_temp_seq \l_coloring_g_tl
      \seq_pop_right:NN \l_coloring_temp_seq \l_coloring_r_tl
      \coloring_set_value:N \l_coloring_b_tl
      \coloring_set_value:N \l_coloring_g_tl
      \coloring_set_value:N \l_coloring_r_tl
      \definecolor {#1} {rgb} {\l_coloring_r_tl, \l_coloring_g_tl, \l_coloring_b_tl}
    }
}

\cs_new_protected_nopar:Npn \coloring_define_hsb:nnn #1 #2 #3
{
  \regex_extract_once:nnNT { \A #2([0-9x])([0-9x]) \Z } { #3 } \l_coloring_temp_seq
    {
      \seq_pop_right:NN \l_coloring_temp_seq \l_coloring_v_tl
      \seq_pop_right:NN \l_coloring_temp_seq \l_coloring_s_tl
      \coloring_set_value:N \l_coloring_v_tl
      \coloring_set_value:N \l_coloring_s_tl
      \definecolor {#3} {Hsb} {\int_use:N #1, \l_coloring_s_tl, \l_coloring_v_tl}
    }
}

\cs_new_protected_nopar:Npn \coloring_set_value:N #1
{
  \tl_set:Nn \l_tmpa_tl { x }
  \tl_if_eq:NNTF #1 \l_tmpa_tl { \tl_set:Nn #1 {1} } { \tl_set:Nx #1 {.#1} }
}

\ExplSyntaxOff