From e9bed94ba40e50932d0366304abfefc2a889bd9f Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Sat, 15 Mar 2025 17:10:21 +0100 Subject: [PATCH 01/12] faster linear_gradient --- branca/utilities.py | 31 +++++++++-------------------- tests/test_utilities.py | 44 ++++++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/branca/utilities.py b/branca/utilities.py index 14a1d46..e59c916 100644 --- a/branca/utilities.py +++ b/branca/utilities.py @@ -66,33 +66,20 @@ def linear_gradient(hexList: List[str], nColors: int) -> List[str]: nColors where the colors are linearly interpolated between the (r, g, b) tuples that are given. """ - - def _scale(start, finish, length, i): - """ - Return the value correct value of a number that is in between start - and finish, for use in a loop of length *length*. - - """ - base = 16 - - fraction = float(i) / (length - 1) - raynge = int(finish, base) - int(start, base) - thex = hex(int(int(start, base) + fraction * raynge)).split("x")[-1] - if len(thex) != 2: - thex = "0" + thex - return thex - allColors: List[str] = [] # Separate (R, G, B) pairs. for start, end in zip(hexList[:-1], hexList[1:]): # Linearly interpolate between pair of hex ###### values and # add to list. - nInterpolate = 765 - for index in range(nInterpolate): - r = _scale(start[1:3], end[1:3], nInterpolate, index) - g = _scale(start[3:5], end[3:5], nInterpolate, index) - b = _scale(start[5:7], end[5:7], nInterpolate, index) - allColors.append("".join(["#", r, g, b])) + start = [int(start[i:i+2], 16) for i in (1, 3, 5)] + end = [int(end[i:i+2], 16) for i in (1, 3, 5)] + + n_interpolate = 765 + for i in range(n_interpolate): + frac = i / (n_interpolate - 1) + byte_ints = [int(x + (y - x) * frac) for x, y in zip(start, end)] + hexs = [hex(x)[2:].zfill(2) for x in byte_ints] + allColors.append("#" + "".join(hexs)) # Pick only nColors colors from the total list. result: List[str] = [] diff --git a/tests/test_utilities.py b/tests/test_utilities.py index a5b83d0..061f47e 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -1,6 +1,7 @@ import json import os from pathlib import Path +from typing import List import pytest @@ -42,7 +43,8 @@ def test_color_brewer_reverse(): assert scheme[::-1] == scheme_r -def test_color_brewer_extendability(): +@pytest.mark.parametrize("sname", core_schemes) +def test_color_brewer_extendability(sname): """ The non-qualitative schemes should be extendable. @@ -54,21 +56,20 @@ def test_color_brewer_extendability(): Indeed, in color_brewer, the key searched in the scheme database was not found, thus, it was passing `None` instead of a real scheme vector to linear_gradient. """ - for sname in core_schemes: - for n in range(color_brewer_minimum_n, color_brewer_maximum_n + 1): - try: - scheme = ut.color_brewer(sname, n=n) - except Exception as e: - if scheme_info[sname] == "Qualitative" and isinstance(e, ValueError): - continue - raise + for n in range(color_brewer_minimum_n, color_brewer_maximum_n + 1): + try: + scheme = ut.color_brewer(sname, n=n) + except Exception as e: + if scheme_info[sname] == "Qualitative" and isinstance(e, ValueError): + continue + raise - assert len(scheme) == n + assert len(scheme) == n - # When we try to extend a scheme, - # the reverse is not always the exact reverse vector of the original one. - # Thus, we do not test this property! - _ = ut.color_brewer(sname + "_r", n=n) + # When we try to extend a scheme, + # the reverse is not always the exact reverse vector of the original one. + # Thus, we do not test this property! + _ = ut.color_brewer(sname + "_r", n=n) def test_color_avoid_unexpected_error(): @@ -169,3 +170,18 @@ def test_write_png_rgb(): ] png = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x04\x00\x00\x00\x02\x08\x06\x00\x00\x00\x7f\xa8}c\x00\x00\x00-IDATx\xda\x01"\x00\xdd\xff\x00\xff\xa7G\xffp\xff+\xff\x9e\x1cH\xff9\x90$\xff\x00\x93\xe9\xb8\xff\x0cz\xe2\xff\xc6\xca\xff\xff\xd4W\xd0\xffYw\x15\x95\xcf\xb9@D\x00\x00\x00\x00IEND\xaeB`\x82' # noqa E501 assert ut.write_png(image_rgb) == png + + +@pytest.mark.parametrize( + "hex_list, n_colors, expected_output", + [ + (["#000000", "#FFFFFF"], 2, ["#000000", "#ffffff"]), + (["#FF0000", "#00FF00", "#0000FF"], 3, ["#ff0000", "#00ff00", "#0000ff"]), + (["#000000", "#0000FF"], 5, ['#000000', '#00003f', '#00007f', '#0000bf', '#0000ff']), + (["#FFFFFF", "#000000"], 5, ['#ffffff', '#bfbfbf', '#7f7f7f', '#3f3f3f', '#000000']), + (["#FF0000", "#00FF00", "#0000FF"], 7, ['#ff0000', '#aa5400', '#55a900', '#00ff00', '#00aa54', '#0055a9', '#0000ff']), + ] +) +def test_linear_gradient(hex_list: List[str], n_colors: int, expected_output: List[str]): + result = ut.linear_gradient(hex_list, n_colors) + assert result == expected_output From a94b338ad511c87d6fe448e5c160ebb28fed889e Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Sat, 15 Mar 2025 17:32:32 +0100 Subject: [PATCH 02/12] more test cases --- tests/test_utilities.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 061f47e..03a3f43 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -176,9 +176,12 @@ def test_write_png_rgb(): "hex_list, n_colors, expected_output", [ (["#000000", "#FFFFFF"], 2, ["#000000", "#ffffff"]), + (["#000000", "#FFFFFF"], 4, ['#000000', '#545454', '#a9a9a9', '#ffffff']), (["#FF0000", "#00FF00", "#0000FF"], 3, ["#ff0000", "#00ff00", "#0000ff"]), + (["#FF0000", "#00FF00", "#0000FF"], 4, ['#ff0000', '#55a900', '#00aa54', '#0000ff']), (["#000000", "#0000FF"], 5, ['#000000', '#00003f', '#00007f', '#0000bf', '#0000ff']), (["#FFFFFF", "#000000"], 5, ['#ffffff', '#bfbfbf', '#7f7f7f', '#3f3f3f', '#000000']), + (["#FF0000", "#00FF00", "#0000FF"], 5, ['#ff0000', '#7f7f00', '#00ff00', '#007f7f', '#0000ff']), (["#FF0000", "#00FF00", "#0000FF"], 7, ['#ff0000', '#aa5400', '#55a900', '#00ff00', '#00aa54', '#0055a9', '#0000ff']), ] ) From 7deb5cd6ff388e143fd41536733ff40178dc1d21 Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Sat, 15 Mar 2025 17:54:42 +0100 Subject: [PATCH 03/12] don't precompute interpolation values --- branca/utilities.py | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/branca/utilities.py b/branca/utilities.py index e59c916..0426064 100644 --- a/branca/utilities.py +++ b/branca/utilities.py @@ -66,27 +66,22 @@ def linear_gradient(hexList: List[str], nColors: int) -> List[str]: nColors where the colors are linearly interpolated between the (r, g, b) tuples that are given. """ - allColors: List[str] = [] - # Separate (R, G, B) pairs. - for start, end in zip(hexList[:-1], hexList[1:]): - # Linearly interpolate between pair of hex ###### values and - # add to list. - start = [int(start[i:i+2], 16) for i in (1, 3, 5)] - end = [int(end[i:i+2], 16) for i in (1, 3, 5)] - - n_interpolate = 765 - for i in range(n_interpolate): - frac = i / (n_interpolate - 1) - byte_ints = [int(x + (y - x) * frac) for x, y in zip(start, end)] - hexs = [hex(x)[2:].zfill(2) for x in byte_ints] - allColors.append("#" + "".join(hexs)) - - # Pick only nColors colors from the total list. + input_color_bytes = [[int(_hex[i:i + 2], 16) for i in (1, 3, 5)] for _hex in hexList] result: List[str] = [] - for counter in range(nColors): - fraction = float(counter) / (nColors - 1) - index = int(fraction * (len(allColors) - 1)) - result.append(allColors[index]) + step_size = (len(hexList) - 1) / (nColors - 1) + step = 0 + idx = 0 + while len(result) < nColors - 1: + start, end = input_color_bytes[idx], input_color_bytes[idx + 1] + new_color_bytes = [int(x + step * (y - x)) for x, y in zip(start, end)] + new_color_hexs = [hex(x)[2:].zfill(2) for x in new_color_bytes] + result.append("#" + "".join(new_color_hexs)) + step += step_size + if step > 1: + step -= 1 + idx += 1 + + result.append(hexList[-1].lower()) return result From 9da9d20aafe64b9e768811809fb066887e836e92 Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Sat, 15 Mar 2025 17:59:00 +0100 Subject: [PATCH 04/12] revert change in test_color_brewer_extendability --- tests/test_utilities.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 03a3f43..c562c6e 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -43,8 +43,7 @@ def test_color_brewer_reverse(): assert scheme[::-1] == scheme_r -@pytest.mark.parametrize("sname", core_schemes) -def test_color_brewer_extendability(sname): +def test_color_brewer_extendability(): """ The non-qualitative schemes should be extendable. @@ -56,20 +55,21 @@ def test_color_brewer_extendability(sname): Indeed, in color_brewer, the key searched in the scheme database was not found, thus, it was passing `None` instead of a real scheme vector to linear_gradient. """ - for n in range(color_brewer_minimum_n, color_brewer_maximum_n + 1): - try: - scheme = ut.color_brewer(sname, n=n) - except Exception as e: - if scheme_info[sname] == "Qualitative" and isinstance(e, ValueError): - continue - raise - - assert len(scheme) == n - - # When we try to extend a scheme, - # the reverse is not always the exact reverse vector of the original one. - # Thus, we do not test this property! - _ = ut.color_brewer(sname + "_r", n=n) + for sname in core_schemes: + for n in range(color_brewer_minimum_n, color_brewer_maximum_n + 1): + try: + scheme = ut.color_brewer(sname, n=n) + except Exception as e: + if scheme_info[sname] == "Qualitative" and isinstance(e, ValueError): + continue + raise + + assert len(scheme) == n + + # When we try to extend a scheme, + # the reverse is not always the exact reverse vector of the original one. + # Thus, we do not test this property! + _ = ut.color_brewer(sname + "_r", n=n) def test_color_avoid_unexpected_error(): From 1fb755f819129b8cd346cbd0a7b242e391e98ce6 Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Sat, 15 Mar 2025 18:26:35 +0100 Subject: [PATCH 05/12] make tests more lenient --- tests/test_utilities.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index c562c6e..50ec36c 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -187,4 +187,13 @@ def test_write_png_rgb(): ) def test_linear_gradient(hex_list: List[str], n_colors: int, expected_output: List[str]): result = ut.linear_gradient(hex_list, n_colors) - assert result == expected_output + assert len(result) == len(expected_output), "Output length mismatch" + + # because we rewrote this function, we allow a difference of 1 between the old and the new version + tolerance = 1 + for actual, expected in zip(result, expected_output): + r1, g1, b1 = (int(actual[i:i+2], 16) for i in (1, 3, 5)) + r2, g2, b2 = (int(expected[i:i+2], 16) for i in (1, 3, 5)) + if not all(abs(a - b) <= tolerance for a, b in zip((r1, g1, b1), (r2, g2, b2))): + # to get a nice output, assert the full array when we spot a failure + assert result == expected_output From c44a0a438b0c46c5188368116bcbb940bb0cb5da Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Sat, 15 Mar 2025 18:33:18 +0100 Subject: [PATCH 06/12] run precommit flake --- branca/utilities.py | 4 +++- tests/test_utilities.py | 50 ++++++++++++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/branca/utilities.py b/branca/utilities.py index 0426064..4d70470 100644 --- a/branca/utilities.py +++ b/branca/utilities.py @@ -66,7 +66,9 @@ def linear_gradient(hexList: List[str], nColors: int) -> List[str]: nColors where the colors are linearly interpolated between the (r, g, b) tuples that are given. """ - input_color_bytes = [[int(_hex[i:i + 2], 16) for i in (1, 3, 5)] for _hex in hexList] + input_color_bytes = [ + [int(_hex[i : i + 2], 16) for i in (1, 3, 5)] for _hex in hexList + ] result: List[str] = [] step_size = (len(hexList) - 1) / (nColors - 1) step = 0 diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 50ec36c..2f6843e 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -176,24 +176,54 @@ def test_write_png_rgb(): "hex_list, n_colors, expected_output", [ (["#000000", "#FFFFFF"], 2, ["#000000", "#ffffff"]), - (["#000000", "#FFFFFF"], 4, ['#000000', '#545454', '#a9a9a9', '#ffffff']), + (["#000000", "#FFFFFF"], 4, ["#000000", "#545454", "#a9a9a9", "#ffffff"]), (["#FF0000", "#00FF00", "#0000FF"], 3, ["#ff0000", "#00ff00", "#0000ff"]), - (["#FF0000", "#00FF00", "#0000FF"], 4, ['#ff0000', '#55a900', '#00aa54', '#0000ff']), - (["#000000", "#0000FF"], 5, ['#000000', '#00003f', '#00007f', '#0000bf', '#0000ff']), - (["#FFFFFF", "#000000"], 5, ['#ffffff', '#bfbfbf', '#7f7f7f', '#3f3f3f', '#000000']), - (["#FF0000", "#00FF00", "#0000FF"], 5, ['#ff0000', '#7f7f00', '#00ff00', '#007f7f', '#0000ff']), - (["#FF0000", "#00FF00", "#0000FF"], 7, ['#ff0000', '#aa5400', '#55a900', '#00ff00', '#00aa54', '#0055a9', '#0000ff']), - ] + ( + ["#FF0000", "#00FF00", "#0000FF"], + 4, + ["#ff0000", "#55a900", "#00aa54", "#0000ff"], + ), + ( + ["#000000", "#0000FF"], + 5, + ["#000000", "#00003f", "#00007f", "#0000bf", "#0000ff"], + ), + ( + ["#FFFFFF", "#000000"], + 5, + ["#ffffff", "#bfbfbf", "#7f7f7f", "#3f3f3f", "#000000"], + ), + ( + ["#FF0000", "#00FF00", "#0000FF"], + 5, + ["#ff0000", "#7f7f00", "#00ff00", "#007f7f", "#0000ff"], + ), + ( + ["#FF0000", "#00FF00", "#0000FF"], + 7, + [ + "#ff0000", + "#aa5400", + "#55a900", + "#00ff00", + "#00aa54", + "#0055a9", + "#0000ff", + ], + ), + ], ) -def test_linear_gradient(hex_list: List[str], n_colors: int, expected_output: List[str]): +def test_linear_gradient( + hex_list: List[str], n_colors: int, expected_output: List[str], +): result = ut.linear_gradient(hex_list, n_colors) assert len(result) == len(expected_output), "Output length mismatch" # because we rewrote this function, we allow a difference of 1 between the old and the new version tolerance = 1 for actual, expected in zip(result, expected_output): - r1, g1, b1 = (int(actual[i:i+2], 16) for i in (1, 3, 5)) - r2, g2, b2 = (int(expected[i:i+2], 16) for i in (1, 3, 5)) + r1, g1, b1 = (int(actual[i : i + 2], 16) for i in (1, 3, 5)) + r2, g2, b2 = (int(expected[i : i + 2], 16) for i in (1, 3, 5)) if not all(abs(a - b) <= tolerance for a, b in zip((r1, g1, b1), (r2, g2, b2))): # to get a nice output, assert the full array when we spot a failure assert result == expected_output From 67d8b68045de0c81b8ebdc2cf7f50c123369e59c Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Sat, 15 Mar 2025 18:35:34 +0100 Subject: [PATCH 07/12] run black again --- tests/test_utilities.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 2f6843e..2f56b81 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -214,7 +214,9 @@ def test_write_png_rgb(): ], ) def test_linear_gradient( - hex_list: List[str], n_colors: int, expected_output: List[str], + hex_list: List[str], + n_colors: int, + expected_output: List[str], ): result = ut.linear_gradient(hex_list, n_colors) assert len(result) == len(expected_output), "Output length mismatch" From 00c703b1d89676e65c8f66d8927962c56b7d70a8 Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Sat, 15 Mar 2025 18:41:58 +0100 Subject: [PATCH 08/12] mypy --- branca/utilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/branca/utilities.py b/branca/utilities.py index 4d70470..6cded0c 100644 --- a/branca/utilities.py +++ b/branca/utilities.py @@ -71,7 +71,7 @@ def linear_gradient(hexList: List[str], nColors: int) -> List[str]: ] result: List[str] = [] step_size = (len(hexList) - 1) / (nColors - 1) - step = 0 + step = 0.0 idx = 0 while len(result) < nColors - 1: start, end = input_color_bytes[idx], input_color_bytes[idx + 1] From 9da94b0473cc3e8bb4949f894e00f28e02e67423 Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Sat, 15 Mar 2025 18:54:29 +0100 Subject: [PATCH 09/12] direct for-loop --- branca/utilities.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/branca/utilities.py b/branca/utilities.py index 6cded0c..a4c528f 100644 --- a/branca/utilities.py +++ b/branca/utilities.py @@ -69,19 +69,15 @@ def linear_gradient(hexList: List[str], nColors: int) -> List[str]: input_color_bytes = [ [int(_hex[i : i + 2], 16) for i in (1, 3, 5)] for _hex in hexList ] - result: List[str] = [] - step_size = (len(hexList) - 1) / (nColors - 1) - step = 0.0 - idx = 0 - while len(result) < nColors - 1: + result = [] + for output_idx in range(nColors - 1): + output_fraction = (len(hexList) - 1) * output_idx / (nColors - 1) + idx = int(output_fraction) + fraction = output_fraction - idx start, end = input_color_bytes[idx], input_color_bytes[idx + 1] - new_color_bytes = [int(x + step * (y - x)) for x, y in zip(start, end)] + new_color_bytes = [int(x + fraction * (y - x)) for x, y in zip(start, end)] new_color_hexs = [hex(x)[2:].zfill(2) for x in new_color_bytes] result.append("#" + "".join(new_color_hexs)) - step += step_size - if step > 1: - step -= 1 - idx += 1 result.append(hexList[-1].lower()) return result From bec970c79d55fb1bbc28d9ae65df029c29380ad0 Mon Sep 17 00:00:00 2001 From: Conengmo <33519926+Conengmo@users.noreply.github.com> Date: Sat, 15 Mar 2025 22:10:10 +0100 Subject: [PATCH 10/12] exactly same as old version --- branca/utilities.py | 17 +++++++++++------ tests/test_utilities.py | 11 +---------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/branca/utilities.py b/branca/utilities.py index a4c528f..1109886 100644 --- a/branca/utilities.py +++ b/branca/utilities.py @@ -69,12 +69,17 @@ def linear_gradient(hexList: List[str], nColors: int) -> List[str]: input_color_bytes = [ [int(_hex[i : i + 2], 16) for i in (1, 3, 5)] for _hex in hexList ] - result = [] - for output_idx in range(nColors - 1): - output_fraction = (len(hexList) - 1) * output_idx / (nColors - 1) - idx = int(output_fraction) - fraction = output_fraction - idx - start, end = input_color_bytes[idx], input_color_bytes[idx + 1] + resolution = 765 + n_indexes = resolution * (len(hexList) - 1) + result: List[str] = [] + for counter in range(nColors - 1): + fraction_overall = float(counter) / (nColors - 1) + index_overall = int(fraction_overall * (n_indexes - 1)) + index_color_bin = index_overall % 765 + idx_input = index_overall // 765 + fraction = index_color_bin / (resolution - 1) + start = input_color_bytes[idx_input] + end = input_color_bytes[idx_input + 1] new_color_bytes = [int(x + fraction * (y - x)) for x, y in zip(start, end)] new_color_hexs = [hex(x)[2:].zfill(2) for x in new_color_bytes] result.append("#" + "".join(new_color_hexs)) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 2f56b81..cef73fd 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -219,13 +219,4 @@ def test_linear_gradient( expected_output: List[str], ): result = ut.linear_gradient(hex_list, n_colors) - assert len(result) == len(expected_output), "Output length mismatch" - - # because we rewrote this function, we allow a difference of 1 between the old and the new version - tolerance = 1 - for actual, expected in zip(result, expected_output): - r1, g1, b1 = (int(actual[i : i + 2], 16) for i in (1, 3, 5)) - r2, g2, b2 = (int(expected[i : i + 2], 16) for i in (1, 3, 5)) - if not all(abs(a - b) <= tolerance for a, b in zip((r1, g1, b1), (r2, g2, b2))): - # to get a nice output, assert the full array when we spot a failure - assert result == expected_output + assert result == expected_output From 1147eb0a4cc1db0323e94e448ba1f32b1c01b497 Mon Sep 17 00:00:00 2001 From: Conengmo <33519926+Conengmo@users.noreply.github.com> Date: Sat, 15 Mar 2025 23:01:50 +0100 Subject: [PATCH 11/12] Update utilities.py --- branca/utilities.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/branca/utilities.py b/branca/utilities.py index 1109886..ec280f3 100644 --- a/branca/utilities.py +++ b/branca/utilities.py @@ -69,14 +69,16 @@ def linear_gradient(hexList: List[str], nColors: int) -> List[str]: input_color_bytes = [ [int(_hex[i : i + 2], 16) for i in (1, 3, 5)] for _hex in hexList ] + # to have the same output as the previous version of this function we use + # a resolution of 765 'indexes' per color bin. resolution = 765 n_indexes = resolution * (len(hexList) - 1) result: List[str] = [] for counter in range(nColors - 1): fraction_overall = float(counter) / (nColors - 1) index_overall = int(fraction_overall * (n_indexes - 1)) - index_color_bin = index_overall % 765 - idx_input = index_overall // 765 + index_color_bin = index_overall % resolution + idx_input = index_overall // resolution fraction = index_color_bin / (resolution - 1) start = input_color_bytes[idx_input] end = input_color_bytes[idx_input + 1] From a73f1cc6c386748b1192f12fba00f63b5c8b2038 Mon Sep 17 00:00:00 2001 From: Conengmo <33519926+Conengmo@users.noreply.github.com> Date: Sun, 16 Mar 2025 11:54:27 +0100 Subject: [PATCH 12/12] more tests --- tests/test_utilities.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index cef73fd..754fbb1 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -211,6 +211,34 @@ def test_write_png_rgb(): "#0000ff", ], ), + ( + ["#abcdef", "#010603", "#f7f9f3"], + 4, + ["#abcdef", "#394851", "#525652", "#f7f9f3"], + ), + ( + ["#abcdef", "#010603", "#f7f9f3"], + 7, + [ + "#abcdef", + "#728aa0", + "#394851", + "#010603", + "#525652", + "#a4a7a2", + "#f7f9f3", + ], + ), + ( + ["#00abff", "#ff00ab", "#abff00", "#00abff"], + 4, + ["#00abff", "#ff00ab", "#abff00", "#00abff"], + ), + ( + ["#00abff", "#ff00ab", "#abff00", "#00abff"], + 6, + ["#00abff", "#9844cc", "#ee3288", "#bbcb22", "#66dd65", "#00abff"], + ), ], ) def test_linear_gradient(