将ColorCth中的LabColor转换为sRGBColor会产生无效的十六进制

问题描述 投票:-1回答:1

我有以下方法:

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>

得到:

sRGBColor(rgb_r=1.0543146152492686,rgb_g=0.42327948385100367,rgb_b=0.0)
set([0, 108, 269])
{'color_type': 'SPOT',
 'display': '#10d6c00',
 'name': 'PANTONE 1505 C',
 'source': {'L*': '66.274513', 'a*': '59', 'b*': '93'},
 'source_type': 'LAB',
 'tint': '100.000000'}

为什么计算#10d6c00十六进制值? 269显然是错误的,但实验室,我没有关注这可能是错误的(除了关于缩放LR值0-255> 0-100?)。

这是使用colormath 2.1.1。

python python-2.7 xmp lab-color-space colormath
1个回答
0
投票

很简单,颜色超出了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 The Nordlander的小费。

© www.soinside.com 2019 - 2024. All rights reserved.