From 26f827aa1ba130fdccd6f81d2107b49f5129b921 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 3 Jun 2020 17:39:09 +0500 Subject: [PATCH 1/2] py3. Fix get_colors. --- core/colors.py | 10 +++++----- tests/test_colors.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 tests/test_colors.py diff --git a/core/colors.py b/core/colors.py index 3013b5f271..566f9222c5 100644 --- a/core/colors.py +++ b/core/colors.py @@ -20,9 +20,9 @@ def hsv_to_rgb(h, s, v): h = float(h) hi = math.floor(h / 60.0) % 6 f = (h / 60.0) - math.floor(h / 60.0) - p = v * (1.0 - s) - q = v * (1.0 - (f * s)) - t = v * (1.0 - ((1.0 - f) * s)) + p = int(v * (1.0 - s)) + q = int(v * (1.0 - (f * s))) + t = int(v * (1.0 - ((1.0 - f) * s))) return {0: (v, t, p), 1: (q, v, p), 2: (p, v, t), 3: (p, q, v), 4: (t, p, v), 5: (v, p, q)}[hi] @@ -48,7 +48,7 @@ def get_colors(N): if not hs: # Rebuild colors hs = [i * d for i in range(p)] - H = hs.pop(len(hs) / 2 if N % 2 else 0) + H = hs.pop(int(len(hs) / 2) if N % 2 else 0) # Yield current color yield "#%02x%02x%02x" % (hsv_to_rgb(H, S, V)) N = N - 1 @@ -71,7 +71,7 @@ def get_float_pallete(n): if not hs: # Rebuild colors hs = [i * d for i in range(p)] - h = hs.pop(len(hs) / 2 if n % 2 else 0) + h = hs.pop(int(len(hs) / 2) if n % 2 else 0) # Yield current color yield [float(x) / 256.0 for x in hsv_to_rgb(h, s, v)] n = n - 1 diff --git a/tests/test_colors.py b/tests/test_colors.py new file mode 100644 index 0000000000..ec6b36ebf1 --- /dev/null +++ b/tests/test_colors.py @@ -0,0 +1,28 @@ +# ---------------------------------------------------------------------- +# noc.core.colors unittests +# ---------------------------------------------------------------------- +# Copyright (C) 2007-2019 The NOC Project +# See LICENSE for details +# ---------------------------------------------------------------------- + +# Third-party modules +import pytest + +# NOC modules +from noc.core.colors import get_colors + + +@pytest.mark.parametrize( + "config,expected", + [ + (1, ["#ff0000"]), + (2, ["#ff0000", "#00ffff"]), + (3, ["#00ff00", "#ff0000", "#0000ff"]), + (4, ["#ff0000", "#00ffff", "#7fff00", "#7f00ff"]), + ], +) +def test_color(config, expected): + """ + :return: + """ + assert list(get_colors(config)) == expected -- GitLab From 99e08a997da32cbeea3027d40e3b59982d55cc15 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 3 Jun 2020 17:54:43 +0500 Subject: [PATCH 2/2] Fix typo. --- core/colors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/colors.py b/core/colors.py index 566f9222c5..2865f25e17 100644 --- a/core/colors.py +++ b/core/colors.py @@ -48,7 +48,7 @@ def get_colors(N): if not hs: # Rebuild colors hs = [i * d for i in range(p)] - H = hs.pop(int(len(hs) / 2) if N % 2 else 0) + H = hs.pop(len(hs) // 2 if N % 2 else 0) # Yield current color yield "#%02x%02x%02x" % (hsv_to_rgb(H, S, V)) N = N - 1 @@ -71,7 +71,7 @@ def get_float_pallete(n): if not hs: # Rebuild colors hs = [i * d for i in range(p)] - h = hs.pop(int(len(hs) / 2) if n % 2 else 0) + h = hs.pop(len(hs) // 2 if n % 2 else 0) # Yield current color yield [float(x) / 256.0 for x in hsv_to_rgb(h, s, v)] n = n - 1 -- GitLab