<?xml version="1.0" encoding="UTF-8"?>

<!-- cxf.xsl © 2016 by Russell Cottrell.
     Formats .cxf files generated at russellcottrell.com/photo/optimization/. -->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:cc="http://colorexchangeformat.com/CxF3-core"
  xmlns:rc="http://www.russellcottrell.com/photo/optimization"
  xmlns:xrp="http://www.xrite.com/products/prism">
<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>

<xsl:template match="/">
  <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
      <meta http-equiv="content-language" content="en-us"/>
      <title>.cxf File</title>

      <script>

        var cWidth = 770;	// default patch set width
        var pWidth = 70;	// default patch width
        var cW, pW;
        var tStr = "";

        window.onload = function() {
          if (tStr != "")
            document.title = tStr;

          var containerDiv = document.getElementById("containerD");
          if (isNaN(cW))
            cW = cWidth;
          containerDiv.style.width = cW + "px";
          var patchDiv = containerDiv.firstChild.cloneNode(true);
          containerDiv.innerHTML = "";
          var divs = document.getElementsByClassName("c");
          if (isNaN(pW)) {
            pW = Math.floor(cW / Math.ceil(Math.sqrt(divs.length-1)));	// patch size
            pW = Math.min(pW, pWidth); }
          for (var i=0; i&lt;divs.length; i++) {
            newDiv = patchDiv.cloneNode(true);
            with (newDiv.style) {
              height = (pW-1) + "px";
              width = (pW-1) + "px";
              backgroundColor = divs[i].style.backgroundColor; }
            containerDiv.appendChild(newDiv); }
        }

      </script>

      <style>
        body  { margin: 2em; background-color: #f9f9f9; color: black }
        table  { border-collapse: collapse }
        th, td  { border: 1px solid #999999; padding: 0.25em 0.75em }
        th  { background-color: #dddddd }
        td  { text-align: right; background-color: #ffffff }
        th, .c, .w  { text-align: center }
        .w  { color: #999999 }
        .lin  { background-color: transparent; border-width: 0px; font-height: 1px; padding: 0px; width: 2px }
        .pDiv  { float: left; font-height: 1px; border: 1px solid gray; margin: 0px -1px -1px 0px }
      </style>

    </head>
    <body>
      <xsl:apply-templates select="cc:CxF/cc:CustomResources/rc:Params/rc:CustomAttributes"/>
      <xsl:apply-templates select="cc:CxF/cc:FileInformation"/>
      <xsl:apply-templates select="cc:CxF/cc:CustomResources/xrp:Prism"/>
      <xsl:apply-templates select="cc:CxF/cc:Resources/cc:ObjectCollection"/>
      <p>Please visit <a href="http://www.russellcottrell.com/photo/optimization/">russellcottrell.com</a>.</p>
    </body>
  </html>
</xsl:template>



<xsl:template match="cc:CxF/cc:CustomResources/rc:Params/rc:CustomAttributes">
  <span id="cWSpan" style="display: none">
    <xsl:value-of select="@PageWidth"/>
  </span>
  <span id="pWSpan" style="display: none">
    <xsl:value-of select="@PatchSizeWidthValue"/>
  </span>
  <script>
    cW = parseInt(document.getElementById("cWSpan").innerHTML);
    pW = parseInt(document.getElementById("pWSpan").innerHTML);
  </script>
</xsl:template>




<xsl:template match="cc:CxF/cc:FileInformation">
  <p>
    <xsl:for-each select="cc:Tag">
      <xsl:choose>
        <xsl:when test="self::node()[@Name!='Memo']">
          <xsl:value-of select="@Name"/>:&#160; <xsl:value-of select="@Value"/><br/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@Value"/><br/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </p>
  <span id="titleSpan" style="display: none">
    <xsl:value-of select="cc:Tag[@Name='Source']/@Value"/>
  </span>
  <script>
    tStr = document.getElementById("titleSpan").innerHTML;
  </script>
</xsl:template>



<xsl:template match="cc:CxF/cc:CustomResources/xrp:Prism">
  <p>

    <xsl:if test="/cc:CxF/cc:FileInformation/cc:Creator">
      Creator:&#160; <xsl:value-of select="/cc:CxF/cc:FileInformation/cc:Creator"/><br/>
    </xsl:if>

    <xsl:if test="ProfileSettings/@DeviceType">
      DeviceType:&#160; <xsl:value-of select="ProfileSettings/@DeviceType"/><br/>
    </xsl:if>

    <xsl:if test="xrp:CustomAttributes/@ColorSpace">
      ColorSpace:&#160; <xsl:value-of select="xrp:CustomAttributes/@ColorSpace"/><br/>
    </xsl:if>

    <xsl:if test="xrp:CustomAttributes/@OptimizeProfile">
          OptimizeProfile:&#160; <xsl:value-of select="xrp:CustomAttributes/@OptimizeProfile"/><br/>
        </xsl:if>

          <xsl:if test="ProfileSettings/WhitePointCIEIlluminant">
            WhitePointCIEIlluminant:&#160; <xsl:value-of select="name(ProfileSettings/WhitePointCIEIlluminant/*[1])"/><br/>
          </xsl:if>

          <xsl:if test="ProfileSettings/StandardGamma">
            StandardGamma:&#160; <xsl:value-of select="ProfileSettings/StandardGamma/@value"/><br/>
          </xsl:if>

          <xsl:if test="ProfileSettings/LuminanceType/TargetLuminosityLevel">
            TargetLuminosityLevel:&#160; <xsl:value-of select="ProfileSettings/LuminanceType/TargetLuminosityLevel/@value"/><br/>
          </xsl:if>

          <xsl:if test="ProfileSettings/ProfileType">
            ProfileType:&#160; <xsl:value-of select="ProfileSettings/ProfileType/@value"/><br/>
          </xsl:if>

<!--
<br/>
<xsl:for-each select="xrp:CustomAttributes/@*">
  <xsl:value-of select="name()"/>:&#160; <xsl:value-of select="."/><br/>
</xsl:for-each>
-->

  </p>
</xsl:template>



<xsl:template match="cc:CxF/cc:Resources/cc:ObjectCollection">

  <xsl:variable name="LCh" select="boolean(cc:Object/cc:TagCollection[@Name='ColorLCh'])"/>
  <xsl:variable name="HSL" select="boolean(cc:Object/cc:TagCollection[@Name='ColorHSL'])"/>
  <xsl:variable name="HSB" select="boolean(cc:Object/cc:TagCollection[@Name='ColorHSB'])"/>
  <xsl:variable name="RGB" select="boolean(cc:Object/cc:DeviceColorValues/cc:ColorRGB)"/>
  <xsl:variable name="Lab" select="boolean(cc:Object/cc:ColorValues/cc:ColorCIELab)"/>

  <table border="0" cellPadding="0" cellSpacing="0">

    <tr>
      <th class="lin">&#160;</th>
      <xsl:if test="$LCh">
        <th class="lin">&#160;</th>
        <th colSpan="3">L C h</th>
      </xsl:if>
      <xsl:if test="$HSL">
        <th class="lin">&#160;</th>
        <th colSpan="3">H S L</th>
      </xsl:if>
      <xsl:if test="$HSB">
        <th class="lin">&#160;</th>
        <th colSpan="3">H S B</th>
      </xsl:if>
      <xsl:if test="$RGB">
        <th class="lin">&#160;</th>
        <th colSpan="3">R G B</th>
      </xsl:if>
      <xsl:if test="$Lab">
        <th class="lin">&#160;</th>
        <th colSpan="3">L a b</th>
      </xsl:if>
    </tr>

    <tr>
      <th>No.</th>
      <xsl:if test="$LCh">
        <th class="lin">&#160;</th>
        <th>L</th>
        <th>C</th>
        <th>h</th>
      </xsl:if>
      <xsl:if test="$HSL">
        <th class="lin">&#160;</th>
        <th>H</th>
        <th>S</th>
        <th>L</th>
      </xsl:if>
      <xsl:if test="$HSB">
        <th class="lin">&#160;</th>
        <th>H</th>
        <th>S</th>
        <th>B</th>
      </xsl:if>
      <xsl:if test="$RGB">
        <th class="lin">&#160;</th>
        <th>R</th>
        <th>G</th>
        <th>B</th>
      </xsl:if>
      <xsl:if test="$Lab">
        <th class="lin">&#160;</th>
        <th>L*</th>
        <th>a*</th>
        <th>b*</th>
      </xsl:if>
    </tr>

    <xsl:for-each select="cc:Object">
      <tr>

        <xsl:variable name="xL">
          <xsl:choose>
          <xsl:when test="cc:ColorValues/cc:ColorCIELab/cc:L">
            <xsl:value-of select="cc:ColorValues/cc:ColorCIELab/cc:L"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="(cc:DeviceColorValues/cc:ColorRGB/cc:R * 0.299 +
              cc:DeviceColorValues/cc:ColorRGB/cc:G * 0.587 +
              cc:DeviceColorValues/cc:ColorRGB/cc:B * 0.114) div 2.55"/>
          </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>

        <xsl:variable name="xn">
          <xsl:value-of select="substring-after(@Id,'c')"/>
        </xsl:variable>

        <xsl:variable name="xc">
          <xsl:choose>
          <xsl:when test="cc:DeviceColorValues/cc:ColorHTML">
            <xsl:value-of select="concat('#', cc:DeviceColorValues/cc:ColorHTML/@HTML)"/>
          </xsl:when>
          <xsl:when test="cc:DeviceColorValues/cc:ColorRGB">
            <xsl:value-of select="concat('rgb(', cc:DeviceColorValues/cc:ColorRGB/cc:R, ',',
              cc:DeviceColorValues/cc:ColorRGB/cc:G, ',',
              cc:DeviceColorValues/cc:ColorRGB/cc:B, ')')"/>
          </xsl:when>
          </xsl:choose>
        </xsl:variable>




        <xsl:choose>
          <xsl:when test="$RGB and not(cc:DeviceColorValues/cc:ColorRGB)"> <!-- RGB with Lab mixed in -->
            <td class="w"><xsl:value-of select="$xn"/></td>
          </xsl:when>
          <xsl:when test="$xL &lt; 50">
            <td class="c" style="color:white; background-color: {$xc}"><xsl:value-of select="$xn"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td class="c" style="background-color: {$xc}"><xsl:value-of select="$xn"/></td>
          </xsl:otherwise>
        </xsl:choose>



        <xsl:if test="cc:TagCollection/@Name='ColorLCh'">
          <xsl:for-each select="cc:TagCollection[@Name='ColorLCh']">
            <td class="lin">&#160;</td>
            <td><xsl:value-of select="cc:Tag[@Name='L']/@Value"/></td>
            <td><xsl:value-of select="cc:Tag[@Name='C']/@Value"/></td>
            <td><xsl:value-of select="cc:Tag[@Name='h']/@Value"/></td>
          </xsl:for-each>
        </xsl:if>

        <xsl:if test="cc:TagCollection/@Name='ColorHSL'">
          <xsl:for-each select="cc:TagCollection[@Name='ColorHSL']">
            <td class="lin">&#160;</td>
            <td><xsl:value-of select="cc:Tag[@Name='H']/@Value"/></td>
            <td><xsl:value-of select="cc:Tag[@Name='S']/@Value"/></td>
            <td><xsl:value-of select="cc:Tag[@Name='L']/@Value"/></td>
          </xsl:for-each>
        </xsl:if>

        <xsl:if test="cc:TagCollection/@Name='ColorHSB'">
          <xsl:for-each select="cc:TagCollection[@Name='ColorHSB']">
            <td class="lin">&#160;</td>
            <td><xsl:value-of select="cc:Tag[@Name='H']/@Value"/></td>
            <td><xsl:value-of select="cc:Tag[@Name='S']/@Value"/></td>
            <td><xsl:value-of select="cc:Tag[@Name='B']/@Value"/></td>
          </xsl:for-each>
        </xsl:if>

        <xsl:if test="$RGB">
          <xsl:choose>
            <xsl:when test="cc:DeviceColorValues/cc:ColorRGB">
              <xsl:for-each select="cc:DeviceColorValues/cc:ColorRGB">
                <td class="lin">&#160;</td>
                <td><xsl:value-of select="cc:R"/></td>
                <td><xsl:value-of select="cc:G"/></td>
                <td><xsl:value-of select="cc:B"/></td>
              </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
                <td class="lin">&#160;</td>
                <td>&#160;</td>
                <td>&#160;</td>
                <td>&#160;</td>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>

        <xsl:if test="$Lab">
          <xsl:choose>
            <xsl:when test="cc:ColorValues/cc:ColorCIELab">
              <xsl:for-each select="cc:ColorValues/cc:ColorCIELab">
                <td class="lin">&#160;</td>
                <td><xsl:value-of select="cc:L"/></td>
                <td><xsl:value-of select="cc:A"/></td>
                <td><xsl:value-of select="cc:B"/></td>
              </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
              <td class="lin">&#160;</td>
              <td>&#160;</td>
              <td>&#160;</td>
              <td>&#160;</td>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>

      </tr>
    </xsl:for-each>
  </table>

  <br/>

  <div id="containerD"><div class="pDiv">&#160;</div></div>

  <div style="clear: both"></div>



</xsl:template>



</xsl:stylesheet>
