Color and ColorTrait

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Color and ColorTrait

Brennan Williams
Hi

I'm using Color rather than ColorTrait (what is the difference?) and I'm
trying to compare a colour name (e.g. "green") with the colours in an
existing list of colours.

My list of colours is (or should be) a list of Color's and if I print
them out they are printed as...

(255, 0, 0, 255)
etc etc.

but they were originally defined as Color('red'), Color('green') etc etc.

so what is the correct/best way to compare a name to my colour list? As
I can edit each colour and change its r,g,b components what I really
need to do is convert my name 'green' into the same r,g,b,a format. But
I don't see how I can do that using Color.

Thx in advance

Brennan




_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Color and ColorTrait

fred-2
Le 20/01/2011 07:09, Brennan Williams a écrit :

> Hi
>
> I'm using Color rather than ColorTrait (what is the difference?) and I'm
> trying to compare a colour name (e.g. "green") with the colours in an
> existing list of colours.
>
> My list of colours is (or should be) a list of Color's and if I print
> them out they are printed as...
>
> (255, 0, 0, 255)
> etc etc.
>
> but they were originally defined as Color('red'), Color('green') etc etc.
>
> so what is the correct/best way to compare a name to my colour list? As
> I can edit each colour and change its r,g,b components what I really
> need to do is convert my name 'green' into the same r,g,b,a format. But
> I don't see how I can do that using Color.
Hi Brennan,

I struggle against same kind of issues with Color traits (lot of issues
between Color, colors used by Chaco, colors used by Mayavi, WX Color &
QT Color, etc.).

This is a really a pain...

But I can get working what I want, so...

Please look at this:

class MyClass(HasTraits):

     c = Color('red')

     view = View(Item('c',
                      style='custom'),
                 resizable=True)

     def _c_changed(self):
         print self.c[0], self.c[1], self.c[2]

if __name__ == '__main__':
     my_class = MyClass()
     my_class.configure_traits()


For instance, this only works for WX backend, not for Qt.

For Qt, you have to do this:

     def _c_changed(self):
         c0 = self.c.getRgb()
         print c0[0], c0[1], c0[2]


HTH



Cheers,

--
Fred
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Color and ColorTrait

Chris Colbert
In reply to this post by Brennan Williams


On Thu, Jan 20, 2011 at 1:09 AM, Brennan Williams <[hidden email]> wrote:
Hi

I'm using Color rather than ColorTrait (what is the difference?)

Color is a factory function that returns a ColorTrait
 
and I'm
trying to compare a colour name (e.g. "green") with the colours in an
existing list of colours.

If you look at the docstring for Color you'll see that it should be a gui-toolkit specific color. For some reason (I didn't go looking for the reason), the docstring for Color doesn't show up at the Ipython prompt. Here it is from the source:

   """ Returns a trait whose value must be a GUI toolkit-specific color.

    Description
    -----------
    For wxPython, the returned trait accepts any of the following values:

    * A wx.Colour instance
    * A wx.ColourPtr instance
    * an integer whose hexadecimal form is 0x*RRGGBB*, where *RR* is the red
      value, *GG* is the green value, and *BB* is the blue value

    Default Value
    -------------
    For wxPython, 0x000000 (that is, white)
    """
 
My list of colours is (or should be) a list of Color's and if I print
them out they are printed as...
 
Printing does not always tell you what you have. Remember that you can override just about any behavior in Python. In this case the __str__ and __repr__ methods of an object define how it behaves when printed to stdout. The safest bet to check the type of the object you have is to use type(...):

In [8]: class Foo(HasTraits):
   ...:     a = Color
   ...:     
   ...:     

In [9]: f = Foo()

In [10]: print f.a
(255, 255, 255, 255)

In [11]: f.a
Out[11]: wx.Colour(255, 255, 255, 255)

In [12]: type(f.a)
Out[12]: <class 'wx._gdi.Colour'>


(255, 0, 0, 255)
etc etc.

but they were originally defined as Color('red'), Color('green') etc etc.

so what is the correct/best way to compare a name to my colour list? As
I can edit each colour and change its r,g,b components what I really
need to do is convert my name 'green' into the same r,g,b,a format. But
I don't see how I can do that using Color.

You will need to create an instance of the gui-toolkit specific color (you can do this with another color trait) and test that against your list of colors: 


In [14]: c1 = wx.Color(0, 0, 0)

In [15]: c2 = wx.Color(0, 0, 0)

In [16]: c1 == c2
Out[16]: True

In [17]: c_list = [c2]

In [18]: c1 in c_list
Out[18]: True

Caveat: Other gui-toolkit color objects may behave differently than wx.
 
However, the easiest way to do all of this is just let traits handle the underlying color objects:

In [20]: class Foo(HasTraits):
   ....:     c1 = Color
   ....:     c_list = List(Color)
   ....:     
   ....:     

In [21]: f = Foo()

In [22]: f.c1
Out[22]: wx.Colour(255, 255, 255, 255)

In [23]: f.c_list
Out[23]: []

In [24]: f.c_list.append(f.c1)

In [25]: f.c_list.append('red')

In [26]: f.c_list.append('green')

In [27]: f.c_list.append('blue')

In [28]: f.c_list
Out[28]: 
[wx.Colour(255, 255, 255, 255),
 wx.Colour(255, 0, 0, 255),
 wx.Colour(0, 128, 0, 255),
 wx.Colour(0, 0, 255, 255)]

In [29]: 'red' in f.c_list
Out[29]: True

In [30]: 'yellow' in f.c_list
Out[30]: False

In [31]: 'white' in f.c_list
Out[31]: True

Thx in advance

Brennan




_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev