| 28 | } |
| 29 | |
| 30 | void InitColor(py::module_ &m) |
| 31 | { |
| 32 | py::class_<mitk::Color>(m, "Color", |
| 33 | R"(RGB color with three float components in the closed range ``[0, 1]``. |
| 34 | |
| 35 | Indexable and iterable (length 3), so it interoperates naturally with |
| 36 | ``tuple``, ``list``, and unpacking: |
| 37 | |
| 38 | Examples: |
| 39 | >>> c = mitk.Color(1.0, 0.5, 0.0) |
| 40 | >>> r, g, b = c |
| 41 | >>> tuple(c) |
| 42 | (1.0, 0.5, 0.0) |
| 43 | )") |
| 44 | .def(py::init<>(), |
| 45 | "Construct a black color (0, 0, 0).") |
| 46 | .def(py::init(&MakeColor), py::arg("r"), py::arg("g"), py::arg("b"), |
| 47 | R"(Construct an RGB color. |
| 48 | |
| 49 | Args: |
| 50 | r: Red component in ``[0, 1]``. |
| 51 | g: Green component in ``[0, 1]``. |
| 52 | b: Blue component in ``[0, 1]``. |
| 53 | )") |
| 54 | .def_property( |
| 55 | "r", [](const mitk::Color &c) { return c.GetRed(); }, |
| 56 | [](mitk::Color &c, float v) { c.SetRed(v); }, |
| 57 | "Red component in ``[0, 1]``.") |
| 58 | .def_property( |
| 59 | "g", [](const mitk::Color &c) { return c.GetGreen(); }, |
| 60 | [](mitk::Color &c, float v) { c.SetGreen(v); }, |
| 61 | "Green component in ``[0, 1]``.") |
| 62 | .def_property( |
| 63 | "b", [](const mitk::Color &c) { return c.GetBlue(); }, |
| 64 | [](mitk::Color &c, float v) { c.SetBlue(v); }, |
| 65 | "Blue component in ``[0, 1]``.") |
| 66 | .def( |
| 67 | "__getitem__", |
| 68 | [](const mitk::Color &c, std::size_t i) |
| 69 | { |
| 70 | if (i >= 3) |
| 71 | throw py::index_error(); |
| 72 | return c[i]; |
| 73 | }, |
| 74 | "Return the i-th component (0 = red, 1 = green, 2 = blue).") |
| 75 | .def( |
| 76 | "__setitem__", |
| 77 | [](mitk::Color &c, std::size_t i, float v) |
| 78 | { |
| 79 | if (i >= 3) |
| 80 | throw py::index_error(); |
| 81 | c[i] = v; |
| 82 | }, |
| 83 | "Set the i-th component.") |
| 84 | .def("__len__", [](const mitk::Color &) { return 3; }, |
| 85 | "Number of components (always 3).") |
| 86 | .def("__iter__", |
| 87 | [](const mitk::Color &c) |