Skip to content

Commit 6bc163f

Browse files
authored
Enhance Icon class with hex color support
Added dynamic CSS for hex color markers and improved color validation.
1 parent 8e57cb6 commit 6bc163f

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

folium/map.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,33 @@ class Icon(MacroElement):
319319
{{ this.options|tojavascript }}
320320
);
321321
{% endmacro %}
322+
323+
{% macro header(this, kwargs) %}
324+
{% if this.is_hex %}
325+
<style>
326+
/* Dynamically generated CSS to replace the sprite image with a colored teardrop */
327+
.awesome-marker-icon-{{ this.color_name }} {
328+
background-image: none;
329+
background-color: {{ this.color }};
330+
width: 35px;
331+
height: 35px;
332+
border-radius: 50% 50% 50% 0;
333+
transform: rotate(-45deg);
334+
margin-top: -15px;
335+
margin-left: -17px;
336+
border: 2px solid white;
337+
box-shadow: 2px 2px 4px rgba(0,0,0,0.3);
338+
display: flex;
339+
align-items: center;
340+
justify-content: center;
341+
}
342+
/* Counter-rotate the inner icon so it stands up straight */
343+
.awesome-marker-icon-{{ this.color_name }} i {
344+
transform: rotate(45deg);
345+
}
346+
</style>
347+
{% endif %}
348+
{% endmacro %}
322349
""")
323350
color_options = {
324351
"red",
@@ -354,16 +381,23 @@ def __init__(
354381
super().__init__()
355382
self._name = "Icon"
356383

357-
# Check if the color is a valid hex code
358-
is_hex_color = color.startswith("#") and len(color) in (4, 7)
384+
# 1. Determine if the input is a hex color
385+
self.is_hex = color.startswith("#") and len(color) in (4, 7)
386+
self.color = color
359387

360-
if color not in self.color_options and not is_hex_color:
388+
# 2. Strip the '#' for the CSS class name generation
389+
self.color_name = color[1:] if self.is_hex else color
390+
391+
# 3. Allow hex codes to pass the warning check
392+
if color not in self.color_options and not self.is_hex:
361393
warnings.warn(
362-
f"color argument of Icon should be one of: {self.color_options} or a valid hex color (e.g., '#FF0000').",
394+
f"color argument of Icon should be one of: {self.color_options} or a valid hex code.",
363395
stacklevel=2,
364396
)
397+
398+
# 4. Pass the stripped color_name to the JavaScript options
365399
self.options = remove_empty(
366-
marker_color=color,
400+
marker_color=self.color_name,
367401
icon_color=icon_color,
368402
icon=icon,
369403
prefix=prefix,

0 commit comments

Comments
 (0)