xslt基于背景颜色亮度更改文本
xsl中是否有一种方法可以根据背景颜色的亮度将文本颜色更改为黑色或白色 因此,在下面的示例中,基于背景颜色亮度,两个问候语都应该有白色文本。xsl中是否有内置函数来实现这一点 xslxslt基于背景颜色亮度更改文本,xslt,xslt-1.0,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: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 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>
背景色:
颜色:
xml
<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 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>
你好,世界!
你好!
你好,世界!
你好!
好的,如果您想使用css控制背景颜色,请在xml中使用xml样式表将其指向执行以下操作的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>
type=“text/css”href=“colortest.css”
type=“text/css”href=“colortest2.css”
您必须计算出RGB背景色的“亮度”值,然后在该值上设置一个阈值,以确定前景色是否需要为黑色或白色(以获得最佳对比度)。
不幸的是,XSLT(和XPath)并没有提供任何处理颜色的函数——而且鉴于您一直使用XSLT1.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 > 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 >= $vG and $vR >= $vB">
<xsl:value-of select="$vR"/>
</xsl:when>
<xsl:when test="$vG >= $vR and $vG >= $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 <= $vG and $vR <= $vB">
<xsl:value-of select="$vR"/>
</xsl:when>
<xsl:when test="$vG <= $vR and $vG <= $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>
黑色
白色
是否需要测试背景颜色的“亮度”,并确定是选择白色还是黑色文本?listItemColor
变量的格式是什么?根据亮度的不同,是黑色还是白色。listItemColor可能是十六进制值。但是,如果有一种方法可以使用颜色名称和十六进制,那也很好。但最有可能的是,你的问题不清楚。请提供一个(或两个)输入示例,以及一组确定所需输出的明确规则。是否尝试以编程方式生成样式表?因为这就是它看起来的样子。但如果你是这么做的,我想你会这么说的。因为我们所得到的只是(a)一个问题描述,它讨论的是颜色和亮度,而不是元素和属性,以及(b)一点代码(没有任何上下文)毫无意义。向我们展示您的转换的输入和期望的输出,并解释它们之间的关系。对于混淆,我深表歉意,我将拿出一个工作示例并更新我的原始帖子。再一次,请原谅我,并与我保持一致。根据规范澄清修改了答案。因此,Rod的想法是将XML转换为使用不同的样式表。然后在该样式表中有不同的设置。然后,当他在浏览器中打开转换后的XML时,它要么是浅文本,要么是深文本。我改变了我的例子来说明这一点。但是,罗德必须弄清楚细节。你在这里实际计算的不是亮度,而是HSL颜色空间中的亮度。亮度和亮度都不是“亮度”——但这是一罐不值得在这里打开的蠕虫。@michael.hor257k——事实上,我很快从XSLT 2.0函数中破解了代码,忘记了HSL中的“L”代表“亮度”。谢谢