Results 1 to 4 of 4

Thread: can Qt darker / lighter a image ?

  1. #1
    Join Date
    Feb 2008
    Posts
    49
    Thanks
    2
    Thanked 4 Times in 1 Post

    Default can Qt darker / lighter a image ?

    I want to implment a widget to indicate the light status, and I alread have a light image.

    Does Qt has ability to darker/lighter a image ? how to do ?

    and the image has some transparent area, and alpha channel, is it possible that the result image still have transparent and alpha ?
    sorry, i am not familiar with this ...

    Tks ...

    /WX

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: can Qt darker / lighter a image ?

    From the QImage you can get pixel ( QImage::pixel ) . You can then set the pixel to desired color.QColor::setRgb and use QColor::darker or QColor::lighter .

    Other wise you can also have look at QGraphicsEffect and derived classes.

  3. #3
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: can Qt darker / lighter a image ?

    hi

    try this code

    Qt Code:
    1. QImage *tmpImage = new QImage(SOME_IMAGE_PATH);
    2. QPoint p1, p2;
    3. p2.setY(tmpImage->height());
    4.  
    5. QLinearGradient gradient(p1, p2);
    6. gradient.setColorAt(0, Qt::transparent);
    7. gradient.setColorAt(1, QColor(0, 0, 0, 255));
    8.  
    9. QPainter p(tmpImage);
    10. p.fillRect(0, 0, tmpImage->width(), tmpImage->height(), gradient);
    11.  
    12. gradient.setColorAt(0,QColor(0, 0, 0, 255));
    13. gradient.setColorAt(1, Qt::transparent);
    14. p.fillRect(0,0, tmpImage->width(), tmpImage->height(), gradient);
    15.  
    16. p.end();
    To copy to clipboard, switch view to plain text mode 

    You can use this code to darker or lighter the image, depending upon ur requirement change gradient color range.

    BTW this is gradient color i have used on the image. you can use plain color as well

  4. #4
    Join Date
    Feb 2008
    Posts
    49
    Thanks
    2
    Thanked 4 Times in 1 Post

    Default Re: can Qt darker / lighter a image ?

    Hello, thanks , follow your suggestion, I have below code. and better idea ?

    place here for someone may also looking for ...

    Qt Code:
    1. void do_light_adjust(QImage *image, int factor)
    2. {
    3. if (image == NULL || image->isNull() || factor == 0)
    4. return;
    5.  
    6. int bytes_per_pixel = image->bytesPerLine() / image->width();
    7. uchar *pixel = NULL;
    8. QRgb *rgba;
    9.  
    10. if (factor > 0) { // lighter
    11. factor += 100;
    12. for (int h = 0; h < image->height(); h++) {
    13. pixel = image->scanLine(h);
    14. for (int w = 0; w < image->width(); w++) {
    15. rgba = (QRgb *)pixel;
    16. if (qAlpha(*rgba) != 0 && (qRed(*rgba) != 0 || qGreen(*rgba) != 0 || qBlue(*rgba) != 0))
    17. *rgba = QColor::fromRgba(*rgba).lighter(factor).rgba();
    18. pixel += bytes_per_pixel;
    19. }
    20. }
    21. } else { // darker
    22. factor = -factor;
    23. factor += 100;
    24. for (int h = 0; h < image->height(); h++) {
    25. uchar *pixel = image->scanLine(h);
    26. for (int w = 0; w < image->width(); w++) {
    27. rgba = (QRgb *)pixel;
    28. if (qAlpha(*rgba) != 0 && (qRed(*rgba) != 0 || qGreen(*rgba) != 0 || qBlue(*rgba) != 0))
    29. *rgba = QColor::fromRgba(*rgba).darker(factor).rgba();
    30. pixel += bytes_per_pixel;
    31. }
    32. }
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 6
    Last Post: 21st September 2009, 10:55
  2. hide image,once moused moved from image
    By yuvaraj.yadav in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2009, 08:16
  3. How can we compare if one big image contains a small image?
    By learning_qt in forum Qt Programming
    Replies: 1
    Last Post: 13th March 2009, 09:20
  4. Replies: 3
    Last Post: 10th January 2009, 12:01
  5. Replies: 3
    Last Post: 14th March 2007, 08:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.