Results 1 to 7 of 7

Thread: How to change QPixmap color from black to red

  1. #1

    Default How to change QPixmap color from black to red

    I have png-file in black/white color.

    QPixmap bearingPixmap;
    bearingPixmap.load(":/Hinge.png");

    How to change QPixmap black color to red?
    In which way can I use createMaskFromColor and setMask?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to change QPixmap color from black to red

    If the image is index coloured then the easiest option is to change the colour map.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: How to change QPixmap color from black to red

    I don't know what is index coloured image.
    I'd like set this pixmap
    Hinge.png
    to my QGraphicsPixmapItem.
    Then I want to change black color to red after picking the item.
    I don't like to create one more red png-file.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to change QPixmap color from black to red

    Quote Originally Posted by oogolov View Post
    I don't know what is index coloured image.
    It's an image that has at most 8 bits per colour (meaning a maximum of 256 colours) as opposed to 24 or 32bit.

    I'd like set this pixmap
    Hinge.png
    to my QGraphicsPixmapItem.
    It's not an indexed image but you can convert it to one using gimp, imagemagick or any other graphics tool available.

    Then you can use QImage::setColorTable() to set a new colour map for the image.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to change QPixmap color from black to red

    try
    Qt Code:
    1. QPixmap px( "C:\\Hinge.png" );
    2. QPixmap pxr( px.size() );
    3. pxr.fill( Qt::red );
    4. pxr.setMask( px.createMaskFromColor( Qt::transparent ) );
    To copy to clipboard, switch view to plain text mode 
    You will get: test.png

    Maybe not the most elengant way (and definitely not the fastest) but does the job.

  6. #6
    Join Date
    Aug 2017
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: How to change QPixmap color from black to red

    @Spitfire's code works good for flat images.
    If your image has varying alpha values you can use:
    Qt Code:
    1. QImage tmp = pixmap.toImage();
    2.  
    3. for(int y = 0; y < tmp.height(); y++)
    4. for(int x= 0; x < tmp.width(); x++)
    5. color.setAlpha(tmp.pixelColor(x,y).alpha());
    6. tmp.setPixelColor(x,y,color);
    7.  
    8. pixmap = QPixmap::fromImage(tmp);
    To copy to clipboard, switch view to plain text mode 
    Commented:

    Qt Code:
    1. // "pixmap" is your QPixmap
    2. // "color" is your QColor
    3.  
    4. // Convert the pixmap to QImage
    5. QImage tmp = pixmap.toImage();
    6.  
    7. // Loop all the pixels
    8. for(int y = 0; y < tmp.height(); y++)
    9. {
    10. for(int x= 0; x < tmp.width(); x++)
    11. {
    12. // Read the alpha value each pixel, keeping the RGB values of your color
    13. color.setAlpha(tmp.pixelColor(x,y).alpha());
    14.  
    15. // Apply the pixel color
    16. tmp.setPixelColor(x,y,color);
    17. }
    18. }
    19.  
    20. // Get the coloured pixmap
    21. pixmap = QPixmap::fromImage(tmp);
    To copy to clipboard, switch view to plain text mode 

  7. #7

    Default Re: How to change QPixmap color from black to red


Similar Threads

  1. Replies: 2
    Last Post: 16th February 2012, 23:10
  2. Replies: 0
    Last Post: 25th August 2010, 17:39
  3. Replies: 3
    Last Post: 22nd January 2010, 16:46
  4. Change QPixmap image at runtime
    By Qt Coder in forum Qt Programming
    Replies: 12
    Last Post: 30th March 2009, 12:37
  5. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 22:25

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.