Skip to content

Instantly share code, notes, and snippets.

@nils-werner
Created December 16, 2010 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nils-werner/743285 to your computer and use it in GitHub Desktop.
Save nils-werner/743285 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl">
<xsl:template name="rbga-to-hex">
<xsl:param name="rgba-val"/>
<xsl:param name="count" select="1"/>
<xsl:variable name="val" select="substring-before($rgba-val,',')"/>
<xsl:variable name="tail" select="substring-after($rgba-val,concat($val,','))"/>
<xsl:choose>
<xsl:when test="$count &lt; 4">
<xsl:call-template name="to-hex">
<xsl:with-param name="val" select="$val"/>
</xsl:call-template>
<xsl:call-template name="rbga-to-hex">
<xsl:with-param name="count" select="$count + 1"/>
<xsl:with-param name="rgba-val" select="$tail"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="to-hex">
<xsl:with-param name="val" select="$val"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="to-hex">
<xsl:param name="val"/>
<xsl:param name="max" select="255"/>
<xsl:param name="min" select="0"/>
<xsl:param name="hex-key" select="'0123456789ABCDEF'"/>
<!-- REMOVE NON-NUMERIC CHARACTERS -->
<xsl:variable name="val"
select="translate($val,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,.-_=+!@#$%^*() ','')"/>
<!-- insure that the rgb value is within 0-255 -->
<xsl:variable name="num">
<xsl:choose>
<xsl:when test="$val &gt; $max">
<xsl:value-of select="$max"/>
</xsl:when>
<xsl:when test="$val &lt; $min">
<xsl:value-of select="$min"/>
</xsl:when>
<!-- insure that we have whole numbers -->
<xsl:otherwise>
<xsl:value-of select="round($val)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Return Hex Val -->
<!-- substring(string, position, length) -->
<xsl:value-of select="concat( substring($hex-key,(ceiling(($num - ceiling($num mod 16)) div 16)+1),1),
substring($hex-key,(ceiling(($num - ceiling($num mod 16)) div 16)+1),1)
)"/>
</xsl:template>
<xsl:template name="hex-to-rgb">
<xsl:param name="hex-val"/>
<xsl:param name="count" select="1"/>
<xsl:variable name="hex-val" select="translate($hex-val,'abcdef#','ABCDEF')"/>
<xsl:variable name="val" select="substring($hex-val,1,2)"/>
<xsl:variable name="tail" select="substring-after($hex-val,$val)"/>
<xsl:choose>
<xsl:when test="$count &lt; 3">
<xsl:call-template name="to-rgb">
<xsl:with-param name="val" select="$val"/>
</xsl:call-template><xsl:text>, </xsl:text>
<xsl:call-template name="hex-to-rgb">
<xsl:with-param name="count" select="$count + 1"/>
<xsl:with-param name="hex-val" select="$tail"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="to-rgb">
<xsl:with-param name="val" select="$val"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="to-rgb">
<xsl:param name="val"/>
<xsl:param name="hex-key" select="'0123456789ABCDEF'"/>
<!-- Return decimal Val -->
<!-- substring(string, position, length) -->
<xsl:value-of select="string-length(substring-before($hex-key,substring($val,1,1)))*16 + string-length(substring-before($hex-key,substring($val,2,1)))" />
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment