Ahhh! Sorry I misunderstood what your values of 0 to 10 were. I thought they were data points 0 through 10. But you mean that these are X values or Y values 0 through 10.
In this case you want to define a function that based on the input value a colour is returned. I do this for contour plotting, but I use a linear colour map. In your case you want a discrete colour map based on input value. So in my code when I want to draw a line I do this:
colourMap = QCPLinearColorMap; // My colour stops are defined as values 0.0 through 1.0.
colourMap.addColorStop( 0.0, Qt::red);
colourMap.addColorStop( 0.1, Qt::blue);
colourMap.addColorStop( 0.3, Qt::green);
...
...
QPen pen;
pen.setColor( colourMap.color( 0.0, 100.0, xValue)); // Set colour range 0 to 100.0.
// Draw somthing.
The code I use for the linear colour map is below. You just need to modify it to do discrete colour changes rather than linear (basically delete some of my code).
class QCPLinearColorMap
{
public:
QCPLinearColorMap() {}
~QCPLinearColorMap() {}
void addColorStop( const double &value, const QColor &color);
QColor color( const double &vMin, const double &vMax, const double value);
private:
struct ColorStop {
ColorStop( const double &v, const QColor &c) : value( v), color( c) {}
double value;
QColor color;
};
QList< ColorStop > colorStops_;
};
void QCPLinearColorMap::addColorStop( const double &value, const QColor &color)
{
colorStops_ << ColorStop( value, color);
}
QColor QCPLinearColorMap::color( const double &vMin, const double &vMax, const double value)
{
if ( colorStops_.isEmpty()) {
return QColor();
}
if ( colorStops_.count() == 1) {
return colorStops_[0].color;
}
// Normalise value between 0 < x < 1.
double x = ( value - vMin) / ( vMax - vMin);
if ( x <= colorStops_.first().value) {
return colorStops_.first().color;
}
if ( x >= colorStops_.last().value) {
return colorStops_.last().color;
}
// Using linear search for closest colour stops rather than mid-point search.
// It is unlikely there would be a large number of colour stops.
int i = 0;
while ( i < colorStops_.count() - 2 && x > colorStops_[i+1].value) {
++i;
}
// Interpolate between closest colour stops.
ColorStop &c1 = colorStops_[i];
ColorStop &c2 = colorStops_[i+1];
double dx = c2.value - c1.value;
double r1 = ( c2.value - x) / dx;
double r2 = ( x - c1.value) / dx;
int r = r1 * c1.color.red() + r2 * c2.color.red();
int g = r1 * c1.color.green() + r2 * c2.color.green();
int b = r1 * c1.color.blue() + r2 * c2.color.blue();
return QColor( r, g, b);
}
I hope this is not too complicated. Have Fun !