Skip to content
Snippets Groups Projects
Commit f3559f06 authored by Ansgar Philippsen's avatar Ansgar Philippsen
Browse files

tweaked gfx color pymod interface

parent 883817f4
Branches
Tags
No related merge requests found
......@@ -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
)
......
//------------------------------------------------------------------------------
// 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);
}
......@@ -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)
......
......@@ -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)
......
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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment