From 393ff25b91014739ec1ffae3c8770bab9234f671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20K=2E=20Guti=C3=A9rrez?= Date: Thu, 25 Apr 2024 07:30:13 -0600 Subject: [PATCH] Fix color clamping code. (#117) My last modification to clamp_colors() broke it. Signed-off-by: Samuel K. Gutierrez --- src/qvi-scope.cc | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/qvi-scope.cc b/src/qvi-scope.cc index d503eae5..04878174 100644 --- a/src/qvi-scope.cc +++ b/src/qvi-scope.cc @@ -963,14 +963,25 @@ agg_split_affinity_preserving( } /** - * Takes a vector of colors and clamps their values to [0, values.size()) in - * place. + * Takes a vector of colors and clamps their values to [0, ndc) + * in place, where ndc is the number of distinct numbers found in values. */ static int clamp_colors( std::vector &values ) { - std::iota(values.begin(), values.end(), 0); + // Recall: sets are ordered. + std::set valset(values.begin(), values.end()); + // Maps the input vector colors to their clamped values. + std::map colors2clamped; + // color': the clamped color. + int colorp = 0; + for (auto val : valset) { + colors2clamped.insert({val, colorp++}); + } + for (uint_t i = 0; i < values.size(); ++i) { + values[i] = colors2clamped[values[i]]; + } return QV_SUCCESS; }