Created
November 28, 2021 22:50
-
-
Save boboben1/12548e0a340bd86821f192412068eb23 to your computer and use it in GitHub Desktop.
Matplotlib gradients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Written by Ben Brecher | |
def interp_color(c1, c2, space): | |
c1 = np.array(c1) | |
c2 = np.array(c2) | |
return [(1-p) * c1 + p * c2 for p in space] | |
def make_color_map(*in_colors): | |
color_map = np.ones((256, 4)) | |
num_colors = len(in_colors) | |
color_ranges = np.linspace(0, 256, num_colors, dtype=np.uint) | |
for x in range(num_colors-1): | |
begin, end = color_ranges[x:x+2] | |
_color = interp_color(in_colors[x], in_colors[x+1], | |
np.linspace(0, 1, end-begin)) | |
color_map[begin:end, :3] = _color | |
return colors.ListedColormap(color_map) | |
# USAGE: | |
color_map_all_colors = [ | |
"#9ccff2", | |
"#cddcff", | |
"#F0F0F0", | |
"#ffc080", | |
"#ff0000", | |
"#000000" | |
] | |
color_map_all_colors.reverse() | |
color_map = make_color_map(*[colors.to_rgb(c) for c in color_map_all_colors]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment