summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gusso230@student.liu.se>2020-11-12 14:13:24 +0100
committerGustav Sörnäs <gusso230@student.liu.se>2020-11-20 13:39:20 +0100
commit0ebe417a8c9edd12d9e87f3843006eba510c97f9 (patch)
tree3dbc6c806ebe3d10a737b8c2b63524acf08bda76
parent51a28d0d8bbe49b6be0d576ff1c94824eebd9174 (diff)
downloadtdde25-0ebe417a8c9edd12d9e87f3843006eba510c97f9.tar.gz
add color lerping
-rw-r--r--algorithms.py34
1 files changed, 34 insertions, 0 deletions
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'. """