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 #include <math.h>
00030
00031 namespace Avogadro {
00032
00033 class ColorPrivate {
00034 public:
00035 ColorPrivate()
00036 { }
00037
00038 ~ColorPrivate()
00039 { }
00040 };
00041
00042 Color::Color(): d(0) {
00043 }
00044
00045 Color::~Color() {
00046
00047 }
00048
00049 Color::Color( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha ):
00050 m_red(red), m_green(green), m_blue(blue), m_alpha(alpha), d(0)
00051 { }
00052
00053 Color::Color( const Primitive *p ): d(0)
00054 {
00055 set(p);
00056 }
00057
00058 Color& Color::operator=( const QColor& other )
00059 {
00060 m_red = other.red();
00061 m_green = other.green();
00062 m_blue = other.blue();
00063 m_alpha = other.alpha();
00064
00065 return *this;
00066 }
00067
00068 void Color::set(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
00069 {
00070 m_red = red;
00071 m_green = green;
00072 m_blue = blue;
00073 m_alpha = alpha;
00074 }
00075
00076 void Color::setToSelectionColor()
00077 {
00078 m_red = 0.3;
00079 m_green = 0.6;
00080 m_blue = 1.0;
00081 m_alpha = 0.7;
00082 }
00083
00084 void Color::set(const Primitive *)
00085 {
00086 return;
00087 }
00088
00089 void Color::setAlpha(double alpha)
00090 {
00091 m_alpha = alpha;
00092 }
00093
00094 void Color::applyAsMaterials()
00095 {
00096 GLfloat ambientColor [] = { m_red / 3, m_green / 3, m_blue / 3,
00097 m_alpha };
00098 GLfloat diffuseColor [] = { m_red, m_green, m_blue, m_alpha };
00099
00100 float s = ( 0.5 + fabsf( m_red - m_green )
00101 + fabsf( m_blue - m_green ) + fabsf( m_blue - m_red ) ) / 4.0;
00102
00103 float t = 1.0 - s;
00104
00105 GLfloat specularColor [] = { s + t * m_red,
00106 s + t * m_green,
00107 s + t * m_blue,
00108 m_alpha };
00109
00110 glMaterialfv( GL_FRONT, GL_AMBIENT, ambientColor );
00111 glMaterialfv( GL_FRONT, GL_DIFFUSE, diffuseColor );
00112 glMaterialfv( GL_FRONT, GL_SPECULAR, specularColor );
00113 glMaterialf( GL_FRONT, GL_SHININESS, 50.0 );
00114 }
00115
00116 void Color::applyAsFlatMaterials()
00117 {
00118 GLfloat diffuseColor [] = { m_red, m_green, m_blue, m_alpha };
00119
00120 glMaterialfv( GL_FRONT, GL_AMBIENT, diffuseColor );
00121 glMaterialfv( GL_FRONT, GL_DIFFUSE, diffuseColor );
00122 glMaterialfv( GL_FRONT, GL_SPECULAR, diffuseColor );
00123 glMaterialf( GL_FRONT, GL_SHININESS, 1.0 );
00124 }
00125
00126 void Color::setName(const QString& name)
00127 {
00128 m_name = name;
00129 }
00130
00131 QString Color::name() const
00132 {
00133 if (m_name.isEmpty())
00134 return type();
00135 else
00136 return m_name;
00137 }
00138 }