Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom color palettes #51 #54

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions mapboxgl/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .colors import color_ramps
import geojson
import json

import re

def row_to_geojson(row, lon, lat):
"""Convert a pandas dataframe row to a geojson format object. Converts all datetimes to epoch seconds.
Expand Down Expand Up @@ -110,17 +110,36 @@ def create_color_stops(breaks, colors='RdYlGn', color_ramps=color_ramps):
"""Convert a list of breaks into color stops using colors from colorBrewer
see www.colorbrewer2.org for a list of color options to pass
"""
num_breaks = len(breaks)

if colors not in color_ramps.keys():
raise ValueError('color does not exist in colorBrewer!')
if isinstance(colors, list):
ramp = colors
for color in color:
if not validateColor(color):
raise ValueError("Color is in wrong format: " + str(color))
else:
stops = []
if colors not in color_ramps.keys():
raise ValueError('color does not exist in colorBrewer!')
try:
num_breaks = len(breaks)
ramp = color_ramps[colors][num_breaks]
except KeyError:
raise ValueError("Color ramp {} does not have a {} breaks".format(
colors, num_breaks))
for i, b in enumerate(breaks):
stops.append([b, ramp[i]])
return stops
stops = []
for i, b in enumerate(breaks):
stops.append([b, ramp[i]])
return stops

def validateColor(color):
if not isinstance(color, str):
return False
regex = "rgb\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)"
if not re.fullmatch(regex, color):
return False
color = color.replace("rgb(", "")
color = color.replace(")", "")
color = color.split(",")
for value in color:
if not 0 <= int(value) <= 255:
return False
return True