Drawing a QLinearGradient in a QImage



  • Hi,

    I am trying to render a gradient into an image to save it in a file. I am doing this:

    @
    const int histogramSize = 256;
    QLinearGradient gradient(0, 0, histogramSize, 0);
    gradient.setStops(stops);

        QImage lut = QImage(QSize(1, histogramSize), QImage::Format_ARGB32);
        QPainter p(&lut);
        p.setCompositionMode(QPainter::CompositionMode_Source);
        p.fillRect(0, 0, histogramSize, 1, gradient);
    
        const uchar * buffer = lut.bits();
        const int nbOfBytes = histogramSize * 4 ;
        assert(nbOfBytes == lut.byteCount());
        const uchar * endBuffer = buffer + nbOfBytes;
        QColor begin(lut.pixel(QPoint(0, 0)));
        QColor end(lut.pixel(QPoint(0, histogramSize - 1)));
        for(int i = 0 ; i < 2 ; ++i)
        {
            QColor color = stops[i].second;
            std::cout << i << ": " << color.red() << ' ' << color.green() << ' ' << color.blue() << ' ' << color.alpha() << '\n';
        }
        std::cout << "begin: " << begin.red() << ' ' << begin.green() << ' ' << begin.blue() << ' ' << begin.alpha() << '\n';
        std::cout << "end: " << end.red() << ' ' << end.green() << ' ' << end.blue() << ' ' << end.alpha() << '\n';
    

    @

    And the gradient is defined by two points like you can see in the screenshot

    http://s2.postimage.org/7uipuh5it/Screen_Hunter_01_May_13_16_15.jpg

    The result of the program is:

    bq. 0: 252 252 252 2
    1: 252 252 252 252
    begin: 255 255 255 255
    end: 205 205 205 255

    However as you can see begin doesn't have the alpha value I am expecting of them. I tried to change the RGB components and they change properly like I expect them to be but the alpha component never change. It is almost like if the gradient didn't care on changing the alpha component even if I set the composition mode to source which should basically just dump the gradient into the image. Is it a bug or is it the normal behavior and if it is the normal behavior could someone explain me why?

    Thanks



  • Could you update reference to screenshot ? When I try to see, I receive an error "Image not available"



  • Could you update reference to screenshot ? When I try to see, I receive an error "Image not available"



  • The original post was over three years old, by a user who only made this one post. Please don't try to revive the dead...



  • The original post was over three years old, by a user who only made this one post. Please don't try to revive the dead...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.