[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [pygame] Life with PyGame
Nicola Larosa wrote:
> I still don't know exactly what values I should use under 8, 16, 24 and
> 32 bit depth to get a white color.
there's a couple ways to deal with color on the surfarray arrays. the
first and potentially easiest is to use the "3d" arrays. when then
allows you to assign colors as RGB triplets. for examples..
my_3d_array[:] = 100, 150, 200
for getting correct colors while using 2d arrays you have two options.
the Surface.map_rgb() function maps RGB values into the mapped integer
value. therefore you could perform the same code like this...
my_2d_array[:] = my_surface.map_rgb(100, 150, 200)
also note there is a "unmap_rgb" method which converts the other way.
lastly, you can do the color mapping yourself. this is a little more
advanced, but not tricky if you know your bit-operations. this is where
the Surface.get_masks(), get_shifts(), and get_losses() come in.
briefly, the masks represent which bits of the integer color make up a
color plane. the shits represents how many bits over that mask is, and
the losses represents how many bits less than 8 are used for the color.
once you grasp that you can fairly easily do the color mapping yourself.
in your case, all white, we simply say
white_int = masks[0] | masks[1] | masks[2]
to map real colors like above we don't need too much extra work
def my_map_rgb(surf, r, g, b):
masks = surf.get_masks()
shifts = surf.get_shifts()
losses = surf.get_losses()
mapr = r >> losses[0] << shifts[0]
mapg = g >> losses[1] << shifts[1]
mapb = b >> losses[2] << shifts[2]
return mapr | mapg | mapb
def my_unmap_rgb(surf, intcolor):
masks = surf.get_masks()
shifts = surf.get_shifts()
losses = surf.get_losses()
r = (intcolor & mask[0]) >> shifts[0] << losses[0]
g = (intcolor & mask[1]) >> shifts[1] << losses[1]
b = (intcolor & mask[2]) >> shifts[2] << losses[2]
return r, g, b
of course, one benefit of the Surface.map_rgb, it will also work with
8bit surfaces and their colormaps. but now armed with this information,
i assume you can get your proper colors without the 2**bits magic :]
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org