Skip to content

Commit

Permalink
Color tweaks. (#240)
Browse files Browse the repository at this point in the history
* Color tweaks.

* Color tweaks bugfix.

* Style tweak and added test.
  • Loading branch information
Eric-Vin authored Apr 22, 2024
1 parent 7f018e0 commit 747f211
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/scenic/core/object_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ 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:
raise ValueError(f"Color property has incorrect length {len(value)}.")

object.__setattr__(context, prop, value)

def _register(self):
Expand Down Expand Up @@ -1555,7 +1560,8 @@ def show3D(self, viewer, highlight=False):
if highlight:
object_mesh.visual.face_colors = [30, 179, 0, 255]
elif self.color is not None:
object_mesh.visual.face_colors = self.color
r, g, b, a = self.color
object_mesh.visual.face_colors = [255 * r, 255 * g, 255 * b, 255 * a]

viewer.add_geometry(object_mesh)

Expand Down
2 changes: 1 addition & 1 deletion src/scenic/simulators/utils/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__(self, baseColor, hueNoise, satNoise, lightNoise):
@staticmethod
def addNoiseTo(color, hueNoise, lightNoise, satNoise):
try:
hue, lightness, saturation = colorsys.rgb_to_hls(*color)
hue, lightness, saturation = colorsys.rgb_to_hls(*color[:3])
except ZeroDivisionError:
hue, lightness, saturation = 0.0, 1.0, 0.0
hue = max(0, min(1, hue + hueNoise))
Expand Down
27 changes: 27 additions & 0 deletions tests/syntax/test_specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,3 +1136,30 @@ def test_shape():

with pytest.raises(InvalidScenarioError):
sampleEgoFrom(program, mode2D=True)


# Color
def test_color():
program = """
ego = new Object with color (0.5,0.5,0.5,0.5)
"""
ego = sampleEgoFrom(program)
assert ego.color == (0.5, 0.5, 0.5, 0.5)

program = """
ego = new Object with color (0.5,0.5,0.5)
"""
ego = sampleEgoFrom(program)
assert ego.color == (0.5, 0.5, 0.5, 1)

with pytest.raises(ValueError):
program = """
ego = new Object with color (255,0,0)
"""
sampleEgoFrom(program)

with pytest.raises(ValueError):
program = """
ego = new Object with color (1,1,1,1,1)
"""
sampleEgoFrom(program)

0 comments on commit 747f211

Please sign in to comment.