Skip to content

Commit 2d8044a

Browse files
authored
Add color picker for change layer color interactively (#771)
1 parent 39a124f commit 2d8044a

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

leafmap/maplibregl.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ def __init__(
103103
"layer": Layer(id="background", type=LayerType.BACKGROUND),
104104
"opacity": 1.0,
105105
"visible": True,
106+
"type": "background",
107+
"color": None,
106108
}
107109

108110
def add_layer(
@@ -133,10 +135,21 @@ def add_layer(
133135
if name is None:
134136
name = layer.id
135137

138+
if (
139+
"paint" in layer.to_dict()
140+
and f"{layer.type}-color" in layer.paint
141+
and isinstance(layer.paint[f"{layer.type}-color"], str)
142+
):
143+
color = check_color(layer.paint[f"{layer.type}-color"])
144+
else:
145+
color = None
146+
136147
self.layer_dict[name] = {
137148
"layer": layer,
138149
"opacity": 1.0,
139150
"visible": True,
151+
"type": layer.type,
152+
"color": color,
140153
}
141154
super().add_layer(layer, before_id=before_id)
142155

@@ -932,6 +945,25 @@ def set_paint_property(self, name: str, prop: str, value: Any) -> None:
932945
if "opacity" in prop:
933946
self.layer_dict[name]["opacity"] = value
934947

948+
def set_color(self, name: str, color: str) -> None:
949+
"""
950+
Set the color of a layer.
951+
952+
This method sets the color of the specified layer to the specified value.
953+
954+
Args:
955+
name (str): The name of the layer.
956+
color (str): The color value to set.
957+
958+
Returns:
959+
None
960+
"""
961+
color = check_color(color)
962+
super().set_paint_property(
963+
name, f"{self.layer_dict[name]['layer'].type}-color", color
964+
)
965+
self.layer_dict[name]["color"] = color
966+
935967
def set_opacity(self, name: str, opacity: float) -> None:
936968
"""
937969
Set the opacity of a layer.
@@ -1006,14 +1038,41 @@ def layer_interact(self, name=None):
10061038
value=self.layer_dict[name]["opacity"],
10071039
style=style,
10081040
)
1041+
1042+
color_picker = widgets.ColorPicker(
1043+
concise=True,
1044+
value="white",
1045+
style=style,
1046+
)
1047+
1048+
if self.layer_dict[name]["color"] is not None:
1049+
color_picker.value = self.layer_dict[name]["color"]
1050+
color_picker.disabled = False
1051+
else:
1052+
color_picker.value = "white"
1053+
color_picker.disabled = True
1054+
1055+
def color_picker_event(change):
1056+
if self.layer_dict[dropdown.value]["color"] is not None:
1057+
self.set_color(dropdown.value, change.new)
1058+
1059+
color_picker.observe(color_picker_event, "value")
1060+
10091061
hbox = widgets.HBox(
1010-
[dropdown, checkbox, opacity_slider], layout=widgets.Layout(width="600px")
1062+
[dropdown, checkbox, opacity_slider, color_picker],
1063+
layout=widgets.Layout(width="750px"),
10111064
)
10121065

10131066
def dropdown_event(change):
10141067
name = change.new
10151068
checkbox.value = self.layer_dict[dropdown.value]["visible"]
10161069
opacity_slider.value = self.layer_dict[dropdown.value]["opacity"]
1070+
if self.layer_dict[dropdown.value]["color"] is not None:
1071+
color_picker.value = self.layer_dict[dropdown.value]["color"]
1072+
color_picker.disabled = False
1073+
else:
1074+
color_picker.value = "white"
1075+
color_picker.disabled = True
10171076

10181077
dropdown.observe(dropdown_event, "value")
10191078

0 commit comments

Comments
 (0)