Debian Codegraph @

xsl/color.xsl

raw
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>
3
4 <!--############################################################################
5 XSLT Stylesheet DocBook -> LaTeX
6 ############################################################################ -->
7
8 <!-- Convert hexadecimal format to decimal -->
9 <xsl:template name="hex-to-int">
10 <xsl:param name="hex" select="'0'"/>
11
12 <xsl:choose>
13 <xsl:when test="string-length($hex)=1">
14 <xsl:call-template name="char-to-int">
15 <xsl:with-param name="char" select="$hex"/>
16 </xsl:call-template>
17 </xsl:when>
18 <xsl:otherwise>
19 <xsl:variable name="low.int">
20 <xsl:call-template name="char-to-int">
21 <xsl:with-param name="char"
22 select="substring($hex, string-length($hex))"/>
23 </xsl:call-template>
24 </xsl:variable>
25 <xsl:variable name="high.int">
26 <xsl:call-template name="hex-to-int">
27 <xsl:with-param name="hex"
28 select="substring($hex, 1, string-length($hex)-1)"/>
29 </xsl:call-template>
30 </xsl:variable>
31 <xsl:value-of select="$low.int + 16*$high.int"/>
32 </xsl:otherwise>
33 </xsl:choose>
34 </xsl:template>
35
36 <xsl:template name="char-to-int">
37 <xsl:param name="char" select="'0'"/>
38 <xsl:variable name="c">
39 <xsl:value-of select="translate($char, 'abcdef', 'ABCDEF')"/>
40 </xsl:variable>
41
42 <xsl:value-of select="string-length(substring-before('0123456789ABCDEF',
43 $c))"/>
44 </xsl:template>
45
46 <!-- Convert xxyyzz to rate(xx),rate(yy),rate(zz) -->
47 <xsl:template name="hex-to-rgb">
48 <xsl:param name="hex" select="'0'"/>
49 <xsl:choose>
50 <xsl:when test="string-length($hex) &lt;= 2">
51 <xsl:call-template name="hex-to-rate">
52 <xsl:with-param name="hex" select="$hex"/>
53 </xsl:call-template>
54 </xsl:when>
55 <xsl:otherwise>
56 <xsl:variable name="right">
57 <xsl:call-template name="hex-to-rate">
58 <xsl:with-param name="hex"
59 select="substring($hex, string-length($hex)-1)"/>
60 </xsl:call-template>
61 </xsl:variable>
62 <xsl:variable name="left">
63 <xsl:call-template name="hex-to-rgb">
64 <xsl:with-param name="hex"
65 select="substring($hex, 1, string-length($hex)-2)"/>
66 </xsl:call-template>
67 </xsl:variable>
68 <xsl:value-of select="$left"/>
69 <xsl:text>,</xsl:text>
70 <xsl:value-of select="$right"/>
71 </xsl:otherwise>
72 </xsl:choose>
73 </xsl:template>
74
75 <!-- Convert 0xCC to 0.8 = 204/255 -->
76 <xsl:template name="hex-to-rate">
77 <xsl:param name="hex" select="'0'"/>
78 <xsl:variable name="int">
79 <xsl:call-template name="hex-to-int">
80 <xsl:with-param name="hex" select="$hex"/>
81 </xsl:call-template>
82 </xsl:variable>
83 <xsl:value-of select="round(($int * 100) div 255) div 100"/>
84 </xsl:template>
85
86
87 <!-- Convert color to a proper latex format:
88 #xxyyzz => [rgb]{u,v,w}
89 #xaxaxa => [gray]{u}
90 xxx => {xxx}
91 {xxx} => {xxx}
92 [aa]{xxx} => [aa]{xxx}
93 -->
94 <xsl:template name="get-color">
95 <xsl:param name="color"/>
96 <xsl:choose>
97 <xsl:when test="starts-with($color, '#')">
98 <xsl:variable name="fullcolor"
99 select="concat('000000', substring-after($color, '#'))"/>
100 <xsl:variable name="rcolor"
101 select="substring($fullcolor, string-length($fullcolor)-5)"/>
102 <xsl:variable name="r" select="substring($rcolor,1,2)"/>
103 <xsl:variable name="g" select="substring($rcolor,3,2)"/>
104 <xsl:variable name="b" select="substring($rcolor,5,2)"/>
105 <xsl:choose>
106 <xsl:when test="$r=$g and $g=$b">
107 <xsl:text>[gray]{</xsl:text>
108 <xsl:call-template name="hex-to-rate">
109 <xsl:with-param name="hex" select="$r"/>
110 </xsl:call-template>
111 <xsl:text>}</xsl:text>
112 </xsl:when>
113 <xsl:otherwise>
114 <xsl:text>[rgb]{</xsl:text>
115 <xsl:call-template name="hex-to-rgb">
116 <xsl:with-param name="hex" select="$rcolor"/>
117 </xsl:call-template>
118 <xsl:text>}</xsl:text>
119 </xsl:otherwise>
120 </xsl:choose>
121 </xsl:when>
122 <xsl:when test="starts-with($color, '[') or starts-with($color, '{')">
123 <xsl:value-of select="$color"/>
124 </xsl:when>
125 <xsl:otherwise>
126 <xsl:text>{</xsl:text>
127 <xsl:value-of select="$color"/>
128 <xsl:text>}</xsl:text>
129 </xsl:otherwise>
130 </xsl:choose>
131 </xsl:template>
132
133
134 <!-- Just for a unit-test like that:
135 <u>
136 <rgb>CCFFEE</rgb>
137 <rgb>DD7E67</rgb>
138 <rgb>7A33</rgb>
139 <color>#d5</color>
140 <color>#7A33</color>
141 <color>#3a7a33</color>
142 <color>#3a3a3a</color>
143 <color>#cccccc</color>
144 <color>red</color>
145 <color>{blue}</color>
146 <color>[gray]{0.8}</color>
147 </u>
148 -->
149 <xsl:template match="rgb">
150 <xsl:call-template name="hex-to-rgb">
151 <xsl:with-param name="hex" select="."/>
152 </xsl:call-template>
153 </xsl:template>
154
155 <xsl:template match="color">
156 <xsl:call-template name="get-color">
157 <xsl:with-param name="color" select="."/>
158 </xsl:call-template>
159 </xsl:template>
160
161 </xsl:stylesheet>