Skip to content

Commit

Permalink
More Color Fixes (#243)
Browse files Browse the repository at this point in the history
* More color fixes.

* Added assert.
  • Loading branch information
Eric-Vin committed Apr 23, 2024
1 parent 1c49d80 commit 0f95996
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/scenic/core/object_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,7 @@ def _specify(cls, context, prop, value):
"Color property contains value not between 0 and 1 (inclusive)."
)

if len(value) == 3:
value = (value[0], value[1], value[2], 1)
elif len(value) != 4:
if not 3 <= len(value) <= 4:
raise ValueError(f"Color property has incorrect length {len(value)}.")

object.__setattr__(context, prop, value)
Expand Down Expand Up @@ -1557,7 +1555,14 @@ def show3D(self, viewer, highlight=False):
if highlight:
object_mesh.visual.face_colors = [30, 179, 0, 255]
elif self.color is not None:
r, g, b, a = self.color
if len(self.color) == 3:
r, g, b = self.color
a = 1
elif len(self.color) == 4:
r, g, b, a = self.color
else:
assert False

object_mesh.visual.face_colors = [255 * r, 255 * g, 255 * b, 255 * a]

viewer.add_geometry(object_mesh)
Expand Down
12 changes: 11 additions & 1 deletion tests/simulators/utils/test_colors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import pytest

from scenic.simulators.utils.colors import NoisyColorDistribution
from scenic.simulators.utils.colors import Color, NoisyColorDistribution
from tests.utils import sampleEgoFrom


def test_color_class():
program = """
from scenic.simulators.utils.colors import Color
ego = new Object with color Color.defaultCarColor()
"""
ego = sampleEgoFrom(program)
assert isinstance(ego.color, Color)


def test_add_noise():
Expand Down
2 changes: 1 addition & 1 deletion tests/syntax/test_specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ def test_color():
ego = new Object with color (0.5,0.5,0.5)
"""
ego = sampleEgoFrom(program)
assert ego.color == (0.5, 0.5, 0.5, 1)
assert ego.color == (0.5, 0.5, 0.5)

with pytest.raises(ValueError):
program = """
Expand Down

0 comments on commit 0f95996

Please sign in to comment.