-3

I want to paint that:

hsv

I already wrote that code. I used that site to write hsvtorgb method: http://www.rapidtables.com/convert/color/hsv-to-rgb.htm Unfortunatelly my code doesn't show what i want. Can you tell me why? Thanks a lot! I am waiting for answers

void Widget::hsvtorgb(int h, float s, float v)
{
float C, X, m;
float r, g, b;
C = v * s;
X = C*(1 - abs(((h/60) % 2) - 1));
m = v - C;
if(h>=0 && h<60){r = C; g = X; b = 0;}
if(h>=60 && h<120){r = X; g = C; b = 0;}
if(h>=120 && h<180){r = 0; g = C; b = X;}
if(h>=180 && h<240){r = 0; g = X; b = C;}
if(h>=240 && h<300){r = X; g = 0; b = C;}
if(h>=300 && h<360){r = C; g = 0; b = X;}

R = (r + m) * 255;
G = (g + m) * 255;
B = (b + m) * 255;
}

void Widget::on_H_slider_valueChanged(int value)
{
h=value;
for(int i=0; i<600; i++)
{
    for(int j=0; j<600; j++)
    {
        hsvtorgb(1.0*j/600, 1.0*i/600, v);
        bits[600*4*i+4*j] = B;
        bits[600*4*i+4*j+1] = G;
        bits[600*4*i+4*j+2] = R;
    }
}
update();
}

void Widget::on_S_slider_valueChanged(int value)
{
s=value/100;
for(int i=0; i<600; i++)
{
    for(int j=0; j<600; j++)
    {
        hsvtorgb(h, s, v);
        bits[600*4*i+4*j] = B;
        bits[600*4*i+4*j+1] = G;
        bits[600*4*i+4*j+2] = R;
    }
}
update();
}

void Widget::on_V_slider_valueChanged(int value)
{
v=value/100;
for(int i=0; i<600; i++)
{
    for(int j=0; j<600; j++)
    {
        hsvtorgb(h, s, v);
        bits[600*4*i+4*j] = B;
        bits[600*4*i+4*j+1] = G;
        bits[600*4*i+4*j+2] = R;
    }
}
update();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
JKSX
  • 1
  • 2
  • 3
    You're waiting for answers, we're waiting for a question. – Mark Ransom May 09 '17 at 18:30
  • 1
    _Can you help me with solution?_ Is way to broad for SO. SO is meant for _specific_ programming questions. – Algirdas Preidžius May 09 '17 at 18:31
  • My question is what i am doing wrong? – JKSX May 09 '17 at 18:32
  • 1
    @JKSX Still off-topic, since _my code doesn't show what i want_ doesn't mean anything to us - we can't read your mind. Please read [ask], and have a look through [help]. In short: You need to describe what output you are getting, what output you expect to get, and explain why your expectations differ from reality. In addition: did you step through your code with a debugger, to see where it starts doing something you don't expect? SO is not debug-it-for-me kind of service either. – Algirdas Preidžius May 09 '17 at 18:36
  • I don't know where I should find information about that. Can you help me with source? – JKSX May 09 '17 at 18:41

1 Answers1

1

First, try to use already implemented HSV to RGB color conversion from HSV to RGB in QColor class - QColor::fromHsvF()

Second, you have mistakes in on_V_slider_valueChanged and in on_S_slider_valueChanged functions. As I understand, you tried to show 2d spectral image in HS and HV color space, but it shows only one color patch.

Here is a little sample how to show 2d spectral image with fixed S value:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    pixmap(600,200)
{
    ui->setupUi(this);

    recalc();
    ui->img->setPixmap(pixmap);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::recalc()
{
    double S = 1.0;
    QPainter p(&pixmap);

    for (int i = 0; i < 200; i++)
        for (int j = 0; j < 600; j++)
        {
            p.setPen(QColor::fromHsvF(j/599.0, S, i/199.0));
            p.drawPoint(QPoint(j, 199-i));
        }
}

and header file:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    QPixmap pixmap;

    void recalc();
};
ramzes2
  • 773
  • 5
  • 13