Skip to content

Commit c50a2f6

Browse files
Skylion007mosra
andcommitted
python: add bindings for convenience color functions.
Co-authored-by: Vladimír Vondruš <mosra@centrum.cz>
1 parent 4d5a9ed commit c50a2f6

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

src/python/magnum/math.vector.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,19 @@ template<class T> void color3(py::class_<Math::Color3<T>, Math::Vector3<T>>& c)
533533
return Math::Color3<T>::fromSrgb(srgb);
534534
}, "Create linear RGB color from 24-bit sRGB representation", py::arg("srgb"))
535535

536+
.def_static("red", &Math::Color3<T>::red,
537+
"Red color", py::arg("red") = Math::Implementation::fullChannel<T>())
538+
.def_static("green", &Math::Color3<T>::green,
539+
"Green color", py::arg("green") = Math::Implementation::fullChannel<T>())
540+
.def_static("blue", &Math::Color3<T>::blue,
541+
"Blue color", py::arg("blue") = Math::Implementation::fullChannel<T>())
542+
.def_static("cyan", &Math::Color3<T>::cyan,
543+
"Cyan color", py::arg("red") = T(0))
544+
.def_static("magenta", &Math::Color3<T>::magenta,
545+
"Magenta color", py::arg("green") = T(0))
546+
.def_static("yellow", &Math::Color3<T>::yellow,
547+
"Yellow color", py::arg("blue") = T(0))
548+
536549
/* Accessors */
537550
.def("to_srgb_int", &Math::Color3<T>::toSrgbInt,
538551
"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)
583596
return Math::Color4<T>::fromSrgb(srgb, a);
584597
}, "Create linear RGBA color from 32-bit sRGB a alpha representation", py::arg("srgb"), py::arg("a") = Math::Implementation::fullChannel<T>())
585598

599+
.def_static("red", &Math::Color4<T>::red,
600+
"Red color",
601+
py::arg("red") = Math::Implementation::fullChannel<T>(),
602+
py::arg("alpha") = Math::Implementation::fullChannel<T>())
603+
.def_static("green", &Math::Color4<T>::green,
604+
"Green color",
605+
py::arg("green") = Math::Implementation::fullChannel<T>(),
606+
py::arg("alpha") = Math::Implementation::fullChannel<T>())
607+
.def_static("blue", &Math::Color4<T>::blue,
608+
"Blue color",
609+
py::arg("blue") = Math::Implementation::fullChannel<T>(),
610+
py::arg("alpha") = Math::Implementation::fullChannel<T>())
611+
.def_static("cyan", &Math::Color4<T>::cyan,
612+
"Cyan color",
613+
py::arg("red") = T(0),
614+
py::arg("alpha") = Math::Implementation::fullChannel<T>())
615+
.def_static("magenta", &Math::Color4<T>::magenta,
616+
"Magenta color",
617+
py::arg("green") = T(0),
618+
py::arg("alpha") = Math::Implementation::fullChannel<T>())
619+
.def_static("yellow", &Math::Color4<T>::yellow,
620+
"Yellow color",
621+
py::arg("blue") = T(0),
622+
py::arg("alpha") = Math::Implementation::fullChannel<T>())
623+
586624
/* Accessors */
587625
.def("to_srgb_alpha_int", &Math::Color4<T>::toSrgbAlphaInt,
588626
"Convert to 32-bit integral sRGB + linear alpha representation")

src/python/magnum/test/test_math.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,22 @@ def test_init(self):
355355
self.assertEqual(c2, Color3(0.5, 0.75, 1.0))
356356
self.assertEqual(c3, Color3(0.5, 0.75, 1.0))
357357

358+
def test_init_primaries(self):
359+
red = Color3.red(0.5)
360+
cyan = Color3.cyan(0.5)
361+
self.assertEqual(red, Color3(0.5, 0.0, 0.0))
362+
self.assertEqual(cyan, Color3(0.5, 1.0, 1.0))
363+
364+
green = Color3.green(0.5)
365+
magenta = Color3.magenta(0.5)
366+
self.assertEqual(green, Color3(0.0, 0.5, 0.0))
367+
self.assertEqual(magenta, Color3(1.0, 0.5, 1.0))
368+
369+
blue = Color3.blue(0.5)
370+
yellow = Color3.yellow(0.5)
371+
self.assertEqual(blue, Color3(0.0, 0.0, 0.5))
372+
self.assertEqual(yellow, Color3(1.0, 1.0, 0.5))
373+
358374
def test_srgb(self):
359375
# Cross-checked with C++ tests
360376
a = Color3.from_srgb(0xf32a80)
@@ -409,6 +425,22 @@ def test_init(self):
409425
self.assertEqual(e4, Color4(0.5, 0.75, 0.875, 0.9))
410426
self.assertEqual(e5, Color4(0.5, 0.75, 0.875, 0.9))
411427

428+
def test_init_primaries(self):
429+
red = Color4.red(0.5, 0.75)
430+
cyan = Color4.cyan(0.5, 0.75)
431+
self.assertEqual(red, Color4(0.5, 0.0, 0.0, 0.75))
432+
self.assertEqual(cyan, Color4(0.5, 1.0, 1.0, 0.75))
433+
434+
green = Color4.green(0.5, 0.75)
435+
magenta = Color4.magenta(0.5, 0.75)
436+
self.assertEqual(green, Color4(0.0, 0.5, 0.0, 0.75))
437+
self.assertEqual(magenta, Color4(1.0, 0.5, 1.0, 0.75))
438+
439+
blue = Color4.blue(0.5, 0.75)
440+
yellow = Color4.yellow(0.5, 0.75)
441+
self.assertEqual(blue, Color4(0.0, 0.0, 0.5, 0.75))
442+
self.assertEqual(yellow, Color4(1.0, 1.0, 0.5, 0.75))
443+
412444
def test_srgb(self):
413445
# Cross-checked with C++ tests
414446
a = Color4.from_srgb(0xf32a80)

0 commit comments

Comments
 (0)