88"""
99from jinja2 import Template
1010from folium .six import text_type , binary_type
11+ from folium .element import MacroElement , Figure , JavascriptLink
12+ from folium .utilities import legend_scaler
1113
1214_cnames = {
1315 'aliceblue' : '#F0F8FF' ,
@@ -241,12 +243,37 @@ def _parse_color(x):
241243 color_tuple = tuple (u / 255. for u in color_tuple )
242244 return tuple (map (float ,(color_tuple + (1. ,))[:4 ]))
243245
244- class ColorMap (object ):
246+ class ColorMap (MacroElement ):
245247 """A generic class for creating colormaps."""
246248
247- def __init__ (self , min = 0. , max = 1. ):
249+ def __init__ (self , min = 0. , max = 1. , caption = "" ):
250+ """
251+ """
252+ super (ColorMap , self ).__init__ ()
253+ self ._name = 'ColorMap'
254+
248255 self .min = min
249256 self .max = max
257+ self .caption = caption
258+ self .index = [min , max ]
259+
260+ self ._template = self ._env .get_template ('color_scale.js' )
261+
262+ def render (self , ** kwargs ):
263+ self .color_domain = [self .min + (self .max - self .min )* i / 499. for i in range (500 )]
264+ self .color_range = [self .__call__ (x ) for x in self .color_domain ]
265+ self .tick_labels = legend_scaler (self .index )
266+ self .caption = ""
267+
268+ super (ColorMap , self ).render (** kwargs )
269+
270+ figure = self .get_root ()
271+ assert isinstance (figure , Figure ), ("You cannot render this Element "
272+ "if it's not in a Figure." )
273+
274+ figure .header .add_children (
275+ JavascriptLink ("https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" ), # noqa
276+ name = 'd3' )
250277
251278 def _rgba_floats_tuple (self , x ):
252279 """This class has to be implemented for each class inheriting from Colormap.
@@ -303,7 +330,7 @@ def _repr_html_(self):
303330 + '</svg>' )
304331
305332class LinearColormap (ColorMap ):
306- def __init__ (self , colors , index = None , min = 0. , max = 1. ):
333+ def __init__ (self , colors , index = None , min = 0. , max = 1. , caption = "" ):
307334 """Creates a ColorMap based on linear interpolation of a set of colors
308335 over a given index.
309336
@@ -327,6 +354,8 @@ def __init__(self, colors, index=None, min=0., max=1.):
327354 The maximal value for the colormap.
328355 Values higher than `max` will be bound directly to `colors[-1]`.
329356 """
357+ super (LinearColormap , self ).__init__ (min = min , max = max , caption = caption )
358+
330359 n = len (colors )
331360 if n < 2 :
332361 raise ValueError ('You must provide at least 2 colors.' )
@@ -335,8 +364,6 @@ def __init__(self, colors, index=None, min=0., max=1.):
335364 else :
336365 self .index = list (index ).copy ()
337366 self .colors = [_parse_color (x ) for x in colors ]
338- self .min = min
339- self .max = max
340367
341368 def _rgba_floats_tuple (self , x ):
342369 """Provides the color corresponding to value `x` in the
@@ -397,7 +424,7 @@ def scale(self, min=0., max=1.):
397424 )
398425
399426class StepColormap (ColorMap ):
400- def __init__ (self , colors , index = None , min = 0. , max = 1. ):
427+ def __init__ (self , colors , index = None , min = 0. , max = 1. , caption = "" ):
401428 """Creates a ColorMap based on stepwise constant colorfunction.
402429
403430 Parameters
@@ -420,6 +447,8 @@ def __init__(self, colors, index=None, min=0., max=1.):
420447 The maximal value for the colormap.
421448 Values higher than `max` will be bound directly to `colors[-1]`.
422449 """
450+ super (StepColormap , self ).__init__ (min = min , max = max , caption = caption )
451+
423452 n = len (colors )
424453 if n < 1 :
425454 raise ValueError ('You must provide at least 1 colors.' )
@@ -428,8 +457,6 @@ def __init__(self, colors, index=None, min=0., max=1.):
428457 else :
429458 self .index = list (index ).copy ()
430459 self .colors = [_parse_color (x ) for x in colors ]
431- self .min = min
432- self .max = max
433460
434461 def _rgba_floats_tuple (self , x ):
435462 """Provides the color corresponding to value `x` in the
0 commit comments