1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#------------------------------------------------------------------------------
# Copyright (c) 2005-2007 by Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
#------------------------------------------------------------------------------
""" Trait definition for an RGBA-based color, which is either:
* A tuple of the form (*red*,*green*,*blue*,*alpha*), where each component is
in the range from 0.0 to 1.0
* An integer which in hexadecimal is of the form 0xAARRGGBB, where AA is alpha,
RR is red, GG is green, and BB is blue.
"""
from traits.etsconfig.api import ETSConfig
from traits.api import Trait, TraitError, TraitFactory
from traits.trait_base import SequenceTypes
from .ui.api import RGBAColorEditor
if ETSConfig.toolkit == 'wx':
from traitsui.wx.color_trait import standard_colors
def rgba_color(color):
return ( color.Red() / 255.0,
color.Green() / 255.0,
color.Blue() / 255.0,
1.0 )
elif ETSConfig.toolkit == 'qt4':
from traitsui.qt4.color_trait import standard_colors
def rgba_color(color):
return ( color.red() / 255.0,
color.green() / 255.0,
color.blue() / 255.0,
1.0 )
else:
from traitsui.null.color_trait import standard_colors
def rgba_color(color):
return (((color >> 16) & 0xFF) / 255.0,
((color >> 8) & 0xFF) / 255.0,
(color & 0xFF) / 255.0 )
#-------------------------------------------------------------------------------
# Convert a value into an Enable/Kiva color:
#-------------------------------------------------------------------------------
def convert_to_color ( object, name, value ):
""" Converts a value to an Enable or Kiva color.
"""
if ((type( value ) in SequenceTypes) and
(len( value ) == 4) and
(0.0 <= value[0] <= 1.0) and
(0.0 <= value[1] <= 1.0) and
(0.0 <= value[2] <= 1.0) and
(0.0 <= value[3] <= 1.0)):
return value
if type( value ) is int:
result = ( ((value >> 24) & 0xFF) / 255.0,
((value >> 16) & 0xFF) / 255.0,
((value >> 8) & 0xFF) / 255.0,
(value & 0xFF) / 255.0 )
return result
raise TraitError
convert_to_color.info = ('a tuple of the form (red,green,blue,alpha), where '
'each component is in the range from 0.0 to 1.0, or '
'an integer which in hex is of the form 0xAARRGGBB, '
'where AA is alpha, RR is red, GG is green, and BB is '
'blue')
#-------------------------------------------------------------------------------
# Standard colors:
#-------------------------------------------------------------------------------
# RGBA versions of standard colors
rgba_standard_colors = {}
for name, color in standard_colors.items():
rgba_standard_colors[ name ] = rgba_color(color)
rgba_standard_colors[ 'clear' ] = ( 0, 0, 0, 0 )
#-------------------------------------------------------------------------------
# Define Enable/Kiva specific color traits:
#-------------------------------------------------------------------------------
def RGBAColorFunc(*args, **metadata):
"""
Returns a trait whose value must be a GUI toolkit-specific RGBA-based color.
Description
-----------
For wxPython, the returned trait accepts any of the following values:
* A tuple of the form (*r*, *g*, *b*, *a*), in which *r*, *g*, *b*, and *a*
represent red, green, blue, and alpha values, respectively, and are floats
in the range from 0.0 to 1.0
* An integer whose hexadecimal form is 0x*AARRGGBB*, where *AA* is the alpha
(transparency) value, *RR* is the red value, *GG* is the green value, and
*BB* is the blue value
Default Value
-------------
For wxPython, (1.0, 1.0, 1.0, 1.0) (that is, opaque white)
"""
tmp_trait = Trait( 'white', convert_to_color, rgba_standard_colors,
editor = RGBAColorEditor )
return tmp_trait(*args, **metadata)
RGBAColorTrait = TraitFactory( RGBAColorFunc )
RGBAColor = RGBAColorTrait
|