colour icon indicating copy to clipboard operation
colour copied to clipboard

[FEATURE]: Add `__eq__` magic method to `RGB_Colourspace` class

Open MrLixm opened this issue 2 years ago • 5 comments

Description

Hello, I was wondering if there is any good reason why RGB_Colourspace class doesn't implement an __eq__ method to allow instance comparison like :

colorspace_a = colour.RGB_COLOURSPACES["sRGB"]
colorspace_b = colour.RGB_Colourspace("sRGB",  [[ 0.64,  0.33],[ 0.3 ,  0.6 ], [ 0.15,  0.06]], [ 0.3127,  0.329 ]],...)
assert colorspace_a == colorspace_b 

Cheers. Liam.

MrLixm avatar May 28 '23 18:05 MrLixm

No good reason, just did not have the need! That would be an exact equality too so 0.6400000001, 0.33 would not work.

KelSolaar avatar May 29 '23 09:05 KelSolaar

It would also raise the question of whether you would want a version of a colourspace which used a derived NPM to be considered equal to one with a "per spec" one.

nick-shaw avatar May 29 '23 09:05 nick-shaw

It would also raise the question of whether you would want a version of a colourspace which used a derived NPM to be considered equal to one with a "per spec" one.

In that case I would say no. As thomas mentioned for me it would be a straight equal comparison of the class attributes :


import numpy

class RGB_Colourspace:
    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return (
                self.name == other.name and
                numpy.array_equal(self.primaries, other.primaries) and
                numpy.array_equal(self.whitepoint, other.whitepoint) and
                self.whitepoint_name == other.whitepoint_name and
                numpy.array_equal(self.matrix_RGB_to_XYZ, other.matrix_RGB_to_XYZ) and
                numpy.array_equal(self.matrix_XYZ_to_RGB, other.matrix_XYZ_to_RGB) and
                self.cctf_encoding == other.cctf_encoding and
                self.cctf_decoding == other.cctf_decoding and
                self.use_derived_matrix_RGB_to_XYZ == other.use_derived_matrix_RGB_to_XYZ and
                self.use_derived_matrix_XYZ_to_RGB == other.use_derived_matrix_XYZ_to_RGB
            )
        return False

Without this I think we would also have the issue with cctf function where it would be impossible to know if they produce the same result. So we just check if they are the same object.

This make the use of eq pretty limited as just a way to check for example if 2 instances are actually the same, like one you would get with RGB_Colourspace().copy(), but still useful for that kind of case.

MrLixm avatar May 29 '23 10:05 MrLixm

This would be free if RGB_Colourspace (and similar) were reimplemented using attrs or dataclass or similar. Would also reduce a lot of repeated and boilerplate code.

jamesmyatt avatar Jun 28 '23 22:06 jamesmyatt

That would not be a bad idea but we have quite a bit of logic on the properties, not easy.

KelSolaar avatar Jul 15 '23 06:07 KelSolaar