PDA

View Full Version : Comparing two QColor objects, finding most similiar



morph
28th October 2015, 01:00
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:


def nearest_color(self, color):

col = QColor(color)
r = col.red()
g = col.green()
b = col.blue()
ranks = {}
for orig_color in self.originalImageColors:
orig_rgb = QColor(orig_color)
diff = abs(orig_rgb.red() - r) + abs(orig_rgb.green() - g) + abs(orig_rgb.blue() - b)
ranks[diff] = orig_color


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

Caolan O'Domhnaill
28th October 2015, 17:28
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



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:


def nearest_color(self, color):

col = QColor(color)
r = col.red()
g = col.green()
b = col.blue()
ranks = {}
for orig_color in self.originalImageColors:
orig_rgb = QColor(orig_color)
diff = abs(orig_rgb.red() - r) + abs(orig_rgb.green() - g) + abs(orig_rgb.blue() - b)
ranks[diff] = orig_color


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