Example 16-7: Template for a rainbow of cool-colored bars

<!-- CoolColor.xsl: Return HTML #RRGGBB color for "cool" color combination -->
<xsl:stylesheet version="1.0" exclude-result-prefixes="Color Int Mayura"
     xmlns:xsl    ="http://www.w3.org/1999/XSL/Transform"
     xmlns:Color  ="http://www.oracle.com/XSL/Transform/java/java.awt.Color"
     xmlns:Int    ="http://www.oracle.com/XSL/Transform/java/java.lang.Integer"
     xmlns:Mayura ="http://www.mayura.com/">
<!--
 | This stylesheet implements a "CoolColor" algorithm that Rajeev, the
 | creator of the shareware SVG editor "Mayura Draw" sent me.
 |
 | Given a total number of colors T and an index N between 1 and T
 | the CoolColor algorithm determines an eye-pleasing color combination
 | for the range of T different colors, and returns the HTML #RRGGBB
 | hex representation of the N-th color in the range.
 +-->
  <xsl:template name="Mayura:CoolColor">
    <!-- Accept a color index and the total number of colors in the range -->
    <xsl:param name="colorIndex"/>
    <xsl:param name="totalColors"/>
    <!-- These are the magic values of saturation and brightness -->
    <xsl:variable name="SAT" select="number(0.6)"/>
    <xsl:variable name="BRT" select="number(0.9)"/>
    <!-- Calculate "r","g","b" values for the 'colorIndex'-th color in range -->
    <xsl:variable name="hue" select="$colorIndex div $totalColors"/>
    <!--
     | Use the public static getHSBColor method on java.awt.Color
     |
     | NOTE: This returns a Java object of the Color class as a return value
     |       into the XSLT variable named "c".
     +-->
    <xsl:variable name="c"   select="Color:getHSBColor($hue, $SAT, $BRT)"/>
    <!--
     | Pass the instance of a java.awt.Color object to three other
     | methods on java.awt.Color to extract the r,g,b values separately
     +-->
    <xsl:variable name="r"   select="Color:getRed($c)"/>
    <xsl:variable name="g"   select="Color:getGreen($c)"/>
    <xsl:variable name="b"   select="Color:getBlue($c)"/>
    <!--
     | Use the public static toHexString() method on java.lang.integer
     | to convert the integer color numbers to Hex for HTML #RRGGBB value.
     +-->
    <xsl:variable name="rs"  select="Int:toHexString($r)"/>
    <xsl:variable name="gs"  select="Int:toHexString($g)"/>
    <xsl:variable name="bs"  select="Int:toHexString($b)"/>
    <!-- If any of r, b, or b values was less than 16, add a leading 0 -->
    <xsl:if test="$r &lt; 16">0</xsl:if><xsl:value-of select="$rs"/>
    <xsl:if test="$g &lt; 16">0</xsl:if><xsl:value-of select="$gs"/>
    <xsl:if test="$b &lt; 16">0</xsl:if><xsl:value-of select="$bs"/>
  </xsl:template>
</xsl:stylesheet>