Python 在colormath中将LabColor转换为sRGBColor会给出无效的十六进制
我有以下方法:Python 在colormath中将LabColor转换为sRGBColor会给出无效的十六进制,python,python-2.7,xmp,lab-color-space,colormath,Python,Python 2.7,Xmp,Lab Color Space,Colormath,我有以下方法: def labProcessColorSwatch(node, namespaces): return { "name": node.find(".//xmpG:swatchName", namespaces).text, "color_type": node.find(".//xmpG:type", namespaces).text, "tint": node.find(".//xmpG:tint", namespaces
def labProcessColorSwatch(node, namespaces):
return {
"name": node.find(".//xmpG:swatchName", namespaces).text,
"color_type": node.find(".//xmpG:type", namespaces).text,
"tint": node.find(".//xmpG:tint", namespaces).text,
"source_type": node.find(".//xmpG:mode", namespaces).text,
"display": '',
"source": {
"L*": node.find(".//xmpG:L", namespaces).text,
"a*": node.find(".//xmpG:A", namespaces).text,
"b*": node.find(".//xmpG:B", namespaces).text,
}
}
def labToHex(swatch):
lab = LabColor(
lab_l = float(swatch["source"]["L*"]),
lab_a = float(swatch["source"]["a*"]),
lab_b = float(swatch["source"]["b*"])
)
rgb = convert_color(lab, sRGBColor)
return bitConvertHex(rgb) # rgb.get_rgb_hex()
使用Xmp数据源:
<rdf:li rdf:parseType="Resource">
<xmpG:swatchName>PANTONE 1505 C</xmpG:swatchName>
<xmpG:type>SPOT</xmpG:type>
<xmpG:tint>100.000000</xmpG:tint>
<xmpG:mode>LAB</xmpG:mode>
<xmpG:L>66.274513</xmpG:L>
<xmpG:A>59</xmpG:A>
<xmpG:B>93</xmpG:B>
</rdf:li>
为什么要计算一个#10d6c00
hexcode值?269显然是错误的,但是实验室,我不明白这是怎么错误的(除了一些关于缩放L
或R
值0-255>0-100的内容)
这是在使用colormath 2.1.1。非常简单,颜色超出了sRGB的可表示范围。切换到夹紧方法可以解决问题:
def bitConvertHex(color):
# Scale up to 0-255 values.
rgb_r = int(math.floor(0.5 + color.clamped_rgb_r * 255))
rgb_g = int(math.floor(0.5 + color.clamped_rgb_g * 255))
rgb_b = int(math.floor(0.5 + color.clamped_rgb_b * 255))
return '#%02x%02x%02x' % (rgb_r, rgb_g, rgb_b)
多亏了诺德兰德人Magnus的提示。不要用camalCase声明函数,我看到你用javascript编写,在javascript中你可以用camalCase,但在python使用中请使用>
函数名应该是小写的,为了提高可读性,有必要用下划线分隔单词。
所以对camelcase投反对票?什么?很公平。这个问题没有错。我也有答案。我从来没有用Python编程,所以我现在不关心它们的约定。谢谢,我不知道<代码>:)
def bitConvertHex(color):
# Scale up to 0-255 values.
rgb_r = int(math.floor(0.5 + color.clamped_rgb_r * 255))
rgb_g = int(math.floor(0.5 + color.clamped_rgb_g * 255))
rgb_b = int(math.floor(0.5 + color.clamped_rgb_b * 255))
return '#%02x%02x%02x' % (rgb_r, rgb_g, rgb_b)