Sat, 12/06/2025 - 19:31
AI and some googling told me that the color space in a XmlXCAF file is sRGB. But it seems not align with what I observed. I used CadQuery-OCP to create a simple box and saved it to a XmlXCAF file with a name and a color assigned to the box.
box = BRepPrimAPI_MakeBox(10, 10, 10).Shape()
theColor = Quantity_Color()
Quantity_Color.ColorFromHex_s("#D43D3D", theColor)
r, g, b = theColor.Values(Quantity_TOC_RGB)
print("linear RGB: ", r, g, b) # output: linear RGB: 0.658 0.046 0.046 i.e. #A70B0B
r, g, b = theColor.Values(Quantity_TOC_sRGB)
print("sRGB space: ", r, g, b) # output: sRGB space: 0.831 0.239 0.239 i.e. #D43D3D
doc_format = TCollection_ExtendedString("XmlXCAF")
doc = TDocStd_Document(doc_format)
application = XCAFApp_Application.GetApplication_s()
XmlXCAFDrivers.DefineFormat_s(application)
application.InitDocument(doc)
shape_tool = XCAFDoc_DocumentTool.ShapeTool_s(doc.Main())
color_tool = XCAFDoc_DocumentTool.ColorTool_s(doc.Main())
XCAFDoc_DocumentTool.SetLengthUnit_s(doc, 0.001)
_root_label = shape_tool.AddShape(box)
TDataStd_Name.Set_s(
_root_label,
TCollection_ExtendedString("solid-box"),
)
color_tool.SetColor(
_root_label,
theColor,
XCAFDoc_ColorType.XCAFDoc_ColorGen,
)
status = application.SaveAs(doc, TCollection_ExtendedString("box.xml"))
print(status)
In the resulting file, I got the following. The only color in the file is #A70B0BFF which is the same as the linear RGB from the above python code. Does it mean the XmlXCAF file saves the color in linear RGB?
<label tag="1">
<TDataStd_Name id="5" nameguid="2a96b608-ec8b-11d0-bee7-080009dc3333">Shapes</TDataStd_Name>
<XCAFDoc_ShapeTool id="6"/>
<TDF_TagSource id="7">1</TDF_TagSource>
<label tag="1">
<TNaming_NamedShape id="8" evolution="primitive">
<olds/>
<news>
<shape tshape="+34" index="1"/>
</news>
</TNaming_NamedShape>
<TDataStd_Name id="9" nameguid="2a96b608-ec8b-11d0-bee7-080009dc3333">solid-box</TDataStd_Name>
<TDataStd_TreeNode id="10" treeid="efd212e4-6dfd-11d4-b9c8-0060b0ee281b"/>
</label>
</label>
<label tag="2">
<TDataStd_Name id="11" nameguid="2a96b608-ec8b-11d0-bee7-080009dc3333">Colors</TDataStd_Name>
<XCAFDoc_ColorTool id="12"/>
<TDF_TagSource id="13">1</TDF_TagSource>
<label tag="1">
<xcaf:Color id="14">30</xcaf:Color>
<TDataStd_Name id="15" nameguid="2a96b608-ec8b-11d0-bee7-080009dc3333">BROWN3 (#A70B0BFF)</TDataStd_Name>
<TDataStd_TreeNode id="16" treeid="efd212e4-6dfd-11d4-b9c8-0060b0ee281b" children="10 "/>
</label>
</label>
<label tag="3">
<TDataStd_Name id="17" nameguid="2a96b608-ec8b-11d0-bee7-080009dc3333">Layers</TDataStd_Name>
<XCAFDoc_LayerTool id="18"/>
</label>
Attachments:
Sun, 12/07/2025 - 13:19
Actually, it neither. The line you've spotted in
XMLfile:is only a Name tag, it isn't used by persistence for a color value and holds an arbitrary text. The fact that you see linear RGB in hex here is a bug of name generator
XCAFDoc_ColorTool::AddColor: https://github.com/Open-Cascade-SAS/OCCT/pull/317The actual value is stored in another tag:
and it is
30, which corresponds toQuantity_NOC_BROWN3value of enumerationQuantity_NameOfColor. That is it, the color isXmlXCAFcolor is stored as an enumerated value (just an artifact of a past). You may also notice that color transparency is lost withinXmlXCAFformat.It would be nice improving
XmlXCAFin this regard, but this format is rarely used in practice. Notice that inBinXCAFthe color is stored as linear RGB values as floating point numbers - I would suggest using this format, when possible.Mon, 12/08/2025 - 00:38
Thanks for the clarification. I will certainly look at BinXCAF.