From 94efc6e235dfe7e145f5644abe95963c722f76af Mon Sep 17 00:00:00 2001
From: Ansgar Philippsen <ansgar.philippsen@gmail.com>
Date: Sat, 6 Aug 2011 11:31:44 -0400
Subject: [PATCH] added list for gfx.Gradient ctor in python

---
 modules/gfx/pymod/export_gradient.cc | 33 ++++++++++++++++++++++++++--
 modules/gfx/tests/test_gfx.py        |  3 ++-
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/modules/gfx/pymod/export_gradient.cc b/modules/gfx/pymod/export_gradient.cc
index e24cc9cee..6cb26cee0 100644
--- a/modules/gfx/pymod/export_gradient.cc
+++ b/modules/gfx/pymod/export_gradient.cc
@@ -26,7 +26,7 @@ using namespace ost;
 using namespace ost::gfx;
 
 namespace {
-  Gradient* make_gradient(const dict& d)
+  Gradient* make_gradient_d(const dict& d)
   {
     std::auto_ptr<Gradient> grad(new Gradient);
     list keys = d.keys();
@@ -57,13 +57,42 @@ namespace {
     }
     return grad.release();
   }
+
+  Gradient* make_gradient_l(const list& l)
+  {
+    std::auto_ptr<Gradient> grad(new Gradient);
+    float mf = len(l)<2 ? 0.0 : 1.0/static_cast<float>(len(l)-1); 
+    for(int i=0;i<len(l);++i) {
+      float mark = static_cast<float>(i)*mf;
+      Color col;
+      object val = l[i];
+      extract<Color> cex(val);
+      if(cex.check()) {
+        // use gfx.Color
+        col=cex();
+      } else {
+        // try simple sequence
+        if(len(val)!=3) {
+          throw std::runtime_error("expected values of gfx.Color or float triplets");
+        }
+        try {
+          col=gfx::Color(extract<float>(val[0]),extract<float>(val[1]),extract<float>(val[2]));
+        } catch (...) {
+          throw std::runtime_error("expected values of gfx.Color or float triplets");
+        }
+      }
+      grad->SetColorAt(mark,col);
+    }
+    return grad.release();
+  }
 }
 
 void export_gradient()
 {
   class_<Gradient>("Gradient", init<>())
     .def(init<const String&>())
-    .def("__init__", make_constructor(make_gradient))
+    .def("__init__", make_constructor(make_gradient_d))
+    .def("__init__", make_constructor(make_gradient_l))
     .def("SetColorAt", &Gradient::SetColorAt)
     .def("GetColorAt", &Gradient::GetColorAt)
     .def("GetStops", &Gradient::GetStops)
diff --git a/modules/gfx/tests/test_gfx.py b/modules/gfx/tests/test_gfx.py
index f6c5f8f6e..5214a3dd7 100644
--- a/modules/gfx/tests/test_gfx.py
+++ b/modules/gfx/tests/test_gfx.py
@@ -41,7 +41,8 @@ class TestGfx(unittest.TestCase):
 
   def test_gradient(self):
     gs=[gfx.Gradient(),
-        gfx.Gradient({0.0: [1,0,0], 1.0: gfx.Color(0,1,0)})]
+        gfx.Gradient({0.0: [1,0,0], 1.0: gfx.Color(0,1,0)}),
+        gfx.Gradient([[1,0,0], gfx.Color(0,1,0)])]
     gs[0].SetColorAt(0.0,gfx.Color(1.0,0.0,0.0))
     gs[0].SetColorAt(1.0,gfx.Color(0.0,1.0,0.0))
     for g in gs:
-- 
GitLab