QCustomPlot Discussion and Comments

QCPColorGradient 2 colorsReturn to overview

Hello!
–°an I make 2 colors in QCPColorGradient and so that they are separated, for example, 30% by 70%?

i do gradient->setLevelCount(2) and QMap<double,QColor> map = {{0.0, Qt::green}, {1, Qt::red}};
but only this always puts 50% to 50%

What do those percentages mean that you write? Position on the scale? Then use 0.3 and 0.7 as color coordinate.

I want the scale to show, for example, 70% green and 30% red
if i write like thisQMap<double,QColor> map = {{0.7, Qt::green}, {1, Qt::red}}; it won't work
i want 0-0.7 - green 0.7 - 1.0 - red

i can achieve this with

gradient->setLevelCount(350);
QMap<double,QColor> map = {{0.0, Qt::green}, {percentage(59.), Qt::green},{percentage(59.) + 0.0000001, Qt::red},{1., Qt::red}};

but i think there is another way

sorry, bad example

gradient->setLevelCount(350);
QMap<double,QColor> map = {{0.0, Qt::green}, {percentage(0.7), Qt::green},{0.7 + 0.0000001, Qt::red},{1., Qt::red}};

0.0, green
0.7, green
1.0, red

Oh, maybe you just want to use the QColor constructor which lets you choose the individual red, green, and blue components from 0..255 range: https://doc.qt.io/qt-5/qcolor.html

so QMap<double,QColor> map = {{0, QColor(77, 179, 0)}, {1, QQColor(179, 77, 0)}};

lancelot

0.0, green
0.7, green
1.0, red

this doesn't work

i want hard border colors

when i write like this

gradient->setLevelCount(350);
QMap<double,QColor> map = {{0.0, Qt::green}, {percentage(0.7), Qt::green},{0.7 + 0.0000001, Qt::red},{1., Qt::red}};

i get this - https://ibb.co/xC3sRbp
when i write like this
gradient->setLevelCount(350);
QMap<double,QColor> map = {{0.0, Qt::green}, {0,7, Qt::yellow}, {1, Qt::red}};

i get this - https://ibb.co/KLLnKfp
but i want first option

I don't want gradient. I just want 3 colors without gradient.

So what's wrong with

gradient->setLevelCount(350);
QMap<double,QColor> map = {{0.0, Qt::green}, {percentage(0.7), Qt::green},{0.7 + 0.0000001, Qt::red},{1., Qt::red}};

If you don't want the trick with +0.00001, did you try to experiment with setLevelCount(3)? So then you define according 4 numbers |---|---|---| at the wanted "|" points

Or three numbers? Ah, you'll find it out by experimenting :)

i experimented with setLevelCount(3), but I don't understand how it works
i want
0 < val < 0.25 green
0.25 < val < 0.7 yellow
0.7 < val < 1.0 red

    gradient->setColorStopAt(0.0, Qt::green);
    gradient->setColorStopAt(0.25, Qt::green);
    gradient->setColorStopAt(0.7, Qt::yellow);
    gradient->setColorStopAt(1.0, Qt::red);
    gradient->setLevelCount(3);

look at the picture - https://ibb.co/mXtZBFw
Where yellow?
thank you for understanding and help =)

https://ibb.co/mXtZBFw

When I wrote this, I noticed strange behavior.

gradient->setLevelCount(350);
QMap<double,QColor> map = {{0.0, Qt::green}, {percentage(0.7), Qt::green},{0.7 + 0.0000001, Qt::red},{1., Qt::red}};

On the map, cells were displayed in red, which should be yellow.

In your previous post yellow isn't visible because with 0.7 you placed it almost on the corner of the switch between green and red. I believe the color of a section is taken from its center. I suggest checking in QCustomPlot code how exactly colors are picked/interpolated.

your second post: why should there be yellow, you don't have yellow in your color stops at all only green, green, red, red. And the transition between green and red is only in a space 0.0000001 wide, as you explicitly made it that way (and 0.0000001 is much smaller than 1/350, so it's not resolved).

Sorry, second example was bad, look at this

    QMap<double,QColor> colorStops = {
        {0.0, Qt::green},
        {0.25, Qt::green},
        {0.25 + 0.0000001, Qt::yellow},
        {0.7 - 0.0000001, Qt::yellow},
        {0.7, Qt::red},
        {1., Qt::red}};

0 - 0.25 -> green
0.25 - 0.7 -> yellow
0.7 - 1.0 -> red

I just want to move the intervals(yellow and red) in the future

thank you for help