Skip to content

Commit bec970c

Browse files
committed
exactly same as old version
1 parent 9da94b0 commit bec970c

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

branca/utilities.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,17 @@ 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 = []
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
77-
start, end = input_color_bytes[idx], input_color_bytes[idx + 1]
72+
resolution = 765
73+
n_indexes = resolution * (len(hexList) - 1)
74+
result: List[str] = []
75+
for counter in range(nColors - 1):
76+
fraction_overall = float(counter) / (nColors - 1)
77+
index_overall = int(fraction_overall * (n_indexes - 1))
78+
index_color_bin = index_overall % 765
79+
idx_input = index_overall // 765
80+
fraction = index_color_bin / (resolution - 1)
81+
start = input_color_bytes[idx_input]
82+
end = input_color_bytes[idx_input + 1]
7883
new_color_bytes = [int(x + fraction * (y - x)) for x, y in zip(start, end)]
7984
new_color_hexs = [hex(x)[2:].zfill(2) for x in new_color_bytes]
8085
result.append("#" + "".join(new_color_hexs))

tests/test_utilities.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,4 @@ def test_linear_gradient(
219219
expected_output: List[str],
220220
):
221221
result = ut.linear_gradient(hex_list, n_colors)
222-
assert len(result) == len(expected_output), "Output length mismatch"
223-
224-
# because we rewrote this function, we allow a difference of 1 between the old and the new version
225-
tolerance = 1
226-
for actual, expected in zip(result, expected_output):
227-
r1, g1, b1 = (int(actual[i : i + 2], 16) for i in (1, 3, 5))
228-
r2, g2, b2 = (int(expected[i : i + 2], 16) for i in (1, 3, 5))
229-
if not all(abs(a - b) <= tolerance for a, b in zip((r1, g1, b1), (r2, g2, b2))):
230-
# to get a nice output, assert the full array when we spot a failure
231-
assert result == expected_output
222+
assert result == expected_output

0 commit comments

Comments
 (0)