I have the following methods:
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()
Using Xmp datasource:
<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>
Gives:
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'}
Why is this calculating a #10d6c00
hexcode value? 269 is obviously wrong, but Lab, I'm not following how this may be wrong (except something about scaling the L
or R
value 0-255 > 0-100?).
This is using colormath 2.1.1.
Simple enough, the color is outside the representable range for sRGB. Switching to the clamped methods resolves the issue:
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)
Thanks to Magnus The Nordlander for the tip.