Skip to content

Commit f1231bf

Browse files
Allowing color mapping to integers in AgentPortrayalStyle (#2818)
* Allowing colormapping to integers in AgentPortrayalStyle * added validation and alias
1 parent 0dbe73a commit f1231bf

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

mesa/visualization/backends/matplotlib_backend.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ def collect_agent_data(self, space, agent_portrayal, default_size=None):
142142
else:
143143
aps = portray_input
144144
# Set defaults if not provided
145-
if aps.edgecolors is None:
146-
aps.edgecolors = aps.color
147145
if aps.x is None and aps.y is None:
148146
aps.x, aps.y = self._get_agent_pos(agent, space)
149147

mesa/visualization/components/portrayal_components.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from dataclasses import dataclass
1414
from typing import Any
1515

16+
ColorLike = str | tuple | int | float
17+
1618

1719
@dataclass
1820
class AgentPortrayalStyle:
@@ -46,7 +48,7 @@ class AgentPortrayalStyle:
4648

4749
x: float | None = None
4850
y: float | None = None
49-
color: str | tuple | None = "tab:blue"
51+
color: ColorLike | None = "tab:blue"
5052
marker: str | None = "o"
5153
size: int | float | None = 50
5254
zorder: int | None = 1
@@ -72,6 +74,13 @@ def update(self, *updates_fields: tuple[str, Any]):
7274
f"'{type(self).__name__}' object has no attribute '{field_to_change}'"
7375
)
7476

77+
def __post_init__(self):
78+
"""Validate that color and size are non-negative."""
79+
if isinstance(self.color, int | float) and self.color < 0:
80+
raise ValueError("Scalar color values must be non-negative")
81+
if isinstance(self.size, int | float) and self.size < 0:
82+
raise ValueError("Size must be a non-negative number")
83+
7584

7685
@dataclass
7786
class PropertyLayerStyle:

0 commit comments

Comments
 (0)