color.xsl coverage = 88/117 (75.21%)

  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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>

<!--############################################################################
    XSLT Stylesheet DocBook -> LaTeX 
    ############################################################################ -->

<!-- Convert hexadecimal format to decimal -->
<xsl:template name="hex-to-int">
  <xsl:param name="hex" select="'0'"/>

  <xsl:choose>
  <xsl:when test="string-length($hex)=1">
    <xsl:call-template name="char-to-int">
      <xsl:with-param name="char" select="$hex"/>
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:variable name="low.int">
      <xsl:call-template name="char-to-int">
        <xsl:with-param name="char"
                        select="substring($hex, string-length($hex))"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="high.int">
      <xsl:call-template name="hex-to-int">
        <xsl:with-param name="hex"
                        select="substring($hex, 1, string-length($hex)-1)"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$low.int + 16*$high.int"/>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="char-to-int">
  <xsl:param name="char" select="'0'"/>
  <xsl:variable name="c">
    <xsl:value-of select="translate($char, 'abcdef', 'ABCDEF')"/>
  </xsl:variable>

  <xsl:value-of select="string-length(substring-before('0123456789ABCDEF',
                                      $c))"/>
</xsl:template>

<!-- Convert xxyyzz to rate(xx),rate(yy),rate(zz) -->
<xsl:template name="hex-to-rgb">
  <xsl:param name="hex" select="'0'"/>
  <xsl:choose>
  <xsl:when test="string-length($hex) &lt;= 2">
    <xsl:call-template name="hex-to-rate">
      <xsl:with-param name="hex" select="$hex"/>
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:variable name="right">
      <xsl:call-template name="hex-to-rate">
        <xsl:with-param name="hex"
                        select="substring($hex, string-length($hex)-1)"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="left">
      <xsl:call-template name="hex-to-rgb">
        <xsl:with-param name="hex"
                        select="substring($hex, 1, string-length($hex)-2)"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$left"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="$right"/>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- Convert 0xCC to 0.8 = 204/255 -->
<xsl:template name="hex-to-rate">
  <xsl:param name="hex" select="'0'"/>
  <xsl:variable name="int">
    <xsl:call-template name="hex-to-int">
      <xsl:with-param name="hex" select="$hex"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:value-of select="round(($int * 100) div 255) div 100"/>
</xsl:template>


<!-- Convert color to a proper latex format:
     #xxyyzz   => [rgb]{u,v,w}
     #xaxaxa   => [gray]{u}
     xxx       => {xxx}
     {xxx}     => {xxx}
     [aa]{xxx} => [aa]{xxx}
-->
<xsl:template name="get-color">
  <xsl:param name="color"/>
  <xsl:choose>
  <xsl:when test="starts-with($color, '#')">
    <xsl:variable name="fullcolor"
                  select="concat('000000', substring-after($color, '#'))"/>
    <xsl:variable name="rcolor"
                  select="substring($fullcolor, string-length($fullcolor)-5)"/>
    <xsl:variable name="r" select="substring($rcolor,1,2)"/>
    <xsl:variable name="g" select="substring($rcolor,3,2)"/>
    <xsl:variable name="b" select="substring($rcolor,5,2)"/>
    <xsl:choose>
    <xsl:when test="$r=$g and $g=$b">
      <xsl:text>[gray]{</xsl:text>
      <xsl:call-template name="hex-to-rate">
        <xsl:with-param name="hex" select="$r"/>
      </xsl:call-template>
      <xsl:text>}</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>[rgb]{</xsl:text>
      <xsl:call-template name="hex-to-rgb">
        <xsl:with-param name="hex" select="$rcolor"/>
      </xsl:call-template>
      <xsl:text>}</xsl:text>
    </xsl:otherwise>
    </xsl:choose>
  </xsl:when>
  <xsl:when test="starts-with($color, '[') or starts-with($color, '{')">
    <xsl:value-of select="$color"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:text>{</xsl:text>
    <xsl:value-of select="$color"/>
    <xsl:text>}</xsl:text>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- Just for a unit-test like that:
     <u>
       <rgb>CCFFEE</rgb>
       <rgb>DD7E67</rgb>
       <rgb>7A33</rgb>
       <color>#d5</color>
       <color>#7A33</color>
       <color>#3a7a33</color>
       <color>#3a3a3a</color>
       <color>#cccccc</color>
       <color>red</color>
       <color>{blue}</color>
       <color>[gray]{0.8}</color>
     </u>
-->
<xsl:template match="rgb">
  <xsl:call-template name="hex-to-rgb">
    <xsl:with-param name="hex" select="."/>
  </xsl:call-template>
</xsl:template>

<xsl:template match="color">
  <xsl:call-template name="get-color">
    <xsl:with-param name="color" select="."/>
  </xsl:call-template>
</xsl:template>

</xsl:stylesheet>