diff --git a/src/python/magnum/math.vector.h b/src/python/magnum/math.vector.h index b35e4ef..37f3968 100644 --- a/src/python/magnum/math.vector.h +++ b/src/python/magnum/math.vector.h @@ -527,7 +527,13 @@ template void color3(py::class_, Math::Vector3>& c) return Math::Color3::fromHsv({Math::Deg(hue), saturation, value}); }, "Create RGB color from HSV representation", py::arg("hue"), py::arg("saturation"), py::arg("value")) + .def_static("from_srgb", [](UnsignedInt srgb) { + return Math::Color3::fromSrgb(srgb); + }, "Create linear RGB color from 24-bit sRGB representation", py::arg("srgb")) + /* Accessors */ + .def("to_srgb_int", &Math::Color3::toSrgbInt, + "Convert to 32-bit integral sRGB representation") .def("to_hsv", [](Math::Color3& self) { auto hsv = self.toHsv(); return std::make_tuple(Degd(hsv.hue), hsv.saturation, hsv.value); @@ -567,7 +573,17 @@ template void color4(py::class_, Math::Vector4>& c) return Math::Color4::fromHsv({Math::Deg(hue), saturation, value}, alpha); }, "Create RGB color from HSV representation", py::arg("hue"), py::arg("saturation"), py::arg("value"), py::arg("alpha") = Math::Implementation::fullChannel()) + .def_static("from_srgb_alpha", [](UnsignedInt srgbAlpha) { + return Math::Color4::fromSrgbAlpha(srgbAlpha); + }, "Create linear RGBA color from 32-bit sRGB a alpha representation", py::arg("srgb_alpha")) + + .def_static("from_srgb", [](UnsignedInt srgb, T a) { + return Math::Color4::fromSrgb(srgb, a); + }, "Create linear RGBA color from 32-bit sRGB a alpha representation", py::arg("srgb"), py::arg("a") = Math::Implementation::fullChannel()) + /* Accessors */ + .def("to_srgb_alpha_int", &Math::Color4::toSrgbAlphaInt, + "Convert to 32-bit integral sRGB + linear alpha representation") .def("to_hsv", [](Math::Color4& self) { auto hsv = self.toHsv(); return std::make_tuple(Degd(hsv.hue), hsv.saturation, hsv.value); diff --git a/src/python/magnum/test/test_math.py b/src/python/magnum/test/test_math.py index 5e7df25..772f74a 100644 --- a/src/python/magnum/test/test_math.py +++ b/src/python/magnum/test/test_math.py @@ -355,6 +355,12 @@ class Color3_(unittest.TestCase): self.assertEqual(c2, Color3(0.5, 0.75, 1.0)) self.assertEqual(c3, Color3(0.5, 0.75, 1.0)) + def test_srgb(self): + # Cross-checked with C++ tests + a = Color3.from_srgb(0xf32a80) + self.assertEqual(a, Color3(0.896269, 0.0231534, 0.215861)) + self.assertEqual(a.to_srgb_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)) @@ -403,6 +409,22 @@ class Color4_(unittest.TestCase): self.assertEqual(e4, Color4(0.5, 0.75, 0.875, 0.9)) self.assertEqual(e5, Color4(0.5, 0.75, 0.875, 0.9)) + def test_srgb(self): + # Cross-checked with C++ tests + a = Color4.from_srgb(0xf32a80) + self.assertEqual(a, Color4(0.896269, 0.0231534, 0.215861, 1.0)) + + self.assertEqual(a.to_srgb_alpha_int(), 0xf32a80ff) + + def test_srgb_alpha(self): + # Cross-checked with C++ tests + a = Color4.from_srgb(0xf32a80, a=0.137255) + b = Color4.from_srgb_alpha(0xf32a8023) + self.assertEqual(a, Color4(0.896269, 0.0231534, 0.215861, 0.137255)) + self.assertEqual(b, Color4(0.896269, 0.0231534, 0.215861, 0.137255)) + + self.assertEqual(a.to_srgb_alpha_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))