From 4b07def43371f72791fca3b22c68468e2a7ab36a Mon Sep 17 00:00:00 2001
From: stefan <stefan@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Wed, 14 Jul 2010 12:09:04 +0000
Subject: [PATCH] ColorOps, new algorithm for color by chain

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2560 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/gfx/pymod/export_color_ops.cc         |  2 +
 .../gfx/src/color_ops/by_chain_color_op.cc    | 40 +++++++++----------
 .../gfx/src/color_ops/by_chain_color_op.hh    |  5 ++-
 modules/gfx/src/color_ops/color_op.hh         |  1 -
 modules/gfx/src/entity.cc                     |  2 +
 5 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/modules/gfx/pymod/export_color_ops.cc b/modules/gfx/pymod/export_color_ops.cc
index d24a15179..78f23bac9 100644
--- a/modules/gfx/pymod/export_color_ops.cc
+++ b/modules/gfx/pymod/export_color_ops.cc
@@ -75,6 +75,8 @@ void export_ColorOps()
     .def(init<const mol::QueryViewWrapper&>())
     .def(init<const String&, int>())
     .def(init<const mol::QueryViewWrapper&, int>())
+    .def("GetChainCount",&ByChainColorOp::GetChainCount)
+    .def("SetChainCount",&ByChainColorOp::SetChainCount)
     .def("FromInfo",&ByChainColorOp::FromInfo)
     .staticmethod("FromInfo")
   ;
diff --git a/modules/gfx/src/color_ops/by_chain_color_op.cc b/modules/gfx/src/color_ops/by_chain_color_op.cc
index 908fb591f..67279f893 100644
--- a/modules/gfx/src/color_ops/by_chain_color_op.cc
+++ b/modules/gfx/src/color_ops/by_chain_color_op.cc
@@ -26,20 +26,11 @@
 
 namespace ost { namespace gfx {
 
-ByChainColorOp::ByChainColorOp() : ColorOp(){this->init();}
+ByChainColorOp::ByChainColorOp() : ColorOp(),chain_count_(0),color_grad_("RAINBOW"){}
 
-ByChainColorOp::ByChainColorOp(const String& selection, int mask) : ColorOp(selection,mask){this->init();}
+ByChainColorOp::ByChainColorOp(const String& selection, int mask) : ColorOp(selection,mask),chain_count_(0),color_grad_("RAINBOW"){}
 
-ByChainColorOp::ByChainColorOp(const mol::QueryViewWrapper& query_view, int mask) : ColorOp(query_view,mask){this->init();}
-
-void ByChainColorOp::init(){
-  color_grad_.SetColorAt(0,Color(1,1,0));
-  color_grad_.SetColorAt(0.16666,Color(1,0,0));
-  color_grad_.SetColorAt(0.33333,Color(0,1,1));
-  color_grad_.SetColorAt(0.5,Color(0,1,0));
-  color_grad_.SetColorAt(0.66666,Color(1,0,1));
-  color_grad_.SetColorAt(0.83333,Color(0,0,1));
-}
+ByChainColorOp::ByChainColorOp(const mol::QueryViewWrapper& query_view, int mask) : ColorOp(query_view,mask),chain_count_(0),color_grad_("RAINBOW"){}
 
 bool ByChainColorOp::CanApplyTo(const GfxObjP& obj) const{
   if(dynamic_cast<Entity*>(obj.get()))
@@ -62,6 +53,16 @@ gfx::Color ByChainColorOp::GetColor(String ident) const{
   return colors_[ident];
 }
 
+int ByChainColorOp::GetChainCount() const
+{
+  return chain_count_;
+}
+
+void ByChainColorOp::SetChainCount(int chain_count)
+{
+  chain_count_ = chain_count;
+}
+
 gfx::ByChainColorOp ByChainColorOp::FromInfo(info::InfoGroup& group){
   gfx::ColorOp op = ColorOp::FromInfo(group);
   String selection = op.GetSelection();
@@ -71,18 +72,13 @@ gfx::ByChainColorOp ByChainColorOp::FromInfo(info::InfoGroup& group){
 
 gfx::Color ByChainColorOp::GenerateColor(String& ident) const{
   unsigned int size=colors_.size()-1;
-  int cnt = size % 6;
-  int factor = abs(size/6.0)+1;
-  float start = 1.0/(6.0*factor);
-  if(factor > 1 && !((factor+1) & 1)){
-    float old_start = 1.0 / (6.0 *abs(size/6.0));
-    start += old_start;
+  if(size<=0){
+    colors_[ident] = color_grad_.GetColorAt(0.0);
   }
-  float value = start + (cnt / 6.0);
-  if(value >= 1){
-    value -=1;
+  else{
+    colors_[ident] = color_grad_.GetColorAt(float(size) / chain_count_);
   }
-  return color_grad_.GetColorAt(value);
+  return colors_[ident];
 }
 
 }}
diff --git a/modules/gfx/src/color_ops/by_chain_color_op.hh b/modules/gfx/src/color_ops/by_chain_color_op.hh
index fffc89668..30d3ec2e6 100644
--- a/modules/gfx/src/color_ops/by_chain_color_op.hh
+++ b/modules/gfx/src/color_ops/by_chain_color_op.hh
@@ -43,13 +43,16 @@ public:
 
   virtual gfx::Color GetColor(String ident) const;
 
+  virtual int GetChainCount() const;
+  virtual void SetChainCount(int chain_count);
+
   //virtual void ToInfo(info::InfoGroup& group) const;
   static gfx::ByChainColorOp FromInfo(info::InfoGroup& group);
 
 private:
-  void init();
   gfx::Color GenerateColor(String& ident) const;
 
+  mutable int chain_count_;
   mutable std::map<String,gfx::Color> colors_;
 
   gfx::Gradient color_grad_;
diff --git a/modules/gfx/src/color_ops/color_op.hh b/modules/gfx/src/color_ops/color_op.hh
index c2c7c9360..d1a98d88e 100644
--- a/modules/gfx/src/color_ops/color_op.hh
+++ b/modules/gfx/src/color_ops/color_op.hh
@@ -66,7 +66,6 @@ public:
   static gfx::ColorOp FromInfo(info::InfoGroup& group);
 private:
   mol::QueryViewWrapper query_view_;
-  mol::EntityView view_;
   ColorMask mask_;
 };
 
diff --git a/modules/gfx/src/entity.cc b/modules/gfx/src/entity.cc
index 8d639a336..2da4d22f9 100644
--- a/modules/gfx/src/entity.cc
+++ b/modules/gfx/src/entity.cc
@@ -908,11 +908,13 @@ void Entity::Apply(const gfx::ByElementColorOp& op, bool store)
 
 void Entity::Apply(const gfx::ByChainColorOp& op, bool store)
 {
+  const_cast<gfx::ByChainColorOp&>(op).SetChainCount(this->GetView().GetChainCount());
   if(store){
     ByChainColorOp* op_ptr = new ByChainColorOp(op);
     this->AppendColorOp(op_ptr);
   }
   apply_color_op_to_renderer_list(renderer_.begin(), renderer_.end(), op);
+  const_cast<gfx::ByChainColorOp&>(op).SetChainCount(0);
   FlagRebuild();
 }
 
-- 
GitLab