diff --git a/doc/python/pages/changelog.rst b/doc/python/pages/changelog.rst index 8700cdd..7e6b6c7 100644 --- a/doc/python/pages/changelog.rst +++ b/doc/python/pages/changelog.rst @@ -50,7 +50,8 @@ Changelog :ref:`Matrix3.projection()` - Exposed remaining vector/scalar, exponential and other functions in the :ref:`math ` library -- Exposed :ref:`Color3.from_xyz()`, :ref:`Color3.to_xyz()` and equivalent +- Exposed :ref:`Color3.from_xyz()`, :ref:`Color3.from_linear_rgb_int()`, + :ref:`Color3.to_xyz()`, :ref:`Color3.to_linear_rgb_int()` and equivalent APIs on :ref:`Color4` - Exposed :ref:`gl.Context` and its platform-specific subclasses for EGL, WGL and GLX diff --git a/src/python/magnum/math.vector.h b/src/python/magnum/math.vector.h index 706db51..7dd7226 100644 --- a/src/python/magnum/math.vector.h +++ b/src/python/magnum/math.vector.h @@ -578,6 +578,9 @@ template void color3(py::class_, Math::Vector3>& c) .def_static("from_srgb_int", [](UnsignedInt srgb) { return Math::Color3::fromSrgbInt(srgb); }, "Create linear RGB color from 24-bit sRGB representation", py::arg("srgb")) + .def_static("from_linear_rgb_int", [](UnsignedInt linear) { + return Math::Color3::fromLinearRgbInt(linear); + }, "Create linear RGB color from 24-bit linear representation", py::arg("linear")) .def_static("from_xyz", &Color3::fromXyz, "Create RGB color from CIE XYZ representation", py::arg("xyz")) @@ -605,7 +608,9 @@ template void color3(py::class_, Math::Vector3>& c) .def("saturation", &Math::Color3::saturation, "Saturation") .def("value", &Math::Color3::value, "Value") .def("to_srgb_int", &Math::Color3::toSrgbInt, - "Convert to 32-bit integral sRGB representation") + "Convert to 24-bit integral sRGB representation") + .def("to_linear_rgb_int", &Math::Color3::toLinearRgbInt, + "Convert to 24-bit integral linear RGB representation") .def("to_xyz", &Math::Color3::toXyz, "Convert to CIE XYZ representation"); } @@ -641,9 +646,15 @@ template void color4(py::class_, Math::Vector4>& c) .def_static("from_srgb_alpha_int", [](UnsignedInt srgbAlpha) { return Math::Color4::fromSrgbAlphaInt(srgbAlpha); }, "Create linear RGBA color from 32-bit sRGB a alpha representation", py::arg("srgb_alpha")) + .def_static("from_linear_rgba_int", [](UnsignedInt linear) { + return Math::Color4::fromLinearRgbaInt(linear); + }, "Create linear RGBA color from 32-bit linear representation", py::arg("linear")) .def_static("from_srgb_int", [](UnsignedInt srgb, T a) { return Math::Color4::fromSrgbInt(srgb, a); - }, "Create linear RGBA color from 32-bit sRGB a alpha representation", py::arg("srgb"), py::arg("a") = Math::Implementation::fullChannel()) + }, "Create linear RGBA color from 32-bit sRGB + alpha representation", py::arg("srgb"), py::arg("a") = Math::Implementation::fullChannel()) + .def_static("from_linear_rgb_int", [](UnsignedInt linear, T a) { + return Math::Color4::fromLinearRgbInt(linear, a); + }, "Create linear RGBA color from 24-bit linear RGB + alpha representation", py::arg("linear"), py::arg("a") = Math::Implementation::fullChannel()) .def_static("from_xyz", &Color4::fromXyz, "Create RGBA color from CIE XYZ representation", py::arg("xyz"), py::arg("a") = Math::Implementation::fullChannel()) @@ -684,6 +695,8 @@ template void color4(py::class_, Math::Vector4>& c) .def("value", &Math::Color4::value, "Value") .def("to_srgb_alpha_int", &Math::Color4::toSrgbAlphaInt, "Convert to 32-bit integral sRGB + linear alpha representation") + .def("to_linear_rgba_int", &Math::Color4::toLinearRgbaInt, + "Convert to 32-bit integral linear RGBa representation") .def("to_xyz", &Math::Color4::toXyz, "Convert to CIE XYZ representation") diff --git a/src/python/magnum/test/test_math.py b/src/python/magnum/test/test_math.py index d41fdb6..9d10da6 100644 --- a/src/python/magnum/test/test_math.py +++ b/src/python/magnum/test/test_math.py @@ -481,6 +481,12 @@ class Color3_(unittest.TestCase): self.assertEqual(a, Color3(0.896269, 0.0231534, 0.215861)) self.assertEqual(a.to_srgb_int(), 0xf32a80) + def test_linear_rgb(self): + # Cross-checked with C++ tests + a = Color3.from_linear_rgb_int(0xf32a80) + self.assertEqual(a, Color3(0.952941, 0.164706, 0.501961)) + self.assertEqual(a.to_linear_rgb_int(), 0xf32a80) + def test_hsv(self): a = Color3.from_hsv(Deg(230.0), 0.749, 0.427) self.assertEqual(a, Color3(0.107177, 0.160481, 0.427)) @@ -552,6 +558,13 @@ class Color4_(unittest.TestCase): self.assertEqual(a.to_srgb_alpha_int(), 0xf32a80ff) + def test_linear_rgb(self): + # Cross-checked with C++ tests + a = Color4.from_linear_rgb_int(0xf32a80) + self.assertEqual(a, Color4(0.952941, 0.164706, 0.501961, 1.0)) + + self.assertEqual(a.to_linear_rgba_int(), 0xf32a80ff) + def test_srgb_alpha(self): # Cross-checked with C++ tests a = Color4.from_srgb_int(0xf32a80, a=0.137255) @@ -561,6 +574,15 @@ class Color4_(unittest.TestCase): self.assertEqual(a.to_srgb_alpha_int(), 0xf32a8023) + def test_linear_rgba(self): + # Cross-checked with C++ tests + a = Color4.from_linear_rgb_int(0xf32a80, a=0.137255) + b = Color4.from_linear_rgba_int(0xf32a8023) + self.assertEqual(a, Color4(0.952941, 0.164706, 0.501961, 0.137255)) + self.assertEqual(b, Color4(0.952941, 0.164706, 0.501961, 0.137255)) + + self.assertEqual(a.to_linear_rgba_int(), 0xf32a8023) + def test_hsv(self): a = Color4.from_hsv(Deg(230.0), 0.749, 0.427, 0.95) self.assertEqual(a, Color4(0.107177, 0.160481, 0.427, 0.95))