diff --git a/core/colors.py b/core/colors.py index 3013b5f271f3aacc5d19571fb6e486d21e8981fd..2865f25e174a139b64f917170a32e7b9d3fac443 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(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(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 0000000000000000000000000000000000000000..ec6b36ebf11c51f1b8a8c0a21af318a712019082 --- /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