def test_map_rgb(self): pformat = pixels.alloc_format(pixels.SDL_PIXELFORMAT_RGBA8888) self.assertIsInstance(pformat, pixels.SDL_PixelFormat) val = pixels.map_rgb(pformat, 0xFF, 0xAA, 0x88) self.assertEqual(val, 0xFFAA88FF) pixels.free_format(pformat) pformat = pixels.alloc_format(pixels.SDL_PIXELFORMAT_UNKNOWN) self.assertIsInstance(pformat, pixels.SDL_PixelFormat) self.assertEqual(pformat.format, pixels.SDL_PIXELFORMAT_UNKNOWN) val = pixels.map_rgb(pformat, 0xFF, 0xAA, 0x88) self.assertEqual(val, 0x0) pixels.free_format(pformat)
def test_get_set_color_key(self): colorkeys = ((0, 0, 0), (32, 64, 128), (10, 20, 30), (1, 2, 4), (255, 255, 255), (128, 128, 128), (127, 127, 127), ) for fmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt): continue pformat = pixels.alloc_format(fmt) bpp, rmask, gmask, bmask, amask = \ pixels.pixelformat_enum_to_masks(fmt) sf = surface.create_rgb_surface(10, 10, bpp, rmask, gmask, bmask, amask) for r, g, b in colorkeys: key = pixels.map_rgb(pformat, r, g, b) surface.set_color_key(sf, 1, key) skey = surface.get_color_key(sf) self.assertEqual(skey, key, "Could not set color key (%d, %d, %d)" % (r, g, b)) pixels.free_format(pformat) surface.free_surface(sf)
def from_color(self, color, size=(0, 0), bpp=32, masks=None): """Creates a sprite with a certain color. """ color = convert_to_color(color) if masks: rmask, gmask, bmask, amask = masks else: rmask = gmask = bmask = amask = 0 sf = sdlsurface.create_rgb_surface(size[0], size[1], bpp, rmask, gmask, bmask, amask) fmt = sf.format if fmt.Amask != 0: # Target has an alpha mask c = pixels.map_rgba(fmt, color.r, color.g, color.b, color.a) else: c = pixels.map_rgb(fmt, color.r, color.g, color.b) sdlsurface.fill_rect(sf, None, c) return self.from_surface(sf, True)
def prepare_color(color, target): """Prepares the passed color for the passed target. """ color = convert_to_color(color) pformat = None # Software surfaces if isinstance(target, sdlpixels.SDL_PixelFormat): pformat = target elif isinstance(target, sdlsurface.SDL_Surface): pformat = target.format elif isinstance(target, SoftwareSprite): pformat = target.surface.format if pformat is None: raise TypeError("unsupported target type") if pformat.Amask != 0: # Target has an alpha mask return sdlpixels.map_rgba(pformat, color.r, color.g, color.b, color.a) return sdlpixels.map_rgb(pformat, color.r, color.g, color.b)