Skip to content

Commit f754469

Browse files
Fix color handling (#1843)
* Fix color handling * Use sRGB in tests * Explicit test for STEP color handling
1 parent eb79a4e commit f754469

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

cadquery/occ_impl/assembly.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
from OCP.TDataStd import TDataStd_Name
2121
from OCP.TDF import TDF_Label
2222
from OCP.TopLoc import TopLoc_Location
23-
from OCP.Quantity import Quantity_ColorRGBA
23+
from OCP.Quantity import (
24+
Quantity_ColorRGBA,
25+
Quantity_Color,
26+
Quantity_TOC_sRGB,
27+
)
2428
from OCP.BRepAlgoAPI import BRepAlgoAPI_Fuse
2529
from OCP.TopTools import TopTools_ListOfShape
2630
from OCP.BOPAlgo import BOPAlgo_GlueEnum, BOPAlgo_MakeConnected
@@ -92,12 +96,16 @@ def __init__(self, *args, **kwargs):
9296
raise ValueError(f"Unknown color name: {args[0]}")
9397
elif len(args) == 3:
9498
r, g, b = args
95-
self.wrapped = Quantity_ColorRGBA(r, g, b, 1)
99+
self.wrapped = Quantity_ColorRGBA(
100+
Quantity_Color(r, g, b, Quantity_TOC_sRGB), 1
101+
)
96102
if kwargs.get("a"):
97103
self.wrapped.SetAlpha(kwargs.get("a"))
98104
elif len(args) == 4:
99105
r, g, b, a = args
100-
self.wrapped = Quantity_ColorRGBA(r, g, b, a)
106+
self.wrapped = Quantity_ColorRGBA(
107+
Quantity_Color(r, g, b, Quantity_TOC_sRGB), a
108+
)
101109
else:
102110
raise ValueError(f"Unsupported arguments: {args}, {kwargs}")
103111

@@ -114,9 +122,9 @@ def toTuple(self) -> Tuple[float, float, float, float]:
114122
Convert Color to RGB tuple.
115123
"""
116124
a = self.wrapped.Alpha()
117-
rgb = self.wrapped.GetRGB()
125+
rgb = self.wrapped.GetRGB().Values(Quantity_TOC_sRGB)
118126

119-
return (rgb.Red(), rgb.Green(), rgb.Blue(), a)
127+
return (*rgb, a)
120128

121129
def __getstate__(self) -> Tuple[float, float, float, float]:
122130

tests/test_assembly.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from OCP.STEPCAFControl import STEPCAFControl_Reader
3535
from OCP.IFSelect import IFSelect_RetDone
3636
from OCP.TDF import TDF_ChildIterator
37-
from OCP.Quantity import Quantity_ColorRGBA, Quantity_TOC_RGB
37+
from OCP.Quantity import Quantity_ColorRGBA, Quantity_TOC_sRGB
3838
from OCP.TopAbs import TopAbs_ShapeEnum
3939

4040

@@ -427,7 +427,7 @@ def get_doc_nodes(doc, leaf=False):
427427
child, XCAFDoc_ColorType.XCAFDoc_ColorSurf, color_subshape
428428
):
429429
face_color = (
430-
*color_subshape.GetRGB().Values(Quantity_TOC_RGB),
430+
*color_subshape.GetRGB().Values(Quantity_TOC_sRGB),
431431
color_subshape.Alpha(),
432432
)
433433

@@ -442,7 +442,7 @@ def get_doc_nodes(doc, leaf=False):
442442
):
443443
color_subshapes_set.add(
444444
(
445-
*color_subshape.GetRGB().Values(Quantity_TOC_RGB),
445+
*color_subshape.GetRGB().Values(Quantity_TOC_sRGB),
446446
color_subshape.Alpha(),
447447
)
448448
)
@@ -454,9 +454,9 @@ def get_doc_nodes(doc, leaf=False):
454454
{
455455
"path": PurePath(node.Id.ToCString()),
456456
"name": TCollection_ExtendedString(name_att.Get()).ToExtString(),
457-
"color": (*color.GetRGB().Values(Quantity_TOC_RGB), color.Alpha()),
457+
"color": (*color.GetRGB().Values(Quantity_TOC_sRGB), color.Alpha()),
458458
"color_shape": (
459-
*color_shape.GetRGB().Values(Quantity_TOC_RGB),
459+
*color_shape.GetRGB().Values(Quantity_TOC_sRGB),
460460
color_shape.Alpha(),
461461
),
462462
"color_subshapes": color_subshapes,
@@ -1972,3 +1972,31 @@ def test_remove_without_parent():
19721972

19731973
assert len(assy.children) == 1
19741974
assert len(assy.objects) == 1
1975+
1976+
1977+
def test_step_color(tmp_path_factory):
1978+
"""
1979+
Checks color handling for STEP export.
1980+
"""
1981+
1982+
# Use a temporary directory
1983+
tmpdir = tmp_path_factory.mktemp("out")
1984+
step_color_path = os.path.join(tmpdir, "step_color.step")
1985+
1986+
# Create a simple assembly with color
1987+
assy = cq.Assembly()
1988+
assy.add(cq.Workplane().box(10, 10, 10), color=cq.Color(0.47, 0.253, 0.18, 1.0))
1989+
1990+
success = exportStepMeta(assy, step_color_path)
1991+
assert success
1992+
1993+
# Read the file as a string and check for the correct colors
1994+
with open(step_color_path, "r") as f:
1995+
step_content = f.readlines()
1996+
1997+
# Step through and try to find the COLOUR line
1998+
for line in step_content:
1999+
if "COLOUR_RGB(''," in line:
2000+
assert "0.47" in line
2001+
assert "0.25" in line
2002+
assert "0.18" in line

0 commit comments

Comments
 (0)