Skip to content

Commit 4bab75e

Browse files
Brought back Color.toTuple() method
1 parent 288f1e6 commit 4bab75e

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

cadquery/materials.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ def rgba(self) -> RGBA:
129129
"""Get RGBA components as tuple."""
130130
return (self.red, self.green, self.blue, self.alpha)
131131

132+
def toTuple(self) -> RGBA:
133+
"""Get RGBA components as tuple."""
134+
return self.rgba()
135+
132136
def to_occ_rgb(self) -> "Quantity_Color":
133137
"""Convert Color to an OCCT RGB color object."""
134138
from OCP.Quantity import Quantity_Color, Quantity_TOC_sRGB
@@ -249,7 +253,7 @@ def apply_to_vtk_actor(self, actor: "vtkActor") -> None:
249253
elif self.simple:
250254
self.simple.apply_to_vtk_actor(actor)
251255
elif self.color:
252-
r, g, b, a = self.color.rgba()
256+
r, g, b, a = self.color.toTuple()
253257
prop.SetColor(r, g, b)
254258
prop.SetOpacity(a)
255259

cadquery/occ_impl/assembly.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def toJSON(
476476
trans, rot = element.location.toTuple()
477477

478478
val["shape"] = data
479-
val["color"] = element.color.rgba() if element.color else color.rgba()
479+
val["color"] = element.color.toTuple() if element.color else color.toTuple()
480480
val["position"] = trans
481481
val["orientation"] = tuple(radians(r) for r in rot)
482482

@@ -489,7 +489,7 @@ def toJSON(
489489
}
490490
if element.material.pbr:
491491
val["material"]["pbr"] = {
492-
"base_color": element.material.pbr.base_color.rgba(),
492+
"base_color": element.material.pbr.base_color.toTuple(),
493493
"metallic": element.material.pbr.metallic,
494494
"roughness": element.material.pbr.roughness,
495495
"refraction_index": element.material.pbr.refraction_index,

doc/gen_colors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
def color_to_rgba_str(c: cq.Color) -> str:
2020
""" Convert a Color object to a string for the HTML/CSS template.
2121
"""
22-
t = c.rgba()
22+
t = c.toTuple()
2323
vals = [int(v * 255) for v in t[:3]]
2424
return ",".join([str(v) for v in chain(vals, [t[3]])])
2525

2626

2727
def calc_text_color(c: cq.Color) -> str:
2828
""" Calculate required overlay text color from background color.
2929
"""
30-
val = sum(c.rgba()[:3]) / 3
30+
val = sum(c.toTuple()[:3]) / 3
3131
if val < 0.5:
3232
rv = "255,255,255"
3333
else:

tests/test_assembly.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ def check_nodes(doc, expected):
10831083
(["box0", "box0_part"], {"color_shape": (1.0, 0.0, 0.0, 1.0)}),
10841084
(
10851085
["box1", "box1_part"],
1086-
{"color_shape": cq.Color().rgba()},
1086+
{"color_shape": cq.Color().toTuple()},
10871087
), # default color when unspecified
10881088
],
10891089
),

tests/test_materials.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def test_rgba_method(self):
7676
color = Color(0.1, 0.2, 0.3, 0.4)
7777
assert color.rgba() == (0.1, 0.2, 0.3, 0.4)
7878

79+
def test_to_tuple(self):
80+
color = Color(0.1, 0.2, 0.3, 0.4)
81+
assert color.toTuple() == (0.1, 0.2, 0.3, 0.4)
82+
7983
def test_equality(self):
8084
color1 = Color(0.1, 0.2, 0.3, 0.4)
8185
color2 = Color(0.1, 0.2, 0.3, 0.4)

0 commit comments

Comments
 (0)