Browse Source

python: expose new linear RGB(A) conversion APIs from Color.

next
Vladimír Vondruš 3 years ago
parent
commit
d75f31e854
  1. 3
      doc/python/pages/changelog.rst
  2. 17
      src/python/magnum/math.vector.h
  3. 22
      src/python/magnum/test/test_math.py

3
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 <magnum.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

17
src/python/magnum/math.vector.h

@ -578,6 +578,9 @@ template<class T> void color3(py::class_<Math::Color3<T>, Math::Vector3<T>>& c)
.def_static("from_srgb_int", [](UnsignedInt srgb) {
return Math::Color3<T>::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<T>::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<class T> void color3(py::class_<Math::Color3<T>, Math::Vector3<T>>& c)
.def("saturation", &Math::Color3<T>::saturation, "Saturation")
.def("value", &Math::Color3<T>::value, "Value")
.def("to_srgb_int", &Math::Color3<T>::toSrgbInt,
"Convert to 32-bit integral sRGB representation")
"Convert to 24-bit integral sRGB representation")
.def("to_linear_rgb_int", &Math::Color3<T>::toLinearRgbInt,
"Convert to 24-bit integral linear RGB representation")
.def("to_xyz", &Math::Color3<T>::toXyz,
"Convert to CIE XYZ representation");
}
@ -641,9 +646,15 @@ template<class T> void color4(py::class_<Math::Color4<T>, Math::Vector4<T>>& c)
.def_static("from_srgb_alpha_int", [](UnsignedInt srgbAlpha) {
return Math::Color4<T>::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<T>::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<T>::fromSrgbInt(srgb, a);
}, "Create linear RGBA color from 32-bit sRGB a alpha representation", py::arg("srgb"), py::arg("a") = Math::Implementation::fullChannel<T>())
}, "Create linear RGBA color from 32-bit sRGB + alpha representation", py::arg("srgb"), py::arg("a") = Math::Implementation::fullChannel<T>())
.def_static("from_linear_rgb_int", [](UnsignedInt linear, T a) {
return Math::Color4<T>::fromLinearRgbInt(linear, a);
}, "Create linear RGBA color from 24-bit linear RGB + alpha representation", py::arg("linear"), py::arg("a") = Math::Implementation::fullChannel<T>())
.def_static("from_xyz", &Color4::fromXyz,
"Create RGBA color from CIE XYZ representation", py::arg("xyz"), py::arg("a") = Math::Implementation::fullChannel<T>())
@ -684,6 +695,8 @@ template<class T> void color4(py::class_<Math::Color4<T>, Math::Vector4<T>>& c)
.def("value", &Math::Color4<T>::value, "Value")
.def("to_srgb_alpha_int", &Math::Color4<T>::toSrgbAlphaInt,
"Convert to 32-bit integral sRGB + linear alpha representation")
.def("to_linear_rgba_int", &Math::Color4<T>::toLinearRgbaInt,
"Convert to 32-bit integral linear RGBa representation")
.def("to_xyz", &Math::Color4<T>::toXyz,
"Convert to CIE XYZ representation")

22
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))

Loading…
Cancel
Save