Skip to content

Commit bc5f2d1

Browse files
author
Ben Cipollini
committed
get_rgba => rgba
1 parent 6699a43 commit bc5f2d1

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

nibabel/gifti/gifti.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,31 @@ def __init__(self, key=0, label='', red=None, green=None, blue=None,
133133
self.blue = blue
134134
self.alpha = alpha
135135

136+
@np.deprecate_with_doc("Use the rgba property instead.")
136137
def get_rgba(self):
138+
return self.rgba
139+
140+
@property
141+
def rgba(self):
137142
""" Returns RGBA as tuple """
138143
return (self.red, self.green, self.blue, self.alpha)
139144

145+
@rgba.setter
146+
def rgba(self, rgba):
147+
""" Set RGBA via tuple
148+
149+
Parameters
150+
----------
151+
rgba : tuple (red, green, blue, alpha)
152+
153+
"""
154+
if len(rgba) != 4:
155+
raise ValueError('rgba must be length 4.')
156+
self.red = rgba[0]
157+
self.green = rgba[1]
158+
self.blue = rgba[2]
159+
self.alpha = rgba[3]
160+
140161

141162
def _arr2txt(arr, elem_fmt):
142163
arr = np.asarray(arr)

nibabel/gifti/tests/test_gifti.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,31 @@ def test_metadata():
6464
with clear_and_catch_warnings() as w:
6565
warnings.filterwarnings('once', category=DeprecationWarning)
6666
assert_equal(len(GiftiMetaData().get_metadata()), 0)
67+
68+
69+
def test_gifti_label_rgba():
70+
rgba = np.random.rand(4)
71+
kwargs = dict(zip(['red', 'green', 'blue', 'alpha'], rgba))
72+
73+
gl = GiftiLabel(**kwargs)
74+
assert_equal(kwargs['red'], gl.rgba[0])
75+
assert_equal(kwargs['green'], gl.rgba[1])
76+
assert_equal(kwargs['blue'], gl.rgba[2])
77+
assert_equal(kwargs['alpha'], gl.rgba[3])
78+
79+
gl = GiftiLabel()
80+
gl.rgba = rgba
81+
assert_equal(kwargs['red'], gl.rgba[0])
82+
assert_equal(kwargs['green'], gl.rgba[1])
83+
assert_equal(kwargs['blue'], gl.rgba[2])
84+
assert_equal(kwargs['alpha'], gl.rgba[3])
85+
86+
with assert_raises(ValueError):
87+
gl.rgba = rgba[:2]
88+
with assert_raises(ValueError):
89+
gl.rgba = rgba.tolist() + rgba.tolist()
90+
91+
# Test deprecation
92+
with clear_and_catch_warnings() as w:
93+
warnings.filterwarnings('once', category=DeprecationWarning)
94+
assert_equal(kwargs['red'], gl.get_rgba()[0])

0 commit comments

Comments
 (0)