Skip to content

Commit ce07c6f

Browse files
committed
direct for-loop
1 parent f8c40a9 commit ce07c6f

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

branca/utilities.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,15 @@ def linear_gradient(hexList: List[str], nColors: int) -> List[str]:
6969
input_color_bytes = [
7070
[int(_hex[i : i + 2], 16) for i in (1, 3, 5)] for _hex in hexList
7171
]
72-
result: List[str] = []
73-
step_size = (len(hexList) - 1) / (nColors - 1)
74-
step = 0.0
75-
idx = 0
76-
while len(result) < nColors - 1:
72+
result = []
73+
for output_idx in range(nColors - 1):
74+
output_fraction = (len(hexList) - 1) * output_idx / (nColors - 1)
75+
idx = int(output_fraction)
76+
fraction = output_fraction - idx
7777
start, end = input_color_bytes[idx], input_color_bytes[idx + 1]
78-
new_color_bytes = [int(x + step * (y - x)) for x, y in zip(start, end)]
78+
new_color_bytes = [int(x + fraction * (y - x)) for x, y in zip(start, end)]
7979
new_color_hexs = [hex(x)[2:].zfill(2) for x in new_color_bytes]
8080
result.append("#" + "".join(new_color_hexs))
81-
step += step_size
82-
if step > 1:
83-
step -= 1
84-
idx += 1
8581

8682
result.append(hexList[-1].lower())
8783
return result

0 commit comments

Comments
 (0)