Browse Source

python: add APIs for Color from/to sRGB int.

I need that for generating colormaps.
pull/9/head
Vladimír Vondruš 6 years ago
parent
commit
500311fa7c
  1. 16
      src/python/magnum/math.vector.h
  2. 22
      src/python/magnum/test/test_math.py

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

@ -527,7 +527,13 @@ template<class T> void color3(py::class_<Math::Color3<T>, Math::Vector3<T>>& c)
return Math::Color3<T>::fromHsv({Math::Deg<T>(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<T>::fromSrgb(srgb);
}, "Create linear RGB color from 24-bit sRGB representation", py::arg("srgb"))
/* Accessors */
.def("to_srgb_int", &Math::Color3<T>::toSrgbInt,
"Convert to 32-bit integral sRGB representation")
.def("to_hsv", [](Math::Color3<T>& self) {
auto hsv = self.toHsv();
return std::make_tuple(Degd(hsv.hue), hsv.saturation, hsv.value);
@ -567,7 +573,17 @@ template<class T> void color4(py::class_<Math::Color4<T>, Math::Vector4<T>>& c)
return Math::Color4<T>::fromHsv({Math::Deg<T>(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<T>())
.def_static("from_srgb_alpha", [](UnsignedInt srgbAlpha) {
return Math::Color4<T>::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<T>::fromSrgb(srgb, a);
}, "Create linear RGBA color from 32-bit sRGB a alpha representation", py::arg("srgb"), py::arg("a") = Math::Implementation::fullChannel<T>())
/* Accessors */
.def("to_srgb_alpha_int", &Math::Color4<T>::toSrgbAlphaInt,
"Convert to 32-bit integral sRGB + linear alpha representation")
.def("to_hsv", [](Math::Color4<T>& self) {
auto hsv = self.toHsv();
return std::make_tuple(Degd(hsv.hue), hsv.saturation, hsv.value);

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

Loading…
Cancel
Save