python-sfml icon indicating copy to clipboard operation
python-sfml copied to clipboard

Added __int__() function to Color in graphics.pyx. Fixes #155: "Color…

Open Necklaces opened this issue 7 years ago • 0 comments

def __int__(self): added to the Color class in graphics.pyx, just like in sfml; https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Color.php#a48f75a30fc93e79390e6d700e4b4b558 https://github.com/SFML/SFML/blob/fae3b65f0567f87fa9925cd42d28df15eb69e79c/src/SFML/Graphics/Color.cpp

What SFML does:

  1. shifts red (which is a uint8) 24 bits to the left
  2. shifts green (which is a uint8) 16 bits to the left
  3. shifts blue (which is a uint8) 8 bits to the left
  4. ORs (|) red, green, blue, and alpha together, forming a uint32 representation of the rgba color.

What this does: Same, but since this is python it ANDs (&) self.r/g/b/a with 0xFF (8 bits) to make sure the shifting doesn't end up bigger or smaller than a 32bit uint.

SFML example: (1 << 24) | (2 << 16) | (3 << 8) | 4;
returns: 16909060

python-sfml: sf.Color(1,2,3,4).__int__()
returns: 16909060 

Necklaces avatar Jun 10 '17 00:06 Necklaces