xslt根据背景颜色亮度更改文本

时间:2017-04-20 17:40:21

标签: xslt xslt-1.0

在xsl中是否有办法根据背景颜色亮度将文字颜色更改为黑色或白色?

因此,在下面的示例中,基于背景颜色亮度,两个问候都应该有白色文本。在xsl中是否有内置函数来实现此目的?

XSL

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html"/>

  <xsl:template match="greetings">
    <xsl:apply-templates select="greeting"/>
  </xsl:template>

  <xsl:template match="greeting">
    <html>
      <body>
        <h1>
          <span>
            <xsl:attribute name="style">
              background-color: <xsl:value-of select="@backcolor"/>
              color: <!-- Is there a way to make color black or white based on background color brightness? -->
            </xsl:attribute>
            <xsl:value-of select="."/>
          </span>
        </h1>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

XML

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="colortest.xslt"?> <!--todo: change this if copying to new file-->
<!--todo: change preceding line if copying to new file-->
<greetings>
  <greeting id="1" backcolor="f59595">
    Hello World!
  </greeting>
  <greeting id="2" backcolor="ff0000">
    Hola!
  </greeting>
</greetings>

2 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xml" href="XSLTFile2.xslt"?>
<greetings>
  <greeting id="1" backcolor="f59595">
    Hello World!
  </greeting>
  <greeting id="2" backcolor="ff0000">
    Hola!
  </greeting>
</greetings>

好的,如果您想使用css来控制背景颜色,请在XML中使用xml-stylesheet将其指向执行以下操作的XSLT文件。此代码将基于listItemColor变量选择正确的样式表,并使用正确的css样式表替换XML中的xml样式表。 (虽然我仍然不知道listItemColor如何获得它的价值。也许你可以告诉我们。)

创建两个样式表,一个用于灯光,一个用于黑暗。

以下是XSLTFile2.xslt文件:

<xsl:variable name="listItemColor" select="'b'"/>

<xsl:template match="processing-instruction('xml-stylesheet')">
  <xsl:choose>
    <xsl:when test="listItemColor ='a'">
      <xsl:processing-instruction name="xml-stylesheet">
        <xsl:text>type="text/css" href="colortest.css"</xsl:text>
      </xsl:processing-instruction>
    </xsl:when>
    <xsl:otherwise>
      <xsl:processing-instruction name="xml-stylesheet">
        <xsl:text>type="text/css" href="colortest2.css"</xsl:text>
      </xsl:processing-instruction>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- Do any other processing. -->

<!-- Identity template.-->
<xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

答案 1 :(得分:0)

你必须弄清楚&#39;亮度&#39; RGB背景颜色的值,然后对其进行阈值以确定前景色是需要是黑色还是白色(以获得最佳对比度)。 不幸的是,XSLT(和XPath)没有提供任何处理颜色的功能 - 并且看到你正在坚持使用XSLT 1.0(我假设 - 因为你在浏览器中使用它?)事情变得有点冗长......但仍有可能......

尝试这样的事情......

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:variable name="vHexDigits" select="'0123456789abcdef'"/>

<xsl:template match="greetings">
    <html>
        <body>
            <xsl:apply-templates select="greeting"/>
        </body>
    </html>
</xsl:template>

<xsl:template match="greeting">
    <xsl:variable name="vLuminance">
        <xsl:call-template name="RGB2Luminance">
            <xsl:with-param name="pRed" select="(string-length(substring-before($vHexDigits, substring(@backcolor,1,1))) * 16) + string-length(substring-before($vHexDigits, substring(@backcolor,2,1)))"/>
            <xsl:with-param name="pGreen" select="(string-length(substring-before($vHexDigits, substring(@backcolor,3,1))) * 16) + string-length(substring-before($vHexDigits, substring(@backcolor,4,1)))"/>
            <xsl:with-param name="pBlue" select="(string-length(substring-before($vHexDigits, substring(@backcolor,5,1))) * 16) + string-length(substring-before($vHexDigits, substring(@backcolor,6,1)))"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="vForegroundColor">
        <xsl:choose>
            <!-- assume our black/white forground threshold is 0.5 -->
            <xsl:when test="$vLuminance &gt; 0.5">
                <xsl:text>black</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>white</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <h1>
        <span style="background-color: {@backcolor}; color: {$vForegroundColor};">
            <xsl:value-of select="."/>
        </span>
    </h1>
</xsl:template>

<xsl:template name="RGB2Luminance">
    <xsl:param name="pRed"/>
    <xsl:param name="pGreen"/>
    <xsl:param name="pBlue"/>
    <xsl:variable name="vR" select="$pRed div 255"/>
    <xsl:variable name="vG" select="$pGreen div 255"/>
    <xsl:variable name="vB" select="$pBlue div 255"/>
    <xsl:variable name="vMax">
        <xsl:choose>
            <xsl:when test="$vR &gt;= $vG and $vR &gt;= $vB">
                <xsl:value-of select="$vR"/>
            </xsl:when>
            <xsl:when test="$vG &gt;= $vR and $vG &gt;= $vB">
                <xsl:value-of select="$vG"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$vB"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="vMin">
        <xsl:choose>
            <xsl:when test="$vR &lt;= $vG and $vR &lt;= $vB">
                <xsl:value-of select="$vR"/>
            </xsl:when>
            <xsl:when test="$vG &lt;= $vR and $vG &lt;= $vB">
                <xsl:value-of select="$vG"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$vB"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:value-of select="($vMax + $vMin) div 2"/>
</xsl:template>
</xsl:stylesheet>
相关问题