Skip to content

Commit

Permalink
A rethinking of curve colors.
Browse files Browse the repository at this point in the history
A new color pallet with more distinguishable colors.
A change of rules for incrementing colors.
  • Loading branch information
netterfield committed Nov 25, 2021
1 parent 9997b17 commit b8fd834
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/libkst/datasource.cpp
Expand Up @@ -141,7 +141,7 @@ DataSource::DataSource(ObjectStore *store, QSettings *cfg, const QString& filena
interf_vector(new NotSupportedImp<DataVector>),
interf_matrix(new NotSupportedImp<DataMatrix>),
_watcher(0),
_color(NextColor::self().next())
_color(NextColor::self().current())
{
Q_UNUSED(type)
Q_UNUSED(store)
Expand Down
6 changes: 5 additions & 1 deletion src/libkst/nextcolor.cpp
Expand Up @@ -29,7 +29,11 @@ QColor NextColor::next() {
return QColor();
}

NextColor& NextColor::self() {
QColor NextColor::current() {
return QColor();
}

NextColor& NextColor::self() {
Q_ASSERT(NextColor::_instance);
return *NextColor::_instance;
}
Expand Down
1 change: 1 addition & 0 deletions src/libkst/nextcolor.h
Expand Up @@ -26,6 +26,7 @@ class KSTCORE_EXPORT NextColor
public:
virtual ~NextColor();
virtual QColor next() = 0;
virtual QColor current() = 0;
static NextColor& self();

protected:
Expand Down
17 changes: 14 additions & 3 deletions src/libkstapp/commandlineparser.cpp
Expand Up @@ -304,12 +304,17 @@ void CommandLineParser::createCurveInPlot(VectorPtr xv, VectorPtr yv, VectorPtr
curve->setYVector(yv);
curve->setXError(0);
curve->setXMinusError(0);
curve->setColor(ColorSequence::self().next());
if (_doConsecutivePlots) {
ColorSequence::self().setIndex(0);
}
curve->setColor(ColorSequence::self().current());
if (!_doConsecutivePlots) {
ColorSequence::self().incIndex();
}
curve->setHasPoints(_usePoints);
curve->setHasLines(_useLines);
curve->setHasBars(_useBargraph);
curve->setLineWidth(dialogDefaults().value("curves/lineWidth",0).toInt());
//curve->setPointType(ptype++ % KSTPOINT_MAXTYPE);

if (ev) {
curve->setYError(ev);
Expand Down Expand Up @@ -938,7 +943,13 @@ Kst::ObjectList<Kst::Object> CommandLineParser::autoCurves(DataSourcePtr ds)
curve->setXError(0);
curve->setXMinusError(0);
curve->setYMinusError(0);
curve->setColor(Kst::ColorSequence::self().next());
if (_doConsecutivePlots) {
ColorSequence::self().setIndex(0);
}
curve->setColor(Kst::ColorSequence::self().current());
if (!_doConsecutivePlots) {
ColorSequence::self().incIndex();
}
curve->setLineWidth(1);

curve->writeLock();
Expand Down
4 changes: 2 additions & 2 deletions src/libkstapp/curvedialog.cpp
Expand Up @@ -468,7 +468,7 @@ ObjectPtr CurveDialog::createNewDataObject() {
curve->registerChange();
curve->unlock();

_curveTab->curveAppearance()->setWidgetDefaults();
_curveTab->curveAppearance()->setWidgetDefaults(true);

if(editMode()==New) {
PlotItem *plotItem = 0;
Expand Down Expand Up @@ -633,7 +633,7 @@ ObjectPtr CurveDialog::editExistingDataObject() const {
curve->registerChange();
curve->unlock();

_curveTab->curveAppearance()->setWidgetDefaults(false);
_curveTab->curveAppearance()->setWidgetDefaults();
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/libkstapp/datawizard.cpp
Expand Up @@ -1163,9 +1163,13 @@ void DataWizard::finished() {
QColor color;
int ptype = 0;
int i_plot = 0;

for (DataVectorList::Iterator it = vectors.begin(); it != vectors.end(); ++it) {
if (_pageDataPresentation->plotData()) {
color = ColorSequence::self().next();
Q_ASSERT(plotList[i_plot]);

color = plotList[i_plot]->nextColor();

colors.append(color);

DataVectorPtr vector = kst_cast<DataVector>(*it);
Expand All @@ -1191,7 +1195,6 @@ void DataWizard::finished() {
curve->registerChange();
curve->unlock();

Q_ASSERT(plotList[i_plot]);

PlotRenderItem *renderItem = plotList[i_plot]->renderItem(PlotRenderItem::Cartesian);
renderItem->addRelation(kst_cast<Relation>(curve));
Expand Down Expand Up @@ -1263,8 +1266,10 @@ void DataWizard::finished() {
curve->setPointSize(dialogDefaults().value("curves/pointSize",CURVE_DEFAULT_POINT_SIZE).toDouble());
curve->setPointType(ptype++ % KSTPOINT_MAXTYPE);

Q_ASSERT(plotList[i_plot]);

if (!_pageDataPresentation->plotDataPSD() || colors.count() <= indexColor) {
color = ColorSequence::self().next();
color = plotList[i_plot]->nextColor();
} else {
color = colors[indexColor];
indexColor++;
Expand All @@ -1275,8 +1280,6 @@ void DataWizard::finished() {
curve->registerChange();
curve->unlock();

Q_ASSERT(plotList[i_plot]);

PlotRenderItem *renderItem = plotList[i_plot]->renderItem(PlotRenderItem::Cartesian);
plotList[i_plot]->xAxis()->setAxisLog(_pagePlot->PSDLogX());
plotList[i_plot]->yAxis()->setAxisLog(_pagePlot->PSDLogY());
Expand Down
2 changes: 1 addition & 1 deletion src/libkstapp/equationdialog.cpp
Expand Up @@ -363,7 +363,7 @@ ObjectPtr EquationDialog::createNewDataObject() {
curve->registerChange();
curve->unlock();

_equationTab->curveAppearance()->setWidgetDefaults();
_equationTab->curveAppearance()->setWidgetDefaults(true);
_lastXVectorName = equation->vXIn()->Name();

if(editMode()==New) {
Expand Down
2 changes: 1 addition & 1 deletion src/libkstapp/filterfitdialog.cpp
Expand Up @@ -280,7 +280,7 @@ ObjectPtr FilterFitDialog::createNewDataObject() {
curve->registerChange();
curve->unlock();

_filterFitTab->curveAppearance()->setWidgetDefaults();
_filterFitTab->curveAppearance()->setWidgetDefaults(true);

PlotRenderItem *renderItem = plotItem->renderItem(PlotRenderItem::Cartesian);
renderItem->addRelation(kst_cast<Relation>(curve));
Expand Down
7 changes: 7 additions & 0 deletions src/libkstapp/plotitem.cpp
Expand Up @@ -11,6 +11,7 @@
***************************************************************************/

#include "plotitem.h"
#include "colorsequence.h"

#include "plotitemmanager.h"
#include "plotrenderitem.h"
Expand Down Expand Up @@ -160,6 +161,12 @@ ScriptInterface* PlotItem::createScriptInterface() {
}


QColor PlotItem::nextColor() {
ColorSequence::self().setIndex(curveList().count());
return ColorSequence::self().current();
}


void PlotItem::applyDefaults() {
QFont font;

Expand Down
2 changes: 2 additions & 0 deletions src/libkstapp/plotitem.h
Expand Up @@ -283,6 +283,8 @@ class PlotItem : public ViewItem, public PlotItemInterface

virtual ScriptInterface *createScriptInterface();

QColor nextColor();

protected:
virtual QString _automaticDescriptiveName() const;
virtual void _initializeShortName();
Expand Down
2 changes: 1 addition & 1 deletion src/libkstapp/powerspectrumdialog.cpp
Expand Up @@ -250,7 +250,7 @@ ObjectPtr PowerSpectrumDialog::createNewDataObject() {
curve->registerChange();
curve->unlock();

_powerSpectrumTab->curveAppearance()->setWidgetDefaults();
_powerSpectrumTab->curveAppearance()->setWidgetDefaults(true);

if(editMode()==New) {
PlotItem *plotItem = 0;
Expand Down
93 changes: 9 additions & 84 deletions src/libkstmath/colorsequence.cpp
Expand Up @@ -12,17 +12,15 @@

// delivers new colors...
//
// Consideratoins
// Don't increment if not used
// Enforce contrast
// increment if asked?

// Mark a color used: MarkUsed();
// Inrement Index: IncIndex()
// Set index: SetIndex(i)
// Index: Index()
// Current primary Color: Current(Background)
// Current Bar color: CurrentBar(Background)
// Current Head Color: CurrentHead(Background)
// Current Bar color: CurrentBar(Background)
// Current Head Color: CurrentHead(Background)

//
// Only increment if there are multiple curves in a plot.

// application specific includes
#include "colorsequence.h"
Expand Down Expand Up @@ -58,7 +56,6 @@ ColorSequence::ColorSequence() {
_colors.append(QColor::fromRgb( 0, 166, 255));
_colors.append(QColor::fromRgb( 95, 154, 0));
_colors.append(QColor::fromRgb(178, 24, 255));
_colors.append(QColor::fromRgb(209, 0, 0));
_colors.append(QColor::fromRgb(255, 200, 0));
_colors.append(QColor::fromRgb(172, 172, 172));
_colors.append(QColor::fromRgb( 89, 220, 205));
Expand All @@ -74,68 +71,13 @@ ColorSequence::ColorSequence() {

}

//ColorSequence::ColorSequence() {
// int n_hues = 7;
// double hues[7];

// for (int i = 0; i < n_hues-1; i++) {
// double step = 1.0/(double)(n_hues-1);
// double h = (double)i/(double)n_hues;
// if (i==2) {
// h -= 0.8*step;
// }
// if (i==3) {
// h -= 0.3*step;
// }
// if (i==5) {
// h += 0.3*step;
// }
// hues[i] = h;
// }
// hues[n_hues-1] = 0.0;


// // Saturated/Bright
// for (int i = 0; i < n_hues-1; i++) {
// if (i!=1) {
// _colors.append(QColor::fromHsvF(hues[i], 1.0, 1.0));
// }
// }
// _colors.append(QColor::fromRgb(255,255,255)); // White

// // Saturated/darker
// for (int i = 0; i < n_hues-1; i++) {
// if (i!=1) {
// _colors.append(QColor::fromHsvF(hues[i], 1.0, 0.6));
// }
// }
// _colors.append(QColor::fromRgb(0,0,0)); // Black

// // Less Saturated/bright
// for (int i = 0; i < n_hues-1; i++) {
// if (i!=1) {
// _colors.append(QColor::fromHsvF(hues[i], 0.3, 1.0));
// }
// }
// _colors.append(QColor::fromRgb(190,190,190)); // Grey

// _count = _colors.size();
// _ptr = 0;
//}


ColorSequence::~ColorSequence() {
}



QColor ColorSequence::next() {

QColor current_color = entry(_ptr);

_ptr++;
_ptr %= _count;
return current_color;
incIndex();
return entry(_ptr);
}


Expand All @@ -144,6 +86,7 @@ QColor ColorSequence::current() {
}



QColor ColorSequence::entry(int i) {
// makes sure 0<=i<count.
i = abs(i)%_count;
Expand All @@ -152,24 +95,6 @@ QColor ColorSequence::entry(int i) {
}


//void ColorSequence::createPalette( ) {
// if (_palette != KstColorSequenceName) {
// _pal.clear();
// _palette = KstColorSequenceName;

// for (int i = 0; i < colorcnt; i++) {
// _pal.insert(i, QColor(colors[i]));
// }

// _count = _pal.count();
// _ptr = 0;
// }
//}





//QColor ColorSequence::next_c_bc(const CurveList& curves, const QColor& badColor) {
// QColor color;
// int dark_factor;
Expand Down
21 changes: 18 additions & 3 deletions src/libkstmath/colorsequence.h
Expand Up @@ -19,17 +19,32 @@
#include "curve.h"


class KPalette;
// Inrement Index: IncIndex()
// Set index: SetIndex(i)
// Index: Index()
// Current primary Color: Current(Background)
// Current Bar color: CurrentBar(Background)
// Current Head Color: CurrentHead(Background)

//class KPalette;

namespace Kst {

class KSTMATH_EXPORT ColorSequence : public NextColor
{
public:
QColor next();
QColor current();
QColor next(); // increment and return next color
QColor current(); // current entry

QColor entry(int ptr);

void incIndex() { _ptr++; _ptr %= _count;};
void setIndex(int ptr) {_ptr = abs(ptr)%_count;};

int index() {return _ptr;};

int count() {return _count;};

static ColorSequence& self();

private:
Expand Down
1 change: 1 addition & 0 deletions src/widgets/colorbutton.cpp
Expand Up @@ -80,6 +80,7 @@ void ColorButton::chooseColor() {
if (_color == Qt::transparent) {
_color = Qt::black;
}

QColor color = QColorDialog::getColor(_color, parentWidget(), "Choose Color", QColorDialog::ShowAlphaChannel);

if (color.isValid()) {
Expand Down

0 comments on commit b8fd834

Please sign in to comment.