diff --git a/apps/regression/calculation_controller.cpp b/apps/regression/calculation_controller.cpp index 4d24c7c26..548241564 100644 --- a/apps/regression/calculation_controller.cpp +++ b/apps/regression/calculation_controller.cpp @@ -168,6 +168,7 @@ void CalculationController::willDisplayCellAtLocation(HighlightCell * cell, int myCell->setFirstText(buffer); buffer[0] = 'Y'; myCell->setSecondText(buffer); + assert(seriesNumber < Palette::numberOfDataColors()); myCell->setColor(Palette::DataColor[seriesNumber]); return; } diff --git a/apps/regression/graph_controller.cpp b/apps/regression/graph_controller.cpp index b6b54aaf1..93b5c5867 100644 --- a/apps/regression/graph_controller.cpp +++ b/apps/regression/graph_controller.cpp @@ -407,6 +407,7 @@ void GraphController::setRoundCrossCursorView() { bool round = *m_selectedDotIndex < 0; if (round) { // Set the color although the cursor view stays round + assert(*m_selectedSeriesIndex < Palette::numberOfDataColors()); m_roundCursorView.setColor(Palette::DataColor[*m_selectedSeriesIndex]); } CursorView * nextCursorView = round ? static_cast(&m_roundCursorView) : static_cast(&m_crossCursorView); diff --git a/apps/regression/graph_view.cpp b/apps/regression/graph_view.cpp index 3b7b1371a..37ee68097 100644 --- a/apps/regression/graph_view.cpp +++ b/apps/regression/graph_view.cpp @@ -22,6 +22,7 @@ void GraphView::drawRect(KDContext * ctx, KDRect rect) const { Poincare::Context * globContext = AppsContainer::sharedAppsContainer()->globalContext(); for (int series = 0; series < Store::k_numberOfSeries; series++) { if (!m_store->seriesIsEmpty(series)) { + assert(series < Palette::numberOfDataColors()); KDColor color = Palette::DataColor[series]; Model * seriesModel = m_store->modelForSeries(series); drawCartesianCurve(ctx, rect, -INFINITY, INFINITY, [](float abscissa, void * model, void * context) { diff --git a/apps/sequence/sequence_store.cpp b/apps/sequence/sequence_store.cpp index d15543595..18012cfa6 100644 --- a/apps/sequence/sequence_store.cpp +++ b/apps/sequence/sequence_store.cpp @@ -31,6 +31,7 @@ Ion::Storage::Record::ErrorStatus SequenceStore::addEmptyModel() { const char * name = firstAvailableName(&nameIndex); assert(name); // Choose the corresponding color + assert(nameIndex < Palette::numberOfDataColors()); KDColor color = Palette::DataColor[nameIndex]; Sequence::RecordDataBuffer data(color); // m_sequences diff --git a/apps/shared/continuous_function.cpp b/apps/shared/continuous_function.cpp index 96a7cb19a..fdb4fd2cc 100644 --- a/apps/shared/continuous_function.cpp +++ b/apps/shared/continuous_function.cpp @@ -57,8 +57,7 @@ ContinuousFunction ContinuousFunction::NewModel(Ion::Storage::Record::ErrorStatu static int s_colorIndex = 0; // Create the record char nameBuffer[SymbolAbstract::k_maxNameSize]; - int numberOfColors = sizeof(Palette::DataColor)/sizeof(KDColor); - RecordDataBuffer data(Palette::DataColor[s_colorIndex++ % numberOfColors]); + RecordDataBuffer data(Palette::nextDataColor(&s_colorIndex)); if (baseName == nullptr) { DefaultName(nameBuffer, SymbolAbstract::k_maxNameSize); baseName = nameBuffer; diff --git a/apps/shared/double_pair_store.h b/apps/shared/double_pair_store.h index 9c8859612..f83e88812 100644 --- a/apps/shared/double_pair_store.h +++ b/apps/shared/double_pair_store.h @@ -55,10 +55,12 @@ public: // Colors static KDColor colorOfSeriesAtIndex(int i) { assert(i >= 0 && i < k_numberOfSeries); + assert(i < Palette::numberOfDataColors()); return Palette::DataColor[i]; } static KDColor colorLightOfSeriesAtIndex(int i) { assert(i >= 0 && i < k_numberOfSeries); + assert(i < Palette::numberOfLightDataColors()); return Palette::DataColorLight[i]; } protected: diff --git a/escher/include/escher/palette.h b/escher/include/escher/palette.h index b4c2c1a95..0ebc550a7 100644 --- a/escher/include/escher/palette.h +++ b/escher/include/escher/palette.h @@ -2,6 +2,7 @@ #define ESCHER_PALETTE_H #include +#include class Palette { public: @@ -34,6 +35,10 @@ public: constexpr static KDColor Purple = KDColor::RGB24(0x6e2d79); constexpr static KDColor DataColor[] = {Red, Blue, Green, YellowDark, Magenta, Turquoise, Pink, Orange}; constexpr static KDColor DataColorLight[] = {RedLight, BlueLight, GreenLight, YellowLight}; + + constexpr static size_t numberOfDataColors() { return sizeof(DataColor)/sizeof(KDColor); } + constexpr static size_t numberOfLightDataColors() { return sizeof(DataColorLight)/sizeof(KDColor); } + static KDColor nextDataColor(int * colorIndex); }; #endif diff --git a/escher/src/palette.cpp b/escher/src/palette.cpp index dcb617ee7..2d6e91145 100644 --- a/escher/src/palette.cpp +++ b/escher/src/palette.cpp @@ -1,4 +1,5 @@ #include +#include constexpr KDColor Palette::YellowDark; constexpr KDColor Palette::YellowLight; @@ -29,3 +30,11 @@ constexpr KDColor Palette::Brown; constexpr KDColor Palette::Purple; constexpr KDColor Palette::DataColor[]; constexpr KDColor Palette::DataColorLight[]; + +KDColor Palette::nextDataColor(int * colorIndex) { + size_t nbOfColors = numberOfDataColors(); + assert(*colorIndex < nbOfColors); + KDColor c = DataColor[*colorIndex]; + *colorIndex = (*colorIndex + 1) % nbOfColors; + return c; +} diff --git a/python/port/mod/matplotlib/modpyplot.cpp b/python/port/mod/matplotlib/modpyplot.cpp index 8077d6ffc..81972335e 100644 --- a/python/port/mod/matplotlib/modpyplot.cpp +++ b/python/port/mod/matplotlib/modpyplot.cpp @@ -50,7 +50,7 @@ mp_obj_t modpyplot_arrow(size_t n_args, const mp_obj_t *args) { assert(n_args == 4); assert(sPlotStore != nullptr); - KDColor color = Palette::DataColor[paletteIndex++]; // FIXME: Share overflow routine + KDColor color = Palette::nextDataColor(&paletteIndex); sPlotStore->addSegment(args[0], args[1], mp_obj_new_float(mp_obj_get_float(args[0])+mp_obj_get_float(args[2])), mp_obj_new_float(mp_obj_get_float(args[1])+mp_obj_get_float(args[3])), color, true); return mp_const_none; @@ -93,7 +93,7 @@ mp_obj_t modpyplot_bar(mp_obj_t x, mp_obj_t height) { size_t length = extractAndValidatePlotInput(x, height, &xItems, &hItems); mp_float_t w = 0.8; // TODO: w should be an optional parameter - KDColor color = Palette::DataColor[paletteIndex++]; // FIXME: Share overflow routine + KDColor color = Palette::nextDataColor(&paletteIndex); for (size_t i=0; iaddRect(edgeItems[i], binItems[i], mp_obj_new_float(width), binItems[i], color); @@ -203,7 +203,7 @@ mp_obj_t modpyplot_scatter(mp_obj_t x, mp_obj_t y) { mp_obj_t * xItems, * yItems; size_t length = extractAndValidatePlotInput(x, y, &xItems, &yItems); - KDColor color = Palette::DataColor[paletteIndex++]; // FIXME: Share overflow routine + KDColor color = Palette::nextDataColor(&paletteIndex); for (size_t i=0; iaddDot(xItems[i], yItems[i], color); } @@ -218,7 +218,7 @@ mp_obj_t modpyplot_plot(mp_obj_t x, mp_obj_t y) { mp_obj_t * xItems, * yItems; size_t length = extractAndValidatePlotInput(x, y, &xItems, &yItems); - KDColor color = Palette::DataColor[paletteIndex++]; // FIXME: Share overflow routine + KDColor color = Palette::nextDataColor(&paletteIndex); for (size_t i=0; iaddSegment(xItems[i], yItems[i], xItems[i+1], yItems[i+1], color, false); }