Commit 525d3861 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

Merge branch 'rel/1.7.2' into 'v1.7'

Rel/1.7.2

See merge request fsl/fsleyes/props!46
parents 7085e65a d166c2fd
...@@ -2,6 +2,21 @@ This document contains the ``fsleyes-props`` release history in reverse ...@@ -2,6 +2,21 @@ This document contains the ``fsleyes-props`` release history in reverse
chronological order. chronological order.
1.7.2 (Saturday March 26th 2021)
--------------------------------
Changed
^^^^^^^
* The :class:`.Color` property type now accepts any value that is accepted by
the `matplotlib.to_rgba
<https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.to_rgba.html>`_
function.
* Properties of type The :class:`.Int` and :class:`.Real` can be set to
``None`` (unless ``required=True and allowInvalid=False``).
1.7.1 (Tuesday March 9th 2021) 1.7.1 (Tuesday March 9th 2021)
------------------------------ ------------------------------
......
...@@ -291,7 +291,7 @@ notification of properties to be suppressed in a ``with`` statement. ...@@ -291,7 +291,7 @@ notification of properties to be suppressed in a ``with`` statement.
""" """
__version__ = '1.7.1' __version__ = '1.7.2'
import sys import sys
......
...@@ -35,7 +35,8 @@ import os.path as op ...@@ -35,7 +35,8 @@ import os.path as op
from collections import abc from collections import abc
import numpy as np import matplotlib.colors as mplcolors
import numpy as np
from . import properties as props from . import properties as props
from . import properties_value as propvals from . import properties_value as propvals
...@@ -161,6 +162,9 @@ class Number(props.PropertyBase): ...@@ -161,6 +162,9 @@ class Number(props.PropertyBase):
is returned unchanged. is returned unchanged.
""" """
if value is None:
return value
clamped = attributes['clamped'] clamped = attributes['clamped']
if not clamped: return value if not clamped: return value
...@@ -186,6 +190,8 @@ class Int(Number): ...@@ -186,6 +190,8 @@ class Int(Number):
"""Overrides :meth:`Number.cast`. Casts the given value to an ``int``, """Overrides :meth:`Number.cast`. Casts the given value to an ``int``,
and then passes the value to :meth:`Number.cast`. and then passes the value to :meth:`Number.cast`.
""" """
if value is None:
return value
return Number.cast(self, instance, attributes, int(value)) return Number.cast(self, instance, attributes, int(value))
...@@ -221,6 +227,8 @@ class Real(Number): ...@@ -221,6 +227,8 @@ class Real(Number):
"""Overrides :meth:`Number.cast`. Casts the given value to a ``float``, """Overrides :meth:`Number.cast`. Casts the given value to a ``float``,
and then passes the value to :meth:`Number.cast`. and then passes the value to :meth:`Number.cast`.
""" """
if value is None:
return value
return Number.cast(self, instance, attributes, float(value)) return Number.cast(self, instance, attributes, float(value))
...@@ -767,8 +775,8 @@ class Colour(props.PropertyBase): ...@@ -767,8 +775,8 @@ class Colour(props.PropertyBase):
"""A property which represents a RGBA colour, stored as four floating """A property which represents a RGBA colour, stored as four floating
point values in the range ``0.0 - 1.0``. point values in the range ``0.0 - 1.0``.
RGB colours are also accepted - if an RGB colour is provided, the Any value which can be interpreted by matplotlib as a RGB(A) colour is
alpha channel is set to 1.0. accepted. If an RGB colour is provided, the alpha channel is set to 1.0.
""" """
...@@ -795,14 +803,7 @@ class Colour(props.PropertyBase): ...@@ -795,14 +803,7 @@ class Colour(props.PropertyBase):
range ``(0.0 - 1.0)``. range ``(0.0 - 1.0)``.
""" """
props.PropertyBase.validate(self, instance, attributes, value) props.PropertyBase.validate(self, instance, attributes, value)
mplcolors.to_rgba(value)
if (not isinstance(value, abc.Sequence)) or \
(len(value) not in (3, 4)):
raise ValueError('Colour must be a sequence of three/four values')
for v in value:
if (v < 0.0) or (v > 1.0):
raise ValueError('Colour values must be between 0.0 and 1.0')
def cast(self, instance, attributes, value): def cast(self, instance, attributes, value):
...@@ -812,24 +813,8 @@ class Colour(props.PropertyBase): ...@@ -812,24 +813,8 @@ class Colour(props.PropertyBase):
If the alpha channel is not provided, it is set to the current alpha If the alpha channel is not provided, it is set to the current alpha
value (which defaults to ``1.0``). value (which defaults to ``1.0``).
""" """
if value is not None: return mplcolors.to_rgba(value)
pv = self.getPropVal(instance) else: return value
if pv is not None: currentVal = pv.get()
else: currentVal = self.getAttribute(None, 'default')
value = [float(v) for v in value]
if len(value) == 3:
value = value + [currentVal[3]]
value = value[:4]
for i, v in enumerate(value):
if v < 0.0: value[i] = 0.0
if v > 1.0: value[i] = 1.0
return value
class ColourMap(props.PropertyBase): class ColourMap(props.PropertyBase):
......
...@@ -15,6 +15,7 @@ def test_Int(): ...@@ -15,6 +15,7 @@ def test_Int():
class MyObj(props.HasProperties): class MyObj(props.HasProperties):
unbounded = props.Int() unbounded = props.Int()
required = props.Int(required=True, allowInvalid=False)
unbounded_default = props.Int(default=10) unbounded_default = props.Int(default=10)
bounded = props.Int(minval=0, maxval=10) bounded = props.Int(minval=0, maxval=10)
bounded_min = props.Int(minval=0) bounded_min = props.Int(minval=0)
...@@ -26,14 +27,17 @@ def test_Int(): ...@@ -26,14 +27,17 @@ def test_Int():
obj = MyObj() obj = MyObj()
# property, value, expected
assert obj.unbounded_default == 10 assert obj.unbounded_default == 10
with pytest.raises(ValueError): obj.unbounded = '' with pytest.raises(ValueError): obj.unbounded = ''
with pytest.raises(ValueError): obj.unbounded = 'abcde' with pytest.raises(ValueError): obj.unbounded = 'abcde'
with pytest.raises(TypeError): obj.unbounded = None with pytest.raises(ValueError): obj.required = None
obj.unbounded = None
obj.required = 10
# property, value, expected
testcases = [ testcases = [
('unbounded', '-999', -999), ('unbounded', '-999', -999),
('unbounded', '0', 0), ('unbounded', '0', 0),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment