Browse Source

python: add bindings for convenience color functions.

Co-authored-by: Vladimír Vondruš <mosra@centrum.cz>
pull/13/head
Aaron Gokaslan 5 years ago committed by Vladimír Vondruš
parent
commit
c50a2f6851
  1. 38
      src/python/magnum/math.vector.h
  2. 32
      src/python/magnum/test/test_math.py

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

@ -533,6 +533,19 @@ template<class T> void color3(py::class_<Math::Color3<T>, Math::Vector3<T>>& c)
return Math::Color3<T>::fromSrgb(srgb);
}, "Create linear RGB color from 24-bit sRGB representation", py::arg("srgb"))
.def_static("red", &Math::Color3<T>::red,
"Red color", py::arg("red") = Math::Implementation::fullChannel<T>())
.def_static("green", &Math::Color3<T>::green,
"Green color", py::arg("green") = Math::Implementation::fullChannel<T>())
.def_static("blue", &Math::Color3<T>::blue,
"Blue color", py::arg("blue") = Math::Implementation::fullChannel<T>())
.def_static("cyan", &Math::Color3<T>::cyan,
"Cyan color", py::arg("red") = T(0))
.def_static("magenta", &Math::Color3<T>::magenta,
"Magenta color", py::arg("green") = T(0))
.def_static("yellow", &Math::Color3<T>::yellow,
"Yellow color", py::arg("blue") = T(0))
/* Accessors */
.def("to_srgb_int", &Math::Color3<T>::toSrgbInt,
"Convert to 32-bit integral sRGB representation")
@ -583,6 +596,31 @@ template<class T> void color4(py::class_<Math::Color4<T>, Math::Vector4<T>>& c)
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>())
.def_static("red", &Math::Color4<T>::red,
"Red color",
py::arg("red") = Math::Implementation::fullChannel<T>(),
py::arg("alpha") = Math::Implementation::fullChannel<T>())
.def_static("green", &Math::Color4<T>::green,
"Green color",
py::arg("green") = Math::Implementation::fullChannel<T>(),
py::arg("alpha") = Math::Implementation::fullChannel<T>())
.def_static("blue", &Math::Color4<T>::blue,
"Blue color",
py::arg("blue") = Math::Implementation::fullChannel<T>(),
py::arg("alpha") = Math::Implementation::fullChannel<T>())
.def_static("cyan", &Math::Color4<T>::cyan,
"Cyan color",
py::arg("red") = T(0),
py::arg("alpha") = Math::Implementation::fullChannel<T>())
.def_static("magenta", &Math::Color4<T>::magenta,
"Magenta color",
py::arg("green") = T(0),
py::arg("alpha") = Math::Implementation::fullChannel<T>())
.def_static("yellow", &Math::Color4<T>::yellow,
"Yellow color",
py::arg("blue") = T(0),
py::arg("alpha") = Math::Implementation::fullChannel<T>())
/* Accessors */
.def("to_srgb_alpha_int", &Math::Color4<T>::toSrgbAlphaInt,
"Convert to 32-bit integral sRGB + linear alpha representation")

32
src/python/magnum/test/test_math.py

@ -355,6 +355,22 @@ 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_init_primaries(self):
red = Color3.red(0.5)
cyan = Color3.cyan(0.5)
self.assertEqual(red, Color3(0.5, 0.0, 0.0))
self.assertEqual(cyan, Color3(0.5, 1.0, 1.0))
green = Color3.green(0.5)
magenta = Color3.magenta(0.5)
self.assertEqual(green, Color3(0.0, 0.5, 0.0))
self.assertEqual(magenta, Color3(1.0, 0.5, 1.0))
blue = Color3.blue(0.5)
yellow = Color3.yellow(0.5)
self.assertEqual(blue, Color3(0.0, 0.0, 0.5))
self.assertEqual(yellow, Color3(1.0, 1.0, 0.5))
def test_srgb(self):
# Cross-checked with C++ tests
a = Color3.from_srgb(0xf32a80)
@ -409,6 +425,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_init_primaries(self):
red = Color4.red(0.5, 0.75)
cyan = Color4.cyan(0.5, 0.75)
self.assertEqual(red, Color4(0.5, 0.0, 0.0, 0.75))
self.assertEqual(cyan, Color4(0.5, 1.0, 1.0, 0.75))
green = Color4.green(0.5, 0.75)
magenta = Color4.magenta(0.5, 0.75)
self.assertEqual(green, Color4(0.0, 0.5, 0.0, 0.75))
self.assertEqual(magenta, Color4(1.0, 0.5, 1.0, 0.75))
blue = Color4.blue(0.5, 0.75)
yellow = Color4.yellow(0.5, 0.75)
self.assertEqual(blue, Color4(0.0, 0.0, 0.5, 0.75))
self.assertEqual(yellow, Color4(1.0, 1.0, 0.5, 0.75))
def test_srgb(self):
# Cross-checked with C++ tests
a = Color4.from_srgb(0xf32a80)

Loading…
Cancel
Save