diff --git a/mitreattack/navlayers/core/gradient.py b/mitreattack/navlayers/core/gradient.py index 7c9612e6..26203693 100644 --- a/mitreattack/navlayers/core/gradient.py +++ b/mitreattack/navlayers/core/gradient.py @@ -1,7 +1,5 @@ """Contains Gradient class.""" -import math - import colour from mitreattack.navlayers.core.exceptions import typeChecker, typeCheckerArray @@ -44,7 +42,6 @@ def colors(self, colors): else: entry = entry[:6] self.__colors.append(entry) - self._compute_curve() @property def minValue(self): @@ -55,7 +52,6 @@ def minValue(self): def minValue(self, minValue): typeChecker(type(self).__name__, minValue, int, "minValue") self.__minValue = minValue - self._compute_curve() @property def maxValue(self): @@ -66,38 +62,25 @@ def maxValue(self): def maxValue(self, maxValue): typeChecker(type(self).__name__, maxValue, int, "maxValue") self.__maxValue = maxValue - self._compute_curve() - - def _compute_curve(self): - """Compute the gradient color curve.""" - if self.maxValue is not None and self.minValue is not None and self.colors is not None: - chunksize = int(math.floor((self.maxValue - self.minValue) / (len(self.colors) - 1))) - fchunksize = int(math.ceil((self.maxValue - self.minValue) / (len(self.colors) - 1))) - self.curve = [] - index = 1 - while index < len(self.colors): - s_c = colour.Color(self.colors[index - 1]) - e_c = colour.Color(self.colors[index]) - if index == len(self.colors): - curve_2 = list(s_c.range_to(e_c, fchunksize)) - else: - curve_2 = list(s_c.range_to(e_c, chunksize)) - index += 1 - self.curve.extend(curve_2) - self.curve.append(colour.Color(self.colors[-1])) def compute_color(self, score): """Compute a specific color based on the score value provided. + Colors are interpolated linearly in RGB between evenly spaced gradient stops, as in the ATT&CK Navigator. + :returns: A hexadecimal color representation of the score on the gradient """ - if score <= self.minValue: - return self.curve[0].hex_l + stops = [colour.Color(color) for color in self.colors] + if score <= self.minValue or len(stops) == 1: + return stops[0].hex_l if score >= self.maxValue: - return self.curve[-1].hex_l + return stops[-1].hex_l - target = self.curve[int(score - self.minValue)] - return target.hex_l + position = (score - self.minValue) / (self.maxValue - self.minValue) * (len(stops) - 1) + index = min(int(position), len(stops) - 2) + fraction = position - index + start, end = stops[index].rgb, stops[index + 1].rgb + return colour.Color(rgb=tuple(s + (e - s) * fraction for s, e in zip(start, end, strict=True))).hex_l def get_dict(self): """Convert the currently loaded gradient file into a dict. diff --git a/tests/test_gradient.py b/tests/test_gradient.py new file mode 100644 index 00000000..a5c166e7 --- /dev/null +++ b/tests/test_gradient.py @@ -0,0 +1,43 @@ +"""Tests for the navlayers Gradient color computation.""" + +import pytest + +from mitreattack.navlayers.core.gradient import Gradient + + +def test_compute_color_at_and_beyond_bounds(): + """Scores at or outside the range map to the first and last colors.""" + gradient = Gradient(colors=["#ff6666", "#ffe766", "#8ec843"], minValue=0, maxValue=100) + assert gradient.compute_color(-5) == "#ff6666" + assert gradient.compute_color(0) == "#ff6666" + assert gradient.compute_color(100) == "#8ec843" + assert gradient.compute_color(150) == "#8ec843" + + +@pytest.mark.parametrize( + "score, expected", + [(1, "#323232"), (2, "#646464"), (3, "#969696"), (4, "#c8c8c8"), (5, "#d2d2d2"), (7, "#e6e6e6")], +) +def test_compute_color_interpolates_between_stops(score, expected): + """Intermediate scores are interpolated between the neighbouring stops.""" + gradient = Gradient(colors=["#000000", "#c8c8c8", "#f0f0f0"], minValue=0, maxValue=8) + assert gradient.compute_color(score) == expected + + +def test_compute_color_middle_stop(): + """A score at a stop position returns that stop color.""" + gradient = Gradient(colors=["#ff6666", "#ffe766", "#8ec843"], minValue=0, maxValue=4) + assert gradient.compute_color(2) == "#ffe766" + + +@pytest.mark.parametrize("score", [9, 10, 10.5, 10.99]) +def test_compute_color_range_not_divisible_by_stop_count(score): + """Ranges that do not divide evenly between the stops must not raise.""" + gradient = Gradient(colors=["#ff0000", "#00ff00", "#0000ff", "#ffffff"], minValue=0, maxValue=11) + assert gradient.compute_color(score).startswith("#") + + +def test_compute_color_single_color(): + """A single-color gradient returns that color for every score.""" + gradient = Gradient(colors=["#66b1ff"], minValue=0, maxValue=10) + assert gradient.compute_color(5) == "#66b1ff"