From 0ebe417a8c9edd12d9e87f3843006eba510c97f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 12 Nov 2020 14:13:24 +0100 Subject: add color lerping --- algorithms.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/algorithms.py b/algorithms.py index 6e01287..8a6e2d3 100644 --- a/algorithms.py +++ b/algorithms.py @@ -1,3 +1,4 @@ +import colorsys import heapq import math @@ -16,6 +17,39 @@ def length_haversine(p1, p2): return 6372797.560856 * c # return the distance in meters +def chunks(lst, n): + """ Split lst into chunks of size n. + + The final chunk will have size equal to len(lst) % n + """ + for i in range(0, len(lst), n): + yield lst[i:min(i+n, len(lst))] + + +def lerp_color(start_color, end_color, t): + """ Lerp between two colors by a factor t. + + Colors are assumed to be in the form "#RRGGBB" + """ + def lerp(a, b, t): + return a + (b - a) * t + + start = [int(c, 16)/255 for c in chunks(start_color[1:], 2)] + end = [int(c, 16)/255 for c in chunks(end_color[1:], 2)] + + start = colorsys.rgb_to_hsv(*start) + end = colorsys.rgb_to_hsv(*end) + + color = [lerp(c1, c2, t) for (c1, c2) in zip(start, end)] + color = colorsys.hsv_to_rgb(*color) + + return "#{:02x}{:02x}{:02x}".format( + int(color[0] * 255), + int(color[1] * 255), + int(color[2] * 255), + ) + + def get_closest_node_id(nodes, source_node): """ Search through all nodes and return the id of the node that is closest to 'source_node'. """ -- cgit v1.2.1