From 700cc89963c16c1dfd446368dff8b2b83e70db24 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 11 Oct 2016 19:58:22 +0200 Subject: [PATCH] fix(progressbar): Make sure index is within bounds --- include/drawtypes/progressbar.hpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/include/drawtypes/progressbar.hpp b/include/drawtypes/progressbar.hpp index 790afe1d..59cff9bb 100644 --- a/include/drawtypes/progressbar.hpp +++ b/include/drawtypes/progressbar.hpp @@ -40,12 +40,11 @@ namespace drawtypes { m_colors = forward(colors); } - string output(float percentage, bool floor_percentage = false) { + string output(float percentage) { if (m_colors.empty()) m_colors.emplace_back(m_fill->m_foreground); - float add = floor_percentage ? 0 : 0.5; - int fill_width = (int)m_width * percentage / 100 + add; + int fill_width = m_width * percentage / 100.0f + 0.5f; int empty_width = m_width - fill_width; int color_step = m_width / m_colors.size() + 0.5f; @@ -61,11 +60,11 @@ namespace drawtypes { } if (!m_gradient) { - auto color = m_colors.size() * percentage / 100; - if (color >= m_colors.size()) - color = 0; - m_fill->m_foreground = m_colors[color - 1]; - while (fill_width--) m_builder->node(m_fill); + auto idx = static_cast((m_colors.size() - 1) * percentage / 100.0f + 0.5f); + m_fill->m_foreground = m_colors[idx]; + while (fill_width--) { + m_builder->node(m_fill); + } } else { int i = 0; for (auto color : m_colors) {