[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] some surfarray questions...



Hi;

My ultimate goal here is to make a color selector box, kind of like you
might see in the GIMP. I have one that works, but it is pretty slow
when you change which hue you want to use. (it basically goes through
a surface and does set_at everywhere)

Here is what I have:

class ColorSelector(Dialog_OK):
# a bunch of other methods snipped...

def set_color_square(self):
"""Paint a square with possible colors.

This uses the ColorSelector C{hue} property for the hue of the
color, then ranges over all possible saturations and values to make
a square.

This is way too slow.

"""

image = self.color_square.image

h = self.hue
r, g, b = colorsys.hsv_to_rgb(h, 1, 1)

rmax = r * 255
gmax = g * 255
bmax = b * 255

dr = (255 - rmax) / 255.0
dg = (255 - gmax) / 255.0
db = (255 - bmax) / 255.0

for y in range(256):
r = g = b = 0

xdr = rmax / 255.0
xdg = gmax / 255.0
xdb = bmax / 255.0

for x in range(256):
image.set_at((x, y), (r, g, b))
r += xdr
g += xdg
b += xdb

rmax += dr
gmax += dg
bmax += db

self.color_square.udraw()

# etc, etc...


This works, but on a fairly fast machine it takes about half a second
to repaint the color square.

I have tried a few different approaches using surfarray, but every
way I tried actually made it slower. I am sure I am doing something
wrong there, plus I am concerned that surfarray is a non-standard
part of pygame and so I would have to make some way to fall back
to a non-surfarray method anyhow.

So. Two questions:

- is surfarray still considered non-standard? Or does pretty much
everyone who has pygame have surfarray?

- do you see a faster way to do this? :o)

Thanks for your time.

_________________________________________________________________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*. http://join.msn.com/?page=features/virus