How do I invert a pygame color? For example how would I invert:
key_color = pygame.Color('grey')
How do I invert a pygame color? For example how would I invert:
key_color = pygame.Color('grey')
pygame.Color object:
The Color class represents RGBA color values using a value range of 0 to 255 inclusive. It allows basic arithmetic operations — binary operations +, -, *, //, %, and unary operation ~ [...]
Hence you can use several operators to compute colors. The red, green and blue color channels can be accessed with the attributes r, g and b:
key_color = pygame.Color('grey')
inverted_color = pygame.Color(255-key_color.r, 255-key_color.g, 255-key_color.b)
recpectively
inverted_color = pygame.Color(255, 255, 255) - key_color
Alternatively, the bitwise inversion (~) of a color can be used:
inverted_color = ~key_color
The bitwise inverse is implemented by (operator.__invert__(obj)). See Standard operators as functions
One can invert a pygame color with:
key_color = pygame.Color('grey')
# one can use bit inversion (the ~ operator) on a color
inverted_color = ~key_color
# which is calculated by calling
inverted_color = key_color.__invert__()