<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/2000/svg"
                xmlns:param="http://foo"
                version="1.0">

  <xsl:output indent="yes"/>

  <param:colors>
    <param:color>red</param:color>
    <param:color>green</param:color>
    <param:color>blue</param:color>
    <param:color>yellow</param:color>
    <param:color>purple</param:color>
    <param:color>grey</param:color>
    <param:color>pink</param:color>
  </param:colors>

  <xsl:param name="l" select="1"/>

  <xsl:template match="/">
    <svg viewBox="-1 -1 2 2">
      <circle cx="0" cy="0" r="1" fill="black"/>
      <circle cx="-.5" cy="0" r=".5" fill="{document('')/*/param:colors/param:color[1]}"/>
      <circle cx=" .5" cy="0" r=".5" fill="{document('')/*/param:colors/param:color[1]}"/>
      <circle cx="0" cy="-.66666666666" r=".333333333" fill="{document('')/*/param:colors/param:color[1]}"/>
      <xsl:if test="$l > 0">
        <xsl:call-template name="dive">
          <xsl:with-param name="n"   select="1"/>
          
          <xsl:with-param name="b1"  select="15"/>
          <xsl:with-param name="bx1" select="0"/>
          <xsl:with-param name="by1" select="-4"/>
          
          <xsl:with-param name="b2"  select="2"/>
          <xsl:with-param name="bx2" select="-1"/>
          <xsl:with-param name="by2" select="0"/>
          
          <xsl:with-param name="b3"  select="2"/>
          <xsl:with-param name="bx3" select="1"/>
          <xsl:with-param name="by3" select="0"/>
          
          <xsl:with-param name="b4"  select="3"/>
          <xsl:with-param name="bx4" select="0"/>
          <xsl:with-param name="by4" select="-2"/>
        </xsl:call-template>
        <xsl:call-template name="dive">
          <xsl:with-param name="n"   select="1"/>
          
          <xsl:with-param name="b1"  select="6"/>
          <xsl:with-param name="bx1" select="3"/>
          <xsl:with-param name="by1" select="-4"/>
          
          <xsl:with-param name="b2"  select="2"/>
          <xsl:with-param name="bx2" select="1"/>
          <xsl:with-param name="by2" select="0"/>
          
          <xsl:with-param name="b3"  select="3"/>
          <xsl:with-param name="bx3" select="0"/>
          <xsl:with-param name="by3" select="-2"/>
          
          <xsl:with-param name="b4"  select="-1"/>
          <xsl:with-param name="bx4" select="0"/>
          <xsl:with-param name="by4" select="0"/>
        </xsl:call-template>
        <xsl:call-template name="dive">
          <xsl:with-param name="n"   select="1"/>
          
          <xsl:with-param name="b1"  select="6"/>
          <xsl:with-param name="bx1" select="-3"/>
          <xsl:with-param name="by1" select="-4"/>
          
          <xsl:with-param name="b2"  select="3"/>
          <xsl:with-param name="bx2" select="0"/>
          <xsl:with-param name="by2" select="-2"/>
          
          <xsl:with-param name="b3"  select="-1"/>
          <xsl:with-param name="bx3" select="0"/>
          <xsl:with-param name="by3" select="0"/>
          
          <xsl:with-param name="b4"  select="2"/>
          <xsl:with-param name="bx4" select="-1"/>
          <xsl:with-param name="by4" select="0"/>
        </xsl:call-template>
        <xsl:call-template name="dive">
          <xsl:with-param name="n"   select="1"/>
          
          <xsl:with-param name="b1"  select="3"/>
          <xsl:with-param name="bx1" select="0"/>
          <xsl:with-param name="by1" select="2"/>
          
          <xsl:with-param name="b2"  select="-1"/>
          <xsl:with-param name="bx2" select="0"/>
          <xsl:with-param name="by2" select="0"/>
          
          <xsl:with-param name="b3"  select="2"/>
          <xsl:with-param name="bx3" select="-1"/>
          <xsl:with-param name="by3" select="0"/>
          
          <xsl:with-param name="b4"  select="2"/>
          <xsl:with-param name="bx4" select="1"/>
          <xsl:with-param name="by4" select="0"/>
        </xsl:call-template>
      </xsl:if>
    </svg>
  </xsl:template>


  <xsl:template name="dive">
    <xsl:param name="n"   select="0"/>
    <xsl:param name="b1"  select="0"/>
    <xsl:param name="bx1" select="0"/>
    <xsl:param name="by1" select="0"/>
    <xsl:param name="b2"  select="0"/>
    <xsl:param name="bx2" select="0"/>
    <xsl:param name="by2" select="0"/>
    <xsl:param name="b3"  select="0"/>
    <xsl:param name="bx3" select="0"/>
    <xsl:param name="by3" select="0"/>
    <xsl:param name="b4"  select="0"/>
    <xsl:param name="bx4" select="0"/>
    <xsl:param name="by4" select="0"/>

    <xsl:variable name="color" select="document('')/*/param:colors/param:color[1+($n mod count(document('')/*/param:colors/param:color))]"/>
    <!--    <xsl:message><xsl:value-of select="concat($n,' ',count(document('')/*/param:colors/param:color),' = ',1 + ($n mod count(document('')/*/param:colors/param:color)),' - ',$color)"/></xsl:message> -->

    <xsl:comment> <xsl:value-of select="concat($n,': ',$b1,' (',$bx1,', ',$bx2,')')"/> </xsl:comment>
    <circle cx="{$bx1 div $b1}" cy="{$by1 div $b1}" r="{1 div $b1}" fill="{$color}"/>

    <xsl:if test="$n &lt; $l">
      <xsl:call-template name="dive">
        <xsl:with-param name="n"   select="$n+1"/>
        
        <xsl:with-param name="b1"  select="2*($b1+$b3+$b4)-$b2"/>
        <xsl:with-param name="bx1" select="2*($bx1+$bx3+$bx4)-$bx2"/>
        <xsl:with-param name="by1" select="2*($by1+$by3+$by4)-$by2"/>
        
        <xsl:with-param name="b2"  select="$b3"/>
        <xsl:with-param name="bx2" select="$bx3"/>
        <xsl:with-param name="by2" select="$by3"/>
        
        <xsl:with-param name="b3"  select="$b4"/>
        <xsl:with-param name="bx3" select="$bx4"/>
        <xsl:with-param name="by3" select="$by4"/>
        
        <xsl:with-param name="b4"  select="$b1"/>
        <xsl:with-param name="bx4" select="$bx1"/>
        <xsl:with-param name="by4" select="$by1"/>
      </xsl:call-template>
    <xsl:call-template name="dive">
      <xsl:with-param name="n"   select="$n+1"/>

      <xsl:with-param name="b1"  select="2*($b1+$b2+$b4)-$b3"/>
      <xsl:with-param name="bx1" select="2*($bx1+$bx2+$bx4)-$bx3"/>
      <xsl:with-param name="by1" select="2*($by1+$by2+$by4)-$by3"/>

      <xsl:with-param name="b2"  select="$b4"/>
      <xsl:with-param name="bx2" select="$bx4"/>
      <xsl:with-param name="by2" select="$by4"/>

      <xsl:with-param name="b3"  select="$b1"/>
      <xsl:with-param name="bx3" select="$bx1"/>
      <xsl:with-param name="by3" select="$by1"/>

      <xsl:with-param name="b4"  select="$b2"/>
      <xsl:with-param name="bx4" select="$bx2"/>
      <xsl:with-param name="by4" select="$by2"/>
    </xsl:call-template>
    <xsl:call-template name="dive">
      <xsl:with-param name="n"   select="$n+1"/>

      <xsl:with-param name="b1"  select="2*($b1+$b2+$b3)-$b4"/>
      <xsl:with-param name="bx1" select="2*($bx1+$bx2+$bx3)-$bx4"/>
      <xsl:with-param name="by1" select="2*($by1+$by2+$by3)-$by4"/>

      <xsl:with-param name="b2"  select="$b1"/>
      <xsl:with-param name="bx2" select="$bx1"/>
      <xsl:with-param name="by2" select="$by1"/>

      <xsl:with-param name="b3"  select="$b2"/>
      <xsl:with-param name="bx3" select="$bx2"/>
      <xsl:with-param name="by3" select="$by2"/>

      <xsl:with-param name="b4"  select="$b3"/>
      <xsl:with-param name="bx4" select="$bx3"/>
      <xsl:with-param name="by4" select="$by3"/>
    </xsl:call-template>

    </xsl:if>

  </xsl:template>

</xsl:stylesheet>
