From f3559f0619dd748f8ce333d85180bc6859048b7f Mon Sep 17 00:00:00 2001 From: Ansgar Philippsen <ansgar.philippsen@gmail.com> Date: Wed, 13 Jul 2011 21:11:20 -0400 Subject: [PATCH] tweaked gfx color pymod interface --- modules/gfx/pymod/CMakeLists.txt | 1 + modules/gfx/pymod/export_color.cc | 108 ++++++++++++++++++++++++++++++ modules/gfx/pymod/wrap_gfx.cc | 33 +-------- modules/gfx/tests/CMakeLists.txt | 1 + modules/gfx/tests/test_color.py | 35 ++++++++++ 5 files changed, 147 insertions(+), 31 deletions(-) create mode 100644 modules/gfx/pymod/export_color.cc create mode 100644 modules/gfx/tests/test_color.py diff --git a/modules/gfx/pymod/CMakeLists.txt b/modules/gfx/pymod/CMakeLists.txt index 0fcc9a8ed..253e8e599 100644 --- a/modules/gfx/pymod/CMakeLists.txt +++ b/modules/gfx/pymod/CMakeLists.txt @@ -10,6 +10,7 @@ set(OST_GFX_PYMOD_SOURCES export_primlist.cc export_scene_observer.cc export_render_options.cc + export_color.cc export_color_ops.cc export_glwin_base.cc ) diff --git a/modules/gfx/pymod/export_color.cc b/modules/gfx/pymod/export_color.cc new file mode 100644 index 000000000..022f7669f --- /dev/null +++ b/modules/gfx/pymod/export_color.cc @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// This file is part of the OpenStructure project <www.openstructure.org> +// +// Copyright (C) 2008-2011 by the OpenStructure authors +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License as published by the Free +// Software Foundation; either version 3.0 of the License, or (at your option) +// any later version. +// This library is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +// details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this library; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +//------------------------------------------------------------------------------ +#include <boost/python.hpp> +using namespace boost::python; + +#include <ost/message.hh> +#include <ost/gfx/color.hh> +using namespace ost; +using namespace ost::gfx; + +namespace { + float get_red(const Color& c) { + return c[0]; + } + + void set_red(Color& c, float v) { + c[0]=v; + } + + float get_green(const Color& c) { + return c[1]; + } + + void set_green(Color& c, float v) { + c[1]=v; + } + + float get_blue(const Color& c) { + return c[2]; + } + + void set_blue(Color& c, float v) { + c[2]=v; + } + + float get_alpha(const Color& c) { + return c[3]; + } + + void set_alpha(Color& c, float v) { + c[3]=v; + } + + float get(const Color& c, int i) { + if(i<0 || i>3) { + throw Error("Color: index out of bounds"); + } + return c[i]; + } + + void set(Color& c, int i, float v) { + if(i<0 || i>3) { + throw Error("Color: index out of bounds"); + } + c[i]=v; + } + + std::string repr(const Color& c) { + std::ostringstream m; + m << "gfx.Color(" << c[0] << "," << c[1] << "," << c[2] << "," << c[3] << ")"; + return m.str(); + } + +} + +void export_color() +{ + class_<Color>("Color",init<>()) + .def(init<float, float, float, optional<float> >()) + .def(self_ns::str(self)) + .def("__repr__",repr) + .def("Red",get_red) + .def("Green",get_green) + .def("Blue",get_blue) + .def("Alpha",get_alpha) + .def("ToHSV",&Color::ToHSV) + .def("FromRGBA",&Color::FromRGB) + .add_property("r",get_red,set_red) + .add_property("g",get_green,set_green) + .add_property("b",get_blue,set_blue) + .add_property("a",get_alpha,set_alpha) + .add_property("red",get_red,set_red) + .add_property("green",get_green,set_green) + .add_property("blue",get_blue,set_blue) + .add_property("alpha",get_alpha,set_alpha) + .def("__getitem__",get) + .def("__setitem__",get) + ; + + def("HSV",HSV); + +} diff --git a/modules/gfx/pymod/wrap_gfx.cc b/modules/gfx/pymod/wrap_gfx.cc index 4be1d63f2..c77b5bc5c 100644 --- a/modules/gfx/pymod/wrap_gfx.cc +++ b/modules/gfx/pymod/wrap_gfx.cc @@ -36,6 +36,7 @@ extern void export_Entity(); extern void export_Surface(); extern void export_primlist(); extern void export_primitives(); +extern void export_color(); #if OST_IMG_ENABLED extern void export_Map(); #endif @@ -51,26 +52,9 @@ extern void export_GLWinBase(); using namespace ost; using namespace ost::gfx; - -float color_get_red(Color* c) { - return c->Red(); -} - -float color_get_green(Color* c) { - return c->Green(); -} - -float color_get_blue(Color* c) { - return c->Blue(); -} - -float color_get_alpha(Color* c) { - return c->Alpha(); -} - - BOOST_PYTHON_MODULE(_ost_gfx) { + export_color(); export_Scene(); export_GfxNode(); export_GfxObj(); @@ -138,19 +122,6 @@ BOOST_PYTHON_MODULE(_ost_gfx) class_<GfxTestObj, bases<GfxObj>, boost::noncopyable>("GfxTestObj", init<>()); - class_<Color>("Color",init<>()) - .def(init<float, float, float, optional<float> >()) - .def(self_ns::str(self)) - .def("Red",color_get_red) - .def("Green",color_get_green) - .def("Blue",color_get_blue) - .def("Alpha",color_get_alpha) - .def("ToHSV",&Color::ToHSV) - .def("FromRGBA",&Color::FromRGB) - ; - - def("HSV",HSV); - class_<Gradient>("Gradient", init<>()) .def(init<const String&>()) .def("SetColorAt", &Gradient::SetColorAt) diff --git a/modules/gfx/tests/CMakeLists.txt b/modules/gfx/tests/CMakeLists.txt index c87cf9183..adf0a6769 100644 --- a/modules/gfx/tests/CMakeLists.txt +++ b/modules/gfx/tests/CMakeLists.txt @@ -2,6 +2,7 @@ set(OST_GFX_UNIT_TESTS tests.cc test_gfx_node.cc test_primlist.py + test_color.py ) if (ENABLE_IMG) list(APPEND OST_GFX_UNIT_TESTS test_map_octree.cc) diff --git a/modules/gfx/tests/test_color.py b/modules/gfx/tests/test_color.py new file mode 100644 index 000000000..58a1afa04 --- /dev/null +++ b/modules/gfx/tests/test_color.py @@ -0,0 +1,35 @@ +import unittest +if __name__== '__main__': + import sys + sys.path.insert(0,"../../../stage/lib64/openstructure/") + +import ost +#import ost.gfx +#import ost.geom + +class TestColor(unittest.TestCase): + def setUp(self): + pass + + def test_(self): + c=ost.gfx.Color(0.5,0.3,0.2) + self.assertAlmostEqual(c.r,0.5) + self.assertAlmostEqual(c.g,0.3) + self.assertAlmostEqual(c.b,0.2) + self.assertAlmostEqual(c.a,1.0) + self.assertAlmostEqual(c.red,0.5) + self.assertAlmostEqual(c.green,0.3) + self.assertAlmostEqual(c.blue,0.2) + self.assertAlmostEqual(c.alpha,1.0) + self.assertAlmostEqual(c[0],0.5) + self.assertAlmostEqual(c[1],0.3) + self.assertAlmostEqual(c[2],0.2) + self.assertAlmostEqual(c[3],1.0) + c.r=0.9 + self.assertAlmostEqual(c.r,0.9) + self.assertAlmostEqual(c.red,0.9) + self.assertAlmostEqual(c[0],0.9) + +if __name__== '__main__': + unittest.main() + -- GitLab