Results 1 to 2 of 2

Thread: Comparing two QColor objects, finding most similiar

  1. #1
    Join Date
    Sep 2015
    Posts
    6

    Default Comparing two QColor objects, finding most similiar

    Hi.

    I'm converting a QImage bitmap file between formats using PyQt.
    After conversion I want to get back to the starting colours, because they change a tiny bit after converting.

    Is there any accurate way to compare QColor objects and find the most similar one?

    I've tried to compare rgb values, but it's not accurate enough:

    Qt Code:
    1. def nearest_color(self, color):
    2.  
    3. col = QColor(color)
    4. r = col.red()
    5. g = col.green()
    6. b = col.blue()
    7. ranks = {}
    8. for orig_color in self.originalImageColors:
    9. orig_rgb = QColor(orig_color)
    10. diff = abs(orig_rgb.red() - r) + abs(orig_rgb.green() - g) + abs(orig_rgb.blue() - b)
    11. ranks[diff] = orig_color
    To copy to clipboard, switch view to plain text mode 


    Test data:
    Original colours: 4281742902L, 4278190335L, 4278190144L, 4294967295L
    Colours after conversion: 4281742902L, 4278190335L, 4278190144L, 4294967295L
    I need to map matching colors

  2. #2
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Comparing two QColor objects, finding most similiar

    Python has bitwise operators I would suggest turning it into a bitstream and do a bit by bit comparison.

    https://wiki.python.org/moin/BitwiseOperators


    Quote Originally Posted by morph View Post
    Hi.

    I'm converting a QImage bitmap file between formats using PyQt.
    After conversion I want to get back to the starting colours, because they change a tiny bit after converting.

    Is there any accurate way to compare QColor objects and find the most similar one?

    I've tried to compare rgb values, but it's not accurate enough:

    Qt Code:
    1. def nearest_color(self, color):
    2.  
    3. col = QColor(color)
    4. r = col.red()
    5. g = col.green()
    6. b = col.blue()
    7. ranks = {}
    8. for orig_color in self.originalImageColors:
    9. orig_rgb = QColor(orig_color)
    10. diff = abs(orig_rgb.red() - r) + abs(orig_rgb.green() - g) + abs(orig_rgb.blue() - b)
    11. ranks[diff] = orig_color
    To copy to clipboard, switch view to plain text mode 


    Test data:
    Original colours: 4281742902L, 4278190335L, 4278190144L, 4294967295L
    Colours after conversion: 4281742902L, 4278190335L, 4278190144L, 4294967295L
    I need to map matching colors

Similar Threads

  1. Help getting QxtScheduleView or somthing similiar working
    By newbie43 in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2011, 07:21
  2. Replies: 3
    Last Post: 9th January 2010, 15:47
  3. finding objects in a QTableWidgetItem
    By winkle99 in forum Qt Programming
    Replies: 2
    Last Post: 16th December 2009, 19:48
  4. Problem with comparing two QDateTime objects
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2008, 11:27
  5. Replies: 7
    Last Post: 18th July 2006, 21:33

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.