kalzium
color.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <config.h>
00027
00028 #include <avogadro/color.h>
00029
00030 using namespace OpenBabel;
00031
00032 namespace Avogadro {
00033
00034 Color::Color( GLfloat red, GLfloat green, GLfloat blue,
00035 GLfloat alpha )
00036 {
00037 set(red, green, blue, alpha);
00038 }
00039
00040 Color::Color( const OBAtom* atom )
00041 {
00042 set(atom);
00043 }
00044
00045 Color& Color::operator=( const Color& other )
00046 {
00047 m_red = other.m_red;
00048 m_green = other.m_green;
00049 m_blue = other.m_blue;
00050 m_alpha = other.m_alpha;
00051
00052 return *this;
00053 }
00054
00055 void Color::set(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
00056 {
00057 m_red = red;
00058 m_green = green;
00059 m_blue = blue;
00060 m_alpha = alpha;
00061 }
00062
00063 void Color::set(const OpenBabel::OBAtom *atom)
00064 {
00065 if (!atom)
00066 return;
00067
00068 std::vector<double> rgb = etab.GetRGB( atom->GetAtomicNum() );
00069 m_red = rgb[0];
00070 m_green = rgb[1];
00071 m_blue = rgb[2];
00072 m_alpha = 1.0;
00073 }
00074
00075 void Color::set(double value, double low, double high)
00076 {
00077 Q_UNUSED(value);
00078 Q_UNUSED(low);
00079 Q_UNUSED(high);
00080 m_red = m_green = m_blue = m_alpha = 1.0;
00081 }
00082
00083 void Color::applyAsMaterials()
00084 {
00085 GLfloat ambientColor [] = { m_red / 3, m_green / 3, m_blue / 3,
00086 m_alpha };
00087 GLfloat diffuseColor [] = { m_red, m_green, m_blue, m_alpha };
00088
00089 float s = ( 0.5 + fabsf( m_red - m_green )
00090 + fabsf( m_blue - m_green ) + fabsf( m_blue - m_red ) ) / 4.0;
00091
00092 float t = 1.0 - s;
00093
00094 GLfloat specularColor [] = { s + t * m_red,
00095 s + t * m_green,
00096 s + t * m_blue,
00097 m_alpha };
00098
00099 glMaterialfv( GL_FRONT, GL_AMBIENT, ambientColor );
00100 glMaterialfv( GL_FRONT, GL_DIFFUSE, diffuseColor );
00101 glMaterialfv( GL_FRONT, GL_SPECULAR, specularColor );
00102 glMaterialf( GL_FRONT, GL_SHININESS, 50.0 );
00103 }
00104
00105 }