summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmcolor.cpp
blob: 5b279d303682fe0f285cc54067d34f0cff119a79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2000-2001 by Andreas Zehender
    email                : [email protected]
**************************************************************************

**************************************************************************
*                                                                        *
*  This program is free software; you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation; either version 2 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
**************************************************************************/


#include "pmcolor.h"
#include "pmvector.h"
#include "pmmath.h"

#include "pmdebug.h"

#include <textstream.h>
#include <tqstring.h>

PMColor::PMColor( )
{
   int i;
   for( i = 0; i < 5; i++ )
      m_colorValue[i] = 0;
}

PMColor::PMColor( const double red, const double green, const double blue,
         const double filter, const double transmit )
{
   m_colorValue[0] = red;
   m_colorValue[1] = green;
   m_colorValue[2] = blue;
   m_colorValue[3] = filter;
   m_colorValue[4] = transmit;
}

PMColor::PMColor( const double red, const double green, const double blue )
{
   m_colorValue[0] = red;
   m_colorValue[1] = green;
   m_colorValue[2] = blue;
   m_colorValue[3] = 0;
   m_colorValue[4] = 0;
}

PMColor::PMColor( const PMVector& v )
{
   int i;
   if( v.size( ) != 5 )
   {
      kdError( PMArea ) << "Vector has wrong size in PMColor::PMColor( const PMVector& v )\n";
      for( i = 0; i < 5; i++ )
         m_colorValue[i] = 0;
   }
   else
   {
      for( i = 0; i < 5; i++ )
         m_colorValue[i] = v[i];
   }
}

PMColor::PMColor( const TQColor& c )
{
   m_colorValue[0] = c.red( ) / 255.0;
   m_colorValue[1] = c.green( ) / 255.0;
   m_colorValue[2] = c.blue( ) / 255.0;
   m_colorValue[3] = 0.0;
   m_colorValue[4] = 0.0;
}

TQColor PMColor::toTQColor( ) const
{
   double r, g, b, max = 0;
   r = red( );
   g = green( );
   b = blue( );

   if( r < 0 ) r = 0;
   if( g < 0 ) g = 0;
   if( b < 0 ) b = 0;
   
   if( r > max )
      max = r;
   if( g > max )
      max = g;
   if( b > max )
      max = b;
   if( max > 1 )
   {
      r /= max;
      g /= max;
      b /= max;
   }
   return TQColor( ( int ) ( r * 255 + 0.5 ), ( int ) ( g * 255 + 0.5 ),
                  ( int ) ( b * 255 + 0.5 ) );
}

TQString PMColor::serialize( bool addColorKeyword ) const
{
   TQString result;
   TQTextStream str( &result, IO_WriteOnly );

   if( addColorKeyword )
      str << "color ";

   if( approxZero( m_colorValue[4] ) )
   {
      if( approxZero( m_colorValue[3] ) )
      {
         // rgb
         str << "rgb <" << m_colorValue[0] << ", " << m_colorValue[1] << ", "
             << m_colorValue[2] << '>';
      }
      else
      {
         // rgbf
         str << "rgbf <" << m_colorValue[0] << ", " << m_colorValue[1] << ", "
             << m_colorValue[2] << ", " << m_colorValue[3] << '>';
      }
   }
   else
   {
      if( approxZero( m_colorValue[3] ) )
      {
         // rgbt
         str << "rgbt <" << m_colorValue[0] << ", " << m_colorValue[1] << ", "
             << m_colorValue[2] << ", " << m_colorValue[4] << '>';
      }
      else
      {
         // rgbft
         str << "rgbft <" << m_colorValue[0] << ", " << m_colorValue[1] << ", "
             << m_colorValue[2] << ", " << m_colorValue[3]
             << ", " << m_colorValue[4] << '>';
      }
   }
   
   return result;
}

TQString PMColor::serializeXML( ) const
{
   TQString result;
   TQTextStream str( &result, IO_WriteOnly );
   int i;

   for( i = 0; i < 5; i++ )
   {
      if( i != 0 )
         str << ' ';
      str << m_colorValue[i];
   }
   
   return result;
}

bool PMColor::operator!= ( const PMColor& c ) const
{
   return !( *this == c );
}

bool PMColor::operator== ( const PMColor& c ) const
{
   unsigned int i;

   for( i = 0; i < 5; i++ )
      if( c.m_colorValue[i] != m_colorValue[i] )
         return false;
   return true;
}

bool PMColor::loadXML( const TQString& str )
{
   TQString tmp( str );
   TQTextStream s( &tmp, IO_ReadOnly );
   TQString val;
   bool ok;
   int i;
   
   for( i = 0; i < 5; i++ )
   {
      s >> val;
      m_colorValue[i] = val.toDouble( &ok );
      if( !ok )
         return false;
   }
   return true;
}