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

Re: [pygame] Color, unpacking to r,g,b and r,g,b,a - ValueError: too many values to unpack



On Thu, Jun 18, 2009 at 10:38 AM, Brian Fisher<brian@xxxxxxxxxxxxxxxxxxx> wrote:
> You could also just let those 2 functions return exactly what they did
> before, no color obj for them...
>

Yeah, that's another option to consider.  Except returning Color is so
much nicer to use than a tuple for colors (and slightly quicker... but
that's not a big point).  Also it should be more consistent to have
all the methods returning Colors for colors.


Some notes on the set_length option...

Color.set_length(len)
set the amount of elements in the Color to 1,2,3, or 4.  By default
the Color length is 4.
The constraints on len are: len < 4 and len > 0.
This is useful if you want to unpack to r,g,b and not r,g,b,a


>>> c = Color(1,2,3,4)
>>> c.set_length(3)
>>> len(c)
3
>>>r,g,b = c
>>>r,g,b
1, 2, 3
>>> c.a
4
>>> c[3]
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
IndexError: invalid index
>>> c.set_length(4)
>>>r,g,b,a = c
>>>r,g,b,a
1, 2, 3, 4
>>> len(c)
4


Then only have the get_palette(_at) functions set the length to 3 by default.

As you can see above, the elements are still there, just the item
assignment doesn't work.  You can still access items through c.a c.r
c.g c.b etc.

It also makes sense(in a way) because some colors are only r,g,b - not r,g,b,a.


cu.