-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
61 lines (48 loc) · 1.28 KB
/
Copy pathutils.py
File metadata and controls
61 lines (48 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import math
import struct
import itertools
from collections import deque
def clamp(v, mn, mx):
return min(max(v, mn), mx)
def slide_window(l, n):
it = iter(l)
buf = deque(itertools.islice(it, n - 1), n)
for i in it:
buf.append(i)
yield tuple(buf)
def grouper(l, n):
chunks = zip(*[iter(l)] * n) # itertools.zip_longest(*[iter(l)]*n)
for chunk in chunks:
yield chunk
def length(p1, p2):
x1, y1 = p1
x2, y2 = p2
return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
def get_channels(reader, channels):
for samples in grouper(
(bytes(x) for x in grouper(itertools.chain.from_iterable(reader), 2)), channels
):
group = [
x / 32768 for x in struct.unpack("<" + "h" * channels, b"".join(samples))
]
group[1] = group[1] * -1 # fix orientation
yield group
def hsv_to_rgb(h, s, v):
if s == 0.0:
return (v, v, v)
i = int(h * 6.0)
f = (h * 6.0) - i
p, q, t = v * (1.0 - s), v * (1.0 - s * f), v * (1.0 - s * (1.0 - f))
i %= 6
if i == 0:
return (v, t, p)
if i == 1:
return (q, v, p)
if i == 2:
return (p, v, t)
if i == 3:
return (p, q, v)
if i == 4:
return (t, p, v)
if i == 5:
return (v, p, q)