Skip to content

Commit

Permalink
Minor refactor to color_map to prioritize returning exact match color…
Browse files Browse the repository at this point in the history
… from color_stops
  • Loading branch information
akacarlyann committed Mar 29, 2018
1 parent 6af2548 commit d80e99e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions mapboxgl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,18 @@ def color_map(lookup, color_stops, default_color='rgb(122,122,122)'):
assumes colors in color_stops provided as strings of form 'rgb(RRR,GGG,BBB)'
or in hex: '#RRGGBB'
"""

# if no color_stops, use default color
if len(color_stops) == 0:
return default_color

# dictionary to lookup color from match-type color_stops
match_map = dict((x, y) for (x, y) in color_stops)

# if lookup matches stop exactly, return corresponding color (first priority)
# (includes non-numeric color_stop "keys" for finding color by match)
if lookup in match_map.keys():
return match_map.get(lookup)

# if lookup value numeric, map color by interpolating from color scale
if isinstance(lookup, (int, float, complex)):

Expand Down Expand Up @@ -234,11 +239,8 @@ def color_map(lookup, color_stops, default_color='rgb(122,122,122)'):
# return string representing rgb color value
return scale(distance).to_string().replace(', ', ',')

# if non-numeric color_stop "key", find color by match
else:

# return string representing rgb color value
return match_map.get(lookup, default_color)
# default color value catch-all
return default_color


def img_encode(arr, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_color_map_default_color():
def test_color_map_numeric_match():
"""Get color for numeric lookup value in categorical color stops if number exists in stops"""
match_stops = [[0.0, 'rgb(255,0,255)'],['CA', 'rgb(255,0,0)'], ['NY', 'rgb(255,255,0)'], ['MA', 'rgb(0,0,255)']]
assert color_map(0.0, match_stops, 'green') == 'rgb(255,0,0)'
assert color_map(0.0, match_stops, 'green') == 'rgb(255,0,255)'


def test_color_map_interp():
Expand Down

0 comments on commit d80e99e

Please sign in to comment.