Skip to content

Added ability to convert linear RGB tuple to sRGB #1839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cadquery/occ_impl/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ def toTuple(self) -> Tuple[float, float, float, float]:

return (rgb.Red(), rgb.Green(), rgb.Blue(), a)

def shiftForExport(self):
def _srgb_to_linear(c):
"""Convert a single sRGB channel (0.0–1.0) to linear RGB."""
if c <= 0.04045:
return c / 12.92
else:
return ((c + 0.055) / 1.055) ** 2.4

# Pre-shift the colors so that when they are converted again during export they end back up at the expected values
rgba = self.toTuple()
return Color(
_srgb_to_linear(rgba[0]),
_srgb_to_linear(rgba[1]),
_srgb_to_linear(rgba[2]),
rgba[3],
)

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

return self.toTuple()
Expand Down
8 changes: 6 additions & 2 deletions cadquery/occ_impl/exporters/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ def _process_child(child: AssemblyProtocol, assy_label: TDF_Label):
part_label = shape_tool.AddShape(shape.wrapped, False)
TDataStd_Name.Set_s(part_label, TCollection_ExtendedString(name))
if color:
color_tool.SetColor(part_label, color.wrapped, XCAFDoc_ColorGen)
color_tool.SetColor(
part_label, color.shiftForExport().wrapped, XCAFDoc_ColorGen
)
shape_tool.AddComponent(assy_label, part_label, loc.wrapped)

# If this assembly has shape metadata, add it to the shape
Expand Down Expand Up @@ -207,7 +209,9 @@ def _process_child(child: AssemblyProtocol, assy_label: TDF_Label):
# Set the individual face color
if face in colors:
color_tool.SetColor(
face_label, colors[face].wrapped, XCAFDoc_ColorGen,
face_label,
colors[face].shiftForExport().wrapped,
XCAFDoc_ColorGen,
)

# Also add a layer to hold the face label data
Expand Down
23 changes: 23 additions & 0 deletions tests/test_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,29 @@ def test_color():
cq.Color(1, 2, 3, 4, 5)


def test_step_color(tmp_path_factory):
# Use a temporary directory
tmpdir = tmp_path_factory.mktemp("out")
step_color_path = os.path.join(tmpdir, "step_color.step")

# Create a simple assembly with color
assy = cq.Assembly()
assy.add(cq.Workplane().box(10, 10, 10), color=cq.Color(0.47, 0.253, 0.18, 1.0))

success = exportStepMeta(assy, step_color_path)
assert success

# Read the file as a string and check for the correct colors
with open(step_color_path, "r") as f:
step_content = f.readlines()
# Step through and try to find the COLOUR line
for line in step_content:
if "COLOUR_RGB(''," in line:
assert "0.47" in line
assert "0.25" in line
assert "0.18" in line


def test_assembly(simple_assy, nested_assy):

# basic checks
Expand Down