@@ -103,6 +103,8 @@ def __init__(
103
103
"layer" : Layer (id = "background" , type = LayerType .BACKGROUND ),
104
104
"opacity" : 1.0 ,
105
105
"visible" : True ,
106
+ "type" : "background" ,
107
+ "color" : None ,
106
108
}
107
109
108
110
def add_layer (
@@ -133,10 +135,21 @@ def add_layer(
133
135
if name is None :
134
136
name = layer .id
135
137
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
+
136
147
self .layer_dict [name ] = {
137
148
"layer" : layer ,
138
149
"opacity" : 1.0 ,
139
150
"visible" : True ,
151
+ "type" : layer .type ,
152
+ "color" : color ,
140
153
}
141
154
super ().add_layer (layer , before_id = before_id )
142
155
@@ -932,6 +945,25 @@ def set_paint_property(self, name: str, prop: str, value: Any) -> None:
932
945
if "opacity" in prop :
933
946
self .layer_dict [name ]["opacity" ] = value
934
947
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
+
935
967
def set_opacity (self , name : str , opacity : float ) -> None :
936
968
"""
937
969
Set the opacity of a layer.
@@ -1006,14 +1038,41 @@ def layer_interact(self, name=None):
1006
1038
value = self .layer_dict [name ]["opacity" ],
1007
1039
style = style ,
1008
1040
)
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
+
1009
1061
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" ),
1011
1064
)
1012
1065
1013
1066
def dropdown_event (change ):
1014
1067
name = change .new
1015
1068
checkbox .value = self .layer_dict [dropdown .value ]["visible" ]
1016
1069
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
1017
1076
1018
1077
dropdown .observe (dropdown_event , "value" )
1019
1078
0 commit comments