From 87cf71ce771436aedbb17ac07b1db6dda1530ea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Garc=C3=ADa=20Garz=C3=B3n?= Date: Tue, 26 Nov 2019 19:56:09 +0100 Subject: [PATCH 1/2] color_scale uses the shorter cycle path by default --- colour.py | 67 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/colour.py b/colour.py index 1bd48e8..0e610e5 100644 --- a/colour.py +++ b/colour.py @@ -690,16 +690,52 @@ def web2hex(web, force_long=False): hsl2web = lambda x: rgb2web(hsl2rgb(x)) -def color_scale(begin_hsl, end_hsl, nb): +def color_scale(begin_hsl, end_hsl, nb, longer=False): """Returns a list of nb color HSL tuples between begin_hsl and end_hsl >>> from colour import color_scale - >>> [rgb2hex(hsl2rgb(hsl)) for hsl in color_scale((0, 1, 0.5), - ... (1, 1, 0.5), 3)] + >>> [rgb2hex(hsl2rgb(hsl)) for hsl in color_scale((0, 1, 0.5), # longer, as is + ... (1, 1, 0.5), 3, longer=True)] ['#f00', '#0f0', '#00f', '#f00'] - >>> [rgb2hex(hsl2rgb(hsl)) + >>> [rgb2hex(hsl2rgb(hsl)) for hsl in color_scale( # longer, as is, backwards + ... (1, 1, 0.5), + ... (0, 1, 0.5), + ... 3, longer=True)] + ['#f00', '#00f', '#0f0', '#f00'] + + >>> [rgb2hex(hsl2rgb(hsl)) for hsl in color_scale( # shorter, h1 incremented + ... (0, 1, 0.5), # lower red + ... (1, 1, 0.5), # higher red + ... 3)] + ['#f00', '#f00', '#f00', '#f00'] + + >>> [rgb2hex(hsl2rgb(hsl)) for hsl in color_scale( # shorter, h2 incremented + ... (1, 1, 0.5), # lower red + ... (0, 1, 0.5), # higher red + ... 3)] + ['#f00', '#f00', '#f00', '#f00'] + + >>> [rgb2hex(hsl2rgb(hsl)) for hsl in color_scale( # shorter, as is + ... (1./3, 1, 0.5), # green + ... (2./3, 1, 0.5), # blue + ... 3)] + ['#0f0', '#0fa', '#0af', '#00f'] + + >>> [rgb2hex(hsl2rgb(hsl)) for hsl in color_scale( # longer, h1 incremented + ... (1./3, 1, 0.5), # green + ... (2./3, 1, 0.5), # blue + ... 3, longer=True)] + ['#0f0', '#fa0', '#f0a', '#00f'] + + >>> [rgb2hex(hsl2rgb(hsl)) for hsl in color_scale( # longer, h2 incremented + ... (2./3, 1, 0.5), # green + ... (1./3, 1, 0.5), # blue + ... 3, longer=True)] + ['#00f', '#f0a', '#fa0', '#0f0'] + + >>> [rgb2hex(hsl2rgb(hsl)) #luminosity interpolation ... for hsl in color_scale((0, 0, 0), ... (0, 0, 1), ... 15)] # doctest: +ELLIPSIS @@ -718,16 +754,19 @@ def color_scale(begin_hsl, end_hsl, nb): raise ValueError( "Unsupported negative number of colors (nb=%r)." % nb) - step = tuple([float(end_hsl[i] - begin_hsl[i]) / nb for i in range(0, 3)]) \ - if nb > 0 else (0, 0, 0) - - def mul(step, value): - return tuple([v * value for v in step]) - - def add_v(step, step2): - return tuple([v + step2[i] for i, v in enumerate(step)]) - - return [add_v(begin_hsl, mul(step, r)) for r in range(0, nb + 1)] + h1,s1,l1 = begin_hsl + h2,s2,l2 = end_hsl + if longer == (abs(h1-h2)<0.5): + if h1 Date: Tue, 26 Nov 2019 20:47:07 +0100 Subject: [PATCH 2/2] added the longer option to range_to as well --- README.rst | 4 +++- colour.py | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index a64f505..18bb1ad 100644 --- a/README.rst +++ b/README.rst @@ -187,7 +187,9 @@ easily. Here, is the color scale of the rainbow between red and blue:: >>> red = Color("red") >>> blue = Color("blue") - >>> list(red.range_to(blue, 5)) + >>> list(red.range_to(blue, 3)) + [, , ] + >>> list(red.range_to(blue, 5, longer=True)) [, , , , ] Or the different amount of gray between black and white:: diff --git a/colour.py b/colour.py index 0e610e5..cd8c597 100644 --- a/colour.py +++ b/colour.py @@ -1124,8 +1124,8 @@ def set_web(self, value): ## range of color generation - def range_to(self, value, steps): - for hsl in color_scale(self._hsl, Color(value).hsl, steps - 1): + def range_to(self, value, steps, longer=False): + for hsl in color_scale(self._hsl, Color(value).hsl, steps - 1, longer=longer): yield Color(hsl=hsl) ##