@@ -38,6 +38,20 @@ def brightness(self, value):
3838 self ._validate_brightness_value (value )
3939 self ._color .brightness = value
4040
41+ @property
42+ def transparency (self ):
43+ """
44+ Read/write float value between 0.0 and 1.0 indicating the transparency
45+ of this color, e.g. 0.0 is completely opaque and 1.0 is completely
46+ transparent. 0.5 is 50% transparent.
47+ """
48+ return self ._color .transparency
49+
50+ @transparency .setter
51+ def transparency (self , value ):
52+ self ._validate_transparency_value (value )
53+ self ._color .transparency = value
54+
4155 @classmethod
4256 def from_colorchoice_parent (cls , eg_colorChoice_parent ):
4357 xClr = eg_colorChoice_parent .eg_colorChoice
@@ -106,6 +120,16 @@ def _validate_brightness_value(self, value):
106120 )
107121 raise ValueError (msg )
108122
123+ def _validate_transparency_value (self , value ):
124+ if value < 0.0 or value > 1.0 :
125+ raise ValueError ("transparency must be number in range 0.0 to 1.0" )
126+ if isinstance (self ._color , _NoneColor ):
127+ msg = (
128+ "can't set transparency when color.type is None. Set color.rgb"
129+ " or .theme_color first."
130+ )
131+ raise ValueError (msg )
132+
109133
110134class _Color (object ):
111135 """
@@ -153,6 +177,42 @@ def brightness(self, value):
153177 else :
154178 self ._xClr .clear_lum ()
155179
180+ @property
181+ def transparency (self ):
182+ """
183+ Read/write float value between 0.0 and 1.0 indicating the transparency
184+ of this color. 0.0 is completely opaque, 1.0 is completely transparent.
185+ """
186+ if self ._xClr is None :
187+ # NoneColor has no transparency
188+ return 0.0
189+ alpha = self ._xClr .alpha
190+ if alpha is not None :
191+ # alpha.val is in range 0.0-1.0, where 1.0 = 100% opaque
192+ # transparency is 1.0 - alpha.val
193+ return 1.0 - alpha .val
194+ # no alpha element means fully opaque (0% transparent)
195+ return 0.0
196+
197+ @transparency .setter
198+ def transparency (self , value ):
199+ if self ._xClr is None :
200+ # NoneColor cannot have transparency set
201+ msg = (
202+ "can't set transparency when color.type is None. Set color.rgb"
203+ " or .theme_color first."
204+ )
205+ raise ValueError (msg )
206+ if value == 0.0 :
207+ # fully opaque, remove alpha element
208+ self ._xClr .clear_alpha ()
209+ else :
210+ # convert transparency (0.0-1.0) to alpha value (1.0-0.0)
211+ # alpha = 1.0 - transparency
212+ alpha_val = 1.0 - value
213+ self ._xClr .clear_alpha ()
214+ self ._xClr .add_alpha (alpha_val )
215+
156216 @property
157217 def color_type (self ): # pragma: no cover
158218 tmpl = ".color_type property must be implemented on %s"
0 commit comments