Skip to content

Commit ff07f67

Browse files
committed
Add new step alpha function
1 parent 621f890 commit ff07f67

File tree

12 files changed

+212
-149
lines changed

12 files changed

+212
-149
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@
22

33
## Version 2.1.2 ##
44

5-
Compatibility with PythonQwt 0.12.
5+
New features:
6+
7+
* Added `Echelon` alpha function to the image parameters:
8+
* The `Echelon` alpha function is a step function, so that the alpha channel is
9+
0 (full transparency) for the lowest value of the Lookup Table (LUT) and opaque
10+
(transparency level set by the `Global alpha` parameter) for the other values
11+
* This feature is added to the other existing alpha functions: `Constant`, `Linear`,
12+
`Sigmoid`, and `Hyperbolic tangent`
13+
14+
Bug fixes:
15+
16+
* Compatibility with PythonQwt 0.12.
617

718
## Version 2.1.1 ##
819

doc/dev/guiqwt_to_plotpy.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ functions are:
189189
* :py:attr:`plotpy.constants.LUTAlpha.LINEAR`
190190
* :py:attr:`plotpy.constants.LUTAlpha.SIGMOID`
191191
* :py:attr:`plotpy.constants.LUTAlpha.TANH`
192+
* :py:attr:`plotpy.constants.LUTAlpha.STEP`
192193

193194
.. warning:: The ``alpha_mask`` parameter has been removed from the methods
194195
:py:meth:`.PlotBuilder.image`, :py:meth:`.PlotBuilder.xyimage`,

doc/dev/v1_to_v2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ functions are:
7777
* :py:attr:`plotpy.constants.LUTAlpha.LINEAR`
7878
* :py:attr:`plotpy.constants.LUTAlpha.SIGMOID`
7979
* :py:attr:`plotpy.constants.LUTAlpha.TANH`
80+
* :py:attr:`plotpy.constants.LUTAlpha.STEP`
8081

8182
.. warning:: The ``alpha_mask`` parameter has been removed from the methods
8283
:py:meth:`.PlotBuilder.image`, :py:meth:`.PlotBuilder.xyimage`,

plotpy/builder/image.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ def image(
168168
filename: image filename. Default is None
169169
title: image title. Default is None
170170
alpha_function: function for LUT alpha channel.
171-
Default is :py:attr:`.LUTAlpha.NONE`
172-
(valid string values are 'none', 'constant', 'linear', 'sigmoid', 'tanh')
171+
Default is :py:attr:`.LUTAlpha.NONE` (valid string values are 'none',
172+
'constant', 'linear', 'sigmoid', 'tanh' and 'step')
173173
alpha: alpha value. Default is None
174174
background_color: background color name. Default is None
175175
colormap: colormap name. Default is None
@@ -317,8 +317,8 @@ def maskedimage(
317317
filename: image filename. Default is None
318318
title: image title. Default is None
319319
alpha_function: function for LUT alpha channel.
320-
Default is :py:attr:`.LUTAlpha.NONE`
321-
(valid string values are 'none', 'constant', 'linear', 'sigmoid', 'tanh')
320+
Default is :py:attr:`.LUTAlpha.NONE` (valid string values are 'none',
321+
'constant', 'linear', 'sigmoid', 'tanh' and 'step')
322322
alpha: alpha value. Default is 1.0
323323
xdata: x data. Default is [None, None]
324324
ydata: y data. Default is [None, None]
@@ -445,8 +445,8 @@ def maskedxyimage(
445445
mask: mask. Default is None
446446
title: image title. Default is None
447447
alpha_function: function for LUT alpha channel.
448-
Default is :py:attr:`.LUTAlpha.NONE`
449-
(valid string values are 'none', 'constant', 'linear', 'sigmoid', 'tanh')
448+
Default is :py:attr:`.LUTAlpha.NONE` (valid string values are 'none',
449+
'constant', 'linear', 'sigmoid', 'tanh' and 'step')
450450
alpha: alpha value. Default is 1.0
451451
background_color: background color. Default is None
452452
colormap: colormap. Default is None
@@ -519,8 +519,8 @@ def rgbimage(
519519
filename: filename. Default is None
520520
title: image title. Default is None
521521
alpha_function: function for LUT alpha channel.
522-
Default is :py:attr:`.LUTAlpha.NONE`
523-
(valid string values are 'none', 'constant', 'linear', 'sigmoid', 'tanh')
522+
Default is :py:attr:`.LUTAlpha.NONE` (valid string values are 'none',
523+
'constant', 'linear', 'sigmoid', 'tanh' and 'step')
524524
alpha: alpha value. Default is 1.0
525525
xdata: x data. Default is [None, None]
526526
ydata: y data. Default is [None, None]
@@ -584,8 +584,8 @@ def quadgrid(
584584
Z: data
585585
title: image title. Default is None
586586
alpha_function: function for LUT alpha channel.
587-
Default is :py:attr:`.LUTAlpha.NONE`
588-
(valid string values are 'none', 'constant', 'linear', 'sigmoid', 'tanh')
587+
Default is :py:attr:`.LUTAlpha.NONE` (valid string values are 'none',
588+
'constant', 'linear', 'sigmoid', 'tanh' and 'step')
589589
alpha: alpha value. Default is None
590590
colormap: colormap. Default is None
591591
interpolation: interpolation method. Default is 'linear'
@@ -662,8 +662,8 @@ def trimage(
662662
filename: filename. Default is None
663663
title: image title. Default is None
664664
alpha_function: function for LUT alpha channel.
665-
Default is :py:attr:`.LUTAlpha.NONE`
666-
(valid string values are 'none', 'constant', 'linear', 'sigmoid', 'tanh')
665+
Default is :py:attr:`.LUTAlpha.NONE` (valid string values are 'none',
666+
'constant', 'linear', 'sigmoid', 'tanh' and 'step')
667667
alpha: alpha value. Default is None
668668
background_color: background color. Default is None
669669
colormap: colormap. Default is None
@@ -743,8 +743,8 @@ def xyimage(
743743
data: data
744744
title: image title. Default is None
745745
alpha_function: function for LUT alpha channel.
746-
Default is :py:attr:`.LUTAlpha.NONE`
747-
(valid string values are 'none', 'constant', 'linear', 'sigmoid', 'tanh')
746+
Default is :py:attr:`.LUTAlpha.NONE` (valid string values are 'none',
747+
'constant', 'linear', 'sigmoid', 'tanh' and 'step')
748748
alpha: alpha value. Default is None
749749
background_color: background color. Default is None
750750
colormap: colormap. Default is None

plotpy/constants.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ class LUTAlpha(enum.Enum):
102102
#: Hyperbolic tangent alpha function
103103
TANH = 4
104104

105+
#: Transparent lowest value and opaque highest value
106+
STEP = 5
107+
105108
@classmethod
106109
def get_member_from_name(cls: type["LUTAlpha"], name: str) -> "LUTAlpha|None":
107110
"""Return the member of the enum from its name"""
@@ -110,16 +113,34 @@ def get_member_from_name(cls: type["LUTAlpha"], name: str) -> "LUTAlpha|None":
110113
return member
111114
return None
112115

113-
def get_choices(self):
116+
@classmethod
117+
def get_choices(cls: type["LUTAlpha"]) -> list[tuple[int, str]]:
114118
"""Return the list of choices"""
115119
return [
116120
(LUTAlpha.NONE.value, _("None")),
117121
(LUTAlpha.CONSTANT.value, _("Constant")),
118122
(LUTAlpha.LINEAR.value, _("Linear")),
119123
(LUTAlpha.SIGMOID.value, _("Sigmoid")),
120124
(LUTAlpha.TANH.value, _("Hyperbolic tangent")),
125+
(LUTAlpha.STEP.value, _("Step")),
121126
]
122127

128+
@classmethod
129+
def get_help(cls: type["LUTAlpha"]) -> str:
130+
"""Return tooltip help for all alpha functions"""
131+
return _(
132+
"Alpha function applied to the Look-Up Table (LUT)<br>"
133+
"to control the transparency of the image:<br>"
134+
"(maximum opacity is given by the <b>Global alpha</b> parameter)<br><br>"
135+
"<b>None</b>: No alpha function<br>"
136+
"<b>Constant</b>: Constant alpha function<br>"
137+
"<b>Linear</b>: Linear alpha function<br>"
138+
"<b>Sigmoid</b>: Sigmoid alpha function<br>"
139+
"<b>Hyperbolic tangent</b>: Hyperbolic tangent alpha function<br>"
140+
"<b>Step</b>: Lowest value of the LUT is 100% transparent, other "
141+
"values are opaque"
142+
)
143+
123144

124145
# Lookup table size
125146
LUT_SIZE = 1024

plotpy/items/image/base.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,17 @@ def set_color_map(self, name_or_table: str | EditableColormap) -> None:
442442
else:
443443
x = i / float(LUT_SIZE - 1)
444444
if alpha_function == LUTAlpha.LINEAR.value:
445+
# Linear alpha function
445446
pix_alpha = alpha * x
446447
elif alpha_function == LUTAlpha.SIGMOID.value:
448+
# Sigmoid alpha function
447449
pix_alpha = alpha / (1 + np.exp(-10 * x))
448450
elif alpha_function == LUTAlpha.TANH.value:
451+
# Hyperbolic tangent alpha function
449452
pix_alpha = alpha * np.tanh(5 * x)
453+
elif alpha_function == LUTAlpha.STEP.value:
454+
# Fully transparent lowest value and `alpha` transparent elsewhere
455+
pix_alpha = alpha if x > 0 else 0
450456
else:
451457
raise ValueError(f"Invalid alpha function {alpha_function}")
452458
# pylint: disable=too-many-function-args
@@ -1220,9 +1226,11 @@ def __setstate__(self, state: tuple) -> None:
12201226

12211227
def serialize(
12221228
self,
1223-
writer: guidata.dataset.io.HDF5Writer
1224-
| guidata.dataset.io.INIWriter
1225-
| guidata.dataset.io.JSONWriter,
1229+
writer: (
1230+
guidata.dataset.io.HDF5Writer
1231+
| guidata.dataset.io.INIWriter
1232+
| guidata.dataset.io.JSONWriter
1233+
),
12261234
) -> None:
12271235
"""Serialize object to HDF5 writer
12281236
@@ -1242,9 +1250,11 @@ def serialize(
12421250

12431251
def deserialize(
12441252
self,
1245-
reader: guidata.dataset.io.HDF5Reader
1246-
| guidata.dataset.io.INIReader
1247-
| guidata.dataset.io.JSONReader,
1253+
reader: (
1254+
guidata.dataset.io.HDF5Reader
1255+
| guidata.dataset.io.INIReader
1256+
| guidata.dataset.io.JSONReader
1257+
),
12481258
) -> None:
12491259
"""Deserialize object from HDF5 reader
12501260
939 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)