Prechádzať zdrojové kódy

added property Label.color_code

Fabian Peter Hammerle 5 rokov pred
rodič
commit
7b621b3b97
2 zmenil súbory, kde vykonal 25 pridanie a 0 odobranie
  1. 5 0
      freesurfer_surface/__init__.py
  2. 20 0
      tests/test_label.py

+ 5 - 0
freesurfer_surface/__init__.py

@@ -66,6 +66,11 @@ class Label:
     green: int
     transparency: int
 
+    @property
+    def color_code(self):
+        return int.from_bytes((self.transparency, self.red, self.green, self.blue),
+                              byteorder='big', signed=False)
+
 
 class Annotation:
 

+ 20 - 0
tests/test_label.py

@@ -0,0 +1,20 @@
+import pytest
+
+from freesurfer_surface import Label
+
+
+@pytest.mark.parametrize(('red', 'green', 'blue', 'transparency', 'color_code'), [
+    # pylint: disable=bad-whitespace
+    (100,  20, 220,   0, 6558940),
+    (140,  30,  20,   0, 9182740),
+    (140,  30,  20,   1, 9182740 + (1 << (8 * 3))),
+    (140,  30,  20,   7, 9182740 + (7 << (8 * 3))),
+    (140,  30,  20, 123, 2072780308),
+])
+def test_color_code(red, green, blue, transparency, color_code):
+    label = Label()
+    label.red = red
+    label.green = green
+    label.blue = blue
+    label.transparency = transparency
+    assert color_code == label.color_code