xref: /trunk/main/slideshow/source/engine/color.cxx (revision d3e5b9da5ecd3a21afd95e21a5fd36807d0753aa)
170f497fbSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
370f497fbSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
470f497fbSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
570f497fbSAndrew Rist  * distributed with this work for additional information
670f497fbSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
770f497fbSAndrew Rist  * to you under the Apache License, Version 2.0 (the
870f497fbSAndrew Rist  * "License"); you may not use this file except in compliance
970f497fbSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1170f497fbSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1370f497fbSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1470f497fbSAndrew Rist  * software distributed under the License is distributed on an
1570f497fbSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1670f497fbSAndrew Rist  * KIND, either express or implied.  See the License for the
1770f497fbSAndrew Rist  * specific language governing permissions and limitations
1870f497fbSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2070f497fbSAndrew Rist  *************************************************************/
2170f497fbSAndrew Rist 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_slideshow.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <hslcolor.hxx>
26cdf0e10cSrcweir #include <rgbcolor.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <cmath> // for fmod
31cdf0e10cSrcweir #include <algorithm>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir namespace slideshow
34cdf0e10cSrcweir {
35cdf0e10cSrcweir     namespace internal
36cdf0e10cSrcweir     {
37cdf0e10cSrcweir         namespace
38cdf0e10cSrcweir         {
39cdf0e10cSrcweir             // helper functions
40cdf0e10cSrcweir             // ================
41cdf0e10cSrcweir 
getMagic(double nLuminance,double nSaturation)42cdf0e10cSrcweir             double getMagic( double nLuminance, double nSaturation )
43cdf0e10cSrcweir             {
44cdf0e10cSrcweir                 if( nLuminance <= 0.5 )
45cdf0e10cSrcweir                     return nLuminance*(1.0 + nSaturation);
46cdf0e10cSrcweir                 else
47cdf0e10cSrcweir                     return nLuminance + nSaturation - nLuminance*nSaturation;
48cdf0e10cSrcweir             }
49cdf0e10cSrcweir 
rgb2hsl(double nRed,double nGreen,double nBlue)50cdf0e10cSrcweir             HSLColor::HSLTriple rgb2hsl( double nRed, double nGreen, double nBlue )
51cdf0e10cSrcweir             {
52cdf0e10cSrcweir                 // r,g,b in [0,1], h in [0,360] and s,l in [0,1]
53cdf0e10cSrcweir                 HSLColor::HSLTriple aRes;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir                 const double nMax( ::std::max(nRed,::std::max(nGreen, nBlue)) );
56cdf0e10cSrcweir                 const double nMin( ::std::min(nRed,::std::min(nGreen, nBlue)) );
57cdf0e10cSrcweir 
58cdf0e10cSrcweir                 const double nDelta( nMax - nMin );
59cdf0e10cSrcweir 
60cdf0e10cSrcweir                 aRes.mnLuminance = (nMax + nMin) / 2.0;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir                 if( ::basegfx::fTools::equalZero( nDelta ) )
63cdf0e10cSrcweir                 {
64cdf0e10cSrcweir                     aRes.mnSaturation = 0.0;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir                     // hue undefined (achromatic case)
67cdf0e10cSrcweir                     aRes.mnHue = 0.0;
68cdf0e10cSrcweir                 }
69cdf0e10cSrcweir                 else
70cdf0e10cSrcweir                 {
71cdf0e10cSrcweir                     aRes.mnSaturation = aRes.mnLuminance > 0.5 ?
72cdf0e10cSrcweir                         nDelta/(2.0-nMax-nMin) :
73cdf0e10cSrcweir                         nDelta/(nMax + nMin);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir                     if( nRed == nMax )
76cdf0e10cSrcweir                         aRes.mnHue = (nGreen - nBlue)/nDelta;
77cdf0e10cSrcweir                     else if( nGreen == nMax )
78cdf0e10cSrcweir                         aRes.mnHue = 2.0 + (nBlue - nRed)/nDelta;
79cdf0e10cSrcweir                     else if( nBlue == nMax )
80cdf0e10cSrcweir                         aRes.mnHue = 4.0 + (nRed - nGreen)/nDelta;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir                     aRes.mnHue *= 60.0;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir                     if( aRes.mnHue < 0.0 )
85cdf0e10cSrcweir                         aRes.mnHue += 360.0;
86cdf0e10cSrcweir                 }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir                 return aRes;
89cdf0e10cSrcweir             }
90cdf0e10cSrcweir 
hsl2rgbHelper(double nValue1,double nValue2,double nHue)91cdf0e10cSrcweir             double hsl2rgbHelper( double nValue1, double nValue2, double nHue )
92cdf0e10cSrcweir             {
93cdf0e10cSrcweir                 // clamp hue to [0,360]
94cdf0e10cSrcweir                 nHue = fmod( nHue, 360.0 );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir                 // cope with wrap-arounds
97cdf0e10cSrcweir                 if( nHue < 0.0 )
98cdf0e10cSrcweir                     nHue += 360.0;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir                 if( nHue < 60.0 )
101cdf0e10cSrcweir                     return nValue1 + (nValue2 - nValue1)*nHue/60.0;
102cdf0e10cSrcweir                 else if( nHue < 180.0 )
103cdf0e10cSrcweir                     return nValue2;
104cdf0e10cSrcweir                 else if( nHue < 240.0 )
105cdf0e10cSrcweir                     return nValue1 + (nValue2 - nValue1)*(240.0 - nHue)/60.0;
106cdf0e10cSrcweir                 else
107cdf0e10cSrcweir                     return nValue1;
108cdf0e10cSrcweir             }
109cdf0e10cSrcweir 
hsl2rgb(double nHue,double nSaturation,double nLuminance)110cdf0e10cSrcweir             RGBColor::RGBTriple hsl2rgb( double nHue, double nSaturation, double nLuminance )
111cdf0e10cSrcweir             {
112cdf0e10cSrcweir                 if( ::basegfx::fTools::equalZero( nSaturation ) )
113cdf0e10cSrcweir                     return RGBColor::RGBTriple(0.0, 0.0, nLuminance );
114cdf0e10cSrcweir 
115cdf0e10cSrcweir                 const double nVal1( getMagic(nLuminance, nSaturation) );
116cdf0e10cSrcweir                 const double nVal2( 2.0*nLuminance - nVal1 );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir                 RGBColor::RGBTriple aRes;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir                 aRes.mnRed = hsl2rgbHelper( nVal2,
121cdf0e10cSrcweir                                             nVal1,
122cdf0e10cSrcweir                                             nHue + 120.0 );
123cdf0e10cSrcweir                 aRes.mnGreen = hsl2rgbHelper( nVal2,
124cdf0e10cSrcweir                                               nVal1,
125cdf0e10cSrcweir                                               nHue );
126cdf0e10cSrcweir                 aRes.mnBlue = hsl2rgbHelper( nVal2,
127cdf0e10cSrcweir                                              nVal1,
128cdf0e10cSrcweir                                              nHue - 120.0 );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir                 return aRes;
131cdf0e10cSrcweir             }
132cdf0e10cSrcweir 
133*d3e5b9daSmseidel             // Truncate range of value to [0,1]
truncateRangeStd(double nVal)134cdf0e10cSrcweir             double truncateRangeStd( double nVal )
135cdf0e10cSrcweir             {
136cdf0e10cSrcweir                 return ::std::max( 0.0,
137cdf0e10cSrcweir                                    ::std::min( 1.0,
138cdf0e10cSrcweir                                                nVal ) );
139cdf0e10cSrcweir             }
140cdf0e10cSrcweir 
141*d3e5b9daSmseidel             // Truncate range of value to [0,360]
truncateRangeHue(double nVal)142cdf0e10cSrcweir             double truncateRangeHue( double nVal )
143cdf0e10cSrcweir             {
144cdf0e10cSrcweir                 return ::std::max( 0.0,
145cdf0e10cSrcweir                                    ::std::min( 360.0,
146cdf0e10cSrcweir                                                nVal ) );
147cdf0e10cSrcweir             }
148cdf0e10cSrcweir 
149*d3e5b9daSmseidel             // convert RGB color to sal_uInt8, truncate range appropriately before
colorToInt(double nCol)150cdf0e10cSrcweir             sal_uInt8 colorToInt( double nCol )
151cdf0e10cSrcweir             {
152cdf0e10cSrcweir                 return static_cast< sal_uInt8 >(
153cdf0e10cSrcweir                     ::basegfx::fround( truncateRangeStd( nCol ) * 255.0 ) );
154cdf0e10cSrcweir             }
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 
158cdf0e10cSrcweir         // HSLColor
159cdf0e10cSrcweir         // ===============================================
160cdf0e10cSrcweir 
HSLTriple()161cdf0e10cSrcweir         HSLColor::HSLTriple::HSLTriple() :
162cdf0e10cSrcweir             mnHue(),
163cdf0e10cSrcweir             mnSaturation(),
164cdf0e10cSrcweir             mnLuminance()
165cdf0e10cSrcweir         {
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir 
HSLTriple(double nHue,double nSaturation,double nLuminance)168cdf0e10cSrcweir         HSLColor::HSLTriple::HSLTriple( double nHue, double nSaturation, double nLuminance ) :
169cdf0e10cSrcweir             mnHue( nHue ),
170cdf0e10cSrcweir             mnSaturation( nSaturation ),
171cdf0e10cSrcweir             mnLuminance( nLuminance )
172cdf0e10cSrcweir         {
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir 
HSLColor()175cdf0e10cSrcweir         HSLColor::HSLColor() :
176cdf0e10cSrcweir             maHSLTriple( 0.0, 0.0, 0.0 ),
177cdf0e10cSrcweir             mnMagicValue( getMagic( maHSLTriple.mnLuminance,
178cdf0e10cSrcweir                                     maHSLTriple.mnSaturation ) )
179cdf0e10cSrcweir         {
180cdf0e10cSrcweir         }
181cdf0e10cSrcweir 
HSLColor(::cppcanvas::Color::IntSRGBA nRGBColor)182cdf0e10cSrcweir         HSLColor::HSLColor( ::cppcanvas::Color::IntSRGBA nRGBColor ) :
183cdf0e10cSrcweir             maHSLTriple( rgb2hsl( ::cppcanvas::getRed( nRGBColor ) / 255.0,
184cdf0e10cSrcweir                                   ::cppcanvas::getGreen( nRGBColor ) / 255.0,
185cdf0e10cSrcweir                                   ::cppcanvas::getBlue( nRGBColor ) / 255.0 ) ),
186cdf0e10cSrcweir             mnMagicValue( getMagic( maHSLTriple.mnLuminance,
187cdf0e10cSrcweir                                     maHSLTriple.mnSaturation ) )
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir 
HSLColor(double nHue,double nSaturation,double nLuminance)191cdf0e10cSrcweir         HSLColor::HSLColor( double nHue, double nSaturation, double nLuminance ) :
192cdf0e10cSrcweir             maHSLTriple( nHue, nSaturation, nLuminance ),
193cdf0e10cSrcweir             mnMagicValue( getMagic( maHSLTriple.mnLuminance,
194cdf0e10cSrcweir                                     maHSLTriple.mnSaturation ) )
195cdf0e10cSrcweir         {
196cdf0e10cSrcweir         }
197cdf0e10cSrcweir 
HSLColor(const RGBColor & rColor)198cdf0e10cSrcweir         HSLColor::HSLColor( const RGBColor& rColor ) :
199cdf0e10cSrcweir             maHSLTriple( rgb2hsl( truncateRangeStd( rColor.getRed() ),
200cdf0e10cSrcweir                                   truncateRangeStd( rColor.getGreen() ),
201cdf0e10cSrcweir                                   truncateRangeStd( rColor.getBlue() ) ) ),
202cdf0e10cSrcweir             mnMagicValue( getMagic( maHSLTriple.mnLuminance,
203cdf0e10cSrcweir                                     maHSLTriple.mnSaturation ) )
204cdf0e10cSrcweir         {
205cdf0e10cSrcweir         }
206cdf0e10cSrcweir 
getHue() const207cdf0e10cSrcweir         double HSLColor::getHue() const
208cdf0e10cSrcweir         {
209cdf0e10cSrcweir             return maHSLTriple.mnHue;
210cdf0e10cSrcweir         }
211cdf0e10cSrcweir 
getSaturation() const212cdf0e10cSrcweir         double HSLColor::getSaturation() const
213cdf0e10cSrcweir         {
214cdf0e10cSrcweir             return maHSLTriple.mnSaturation;
215cdf0e10cSrcweir         }
216cdf0e10cSrcweir 
getLuminance() const217cdf0e10cSrcweir         double HSLColor::getLuminance() const
218cdf0e10cSrcweir         {
219cdf0e10cSrcweir             return maHSLTriple.mnLuminance;
220cdf0e10cSrcweir         }
221cdf0e10cSrcweir 
getRed() const222cdf0e10cSrcweir         double HSLColor::getRed() const
223cdf0e10cSrcweir         {
224cdf0e10cSrcweir             if( ::basegfx::fTools::equalZero( getSaturation() ) )
225cdf0e10cSrcweir                 return getLuminance();
226cdf0e10cSrcweir 
227cdf0e10cSrcweir             return hsl2rgbHelper( 2.0*getLuminance() - mnMagicValue,
228cdf0e10cSrcweir                                   mnMagicValue,
229cdf0e10cSrcweir                                   getHue() + 120.0 );
230cdf0e10cSrcweir         }
231cdf0e10cSrcweir 
getGreen() const232cdf0e10cSrcweir         double HSLColor::getGreen() const
233cdf0e10cSrcweir         {
234cdf0e10cSrcweir             if( ::basegfx::fTools::equalZero( getSaturation() ) )
235cdf0e10cSrcweir                 return getLuminance();
236cdf0e10cSrcweir 
237cdf0e10cSrcweir             return hsl2rgbHelper( 2.0*getLuminance() - mnMagicValue,
238cdf0e10cSrcweir                                   mnMagicValue,
239cdf0e10cSrcweir                                   getHue() );
240cdf0e10cSrcweir         }
241cdf0e10cSrcweir 
getBlue() const242cdf0e10cSrcweir         double HSLColor::getBlue() const
243cdf0e10cSrcweir         {
244cdf0e10cSrcweir             if( ::basegfx::fTools::equalZero( getSaturation() ) )
245cdf0e10cSrcweir                 return getLuminance();
246cdf0e10cSrcweir 
247cdf0e10cSrcweir             return hsl2rgbHelper( 2.0*getLuminance() - mnMagicValue,
248cdf0e10cSrcweir                                   mnMagicValue,
249cdf0e10cSrcweir                                   getHue() - 120.0 );
250cdf0e10cSrcweir         }
251cdf0e10cSrcweir 
getRGBColor() const252cdf0e10cSrcweir         RGBColor HSLColor::getRGBColor() const
253cdf0e10cSrcweir         {
254cdf0e10cSrcweir             RGBColor::RGBTriple aColor( hsl2rgb( getHue(),
255cdf0e10cSrcweir                                                  getSaturation(),
256cdf0e10cSrcweir                                                  getLuminance() ) );
257cdf0e10cSrcweir             return RGBColor( aColor.mnRed, aColor.mnGreen, aColor.mnBlue );
258cdf0e10cSrcweir         }
259cdf0e10cSrcweir 
RGBColor(const RGBColor & rLHS)260cdf0e10cSrcweir         RGBColor::RGBColor(const RGBColor& rLHS)
261cdf0e10cSrcweir         {
262cdf0e10cSrcweir             maRGBTriple.mnRed = rLHS.getRed();
263cdf0e10cSrcweir             maRGBTriple.mnGreen = rLHS.getGreen();
264cdf0e10cSrcweir             maRGBTriple.mnBlue = rLHS.getBlue();
265cdf0e10cSrcweir         }
266cdf0e10cSrcweir 
operator =(const RGBColor & rLHS)267cdf0e10cSrcweir         RGBColor& RGBColor::operator=( const RGBColor& rLHS ){
268cdf0e10cSrcweir 
269cdf0e10cSrcweir             maRGBTriple.mnRed = rLHS.getRed();
270cdf0e10cSrcweir             maRGBTriple.mnGreen = rLHS.getGreen();
271cdf0e10cSrcweir             maRGBTriple.mnBlue = rLHS.getBlue();
272cdf0e10cSrcweir             return *this;
273cdf0e10cSrcweir         }
274cdf0e10cSrcweir 
operator +(const HSLColor & rLHS,const HSLColor & rRHS)275cdf0e10cSrcweir         HSLColor operator+( const HSLColor& rLHS, const HSLColor& rRHS )
276cdf0e10cSrcweir         {
277cdf0e10cSrcweir             return HSLColor( rLHS.getHue() + rRHS.getHue(),
278cdf0e10cSrcweir                              rLHS.getSaturation() + rRHS.getSaturation(),
279cdf0e10cSrcweir                              rLHS.getLuminance() + rRHS.getLuminance() );
280cdf0e10cSrcweir         }
281cdf0e10cSrcweir 
operator *(const HSLColor & rLHS,const HSLColor & rRHS)282cdf0e10cSrcweir         HSLColor operator*( const HSLColor& rLHS, const HSLColor& rRHS )
283cdf0e10cSrcweir         {
284cdf0e10cSrcweir             return HSLColor( rLHS.getHue() * rRHS.getHue(),
285cdf0e10cSrcweir                              rLHS.getSaturation() * rRHS.getSaturation(),
286cdf0e10cSrcweir                              rLHS.getLuminance() * rRHS.getLuminance() );
287cdf0e10cSrcweir         }
288cdf0e10cSrcweir 
operator *(double nFactor,const HSLColor & rRHS)289cdf0e10cSrcweir         HSLColor operator*( double nFactor, const HSLColor& rRHS )
290cdf0e10cSrcweir         {
291cdf0e10cSrcweir             return HSLColor( nFactor * rRHS.getHue(),
292cdf0e10cSrcweir                              nFactor * rRHS.getSaturation(),
293cdf0e10cSrcweir                              nFactor * rRHS.getLuminance() );
294cdf0e10cSrcweir         }
295cdf0e10cSrcweir 
interpolate(const HSLColor & rFrom,const HSLColor & rTo,double t,bool bCCW)296cdf0e10cSrcweir         HSLColor interpolate( const HSLColor& rFrom, const HSLColor& rTo, double t, bool bCCW )
297cdf0e10cSrcweir         {
298cdf0e10cSrcweir             const double nFromHue( rFrom.getHue() );
299cdf0e10cSrcweir             const double nToHue  ( rTo.getHue()   );
300cdf0e10cSrcweir 
301cdf0e10cSrcweir             double nHue=0.0;
302cdf0e10cSrcweir 
303cdf0e10cSrcweir             if( nFromHue <= nToHue && !bCCW )
304cdf0e10cSrcweir             {
305cdf0e10cSrcweir                 // interpolate hue clockwise. That is, hue starts at
306cdf0e10cSrcweir                 // high values and ends at low ones. Therefore, we
307cdf0e10cSrcweir                 // must 'cross' the 360 degrees and start at low
308cdf0e10cSrcweir                 // values again (imagine the hues to lie on the
309cdf0e10cSrcweir                 // circle, where values above 360 degrees are mapped
310cdf0e10cSrcweir                 // back to [0,360)).
311cdf0e10cSrcweir                 nHue = (1.0-t)*(nFromHue + 360.0) + t*nToHue;
312cdf0e10cSrcweir             }
313cdf0e10cSrcweir             else if( nFromHue > nToHue && bCCW )
314cdf0e10cSrcweir             {
315cdf0e10cSrcweir                 // interpolate hue counter-clockwise. That is, hue
316cdf0e10cSrcweir                 // starts at high values and ends at low
317cdf0e10cSrcweir                 // ones. Therefore, we must 'cross' the 360 degrees
318cdf0e10cSrcweir                 // and start at low values again (imagine the hues to
319cdf0e10cSrcweir                 // lie on the circle, where values above 360 degrees
320cdf0e10cSrcweir                 // are mapped back to [0,360)).
321cdf0e10cSrcweir                 nHue = (1.0-t)*nFromHue + t*(nToHue + 360.0);
322cdf0e10cSrcweir             }
323cdf0e10cSrcweir             else
324cdf0e10cSrcweir             {
325cdf0e10cSrcweir                 // interpolate hue counter-clockwise. That is, hue
326cdf0e10cSrcweir                 // starts at low values and ends at high ones (imagine
327cdf0e10cSrcweir                 // the hue value as degrees on a circle, with
328cdf0e10cSrcweir                 // increasing values going counter-clockwise)
329cdf0e10cSrcweir                 nHue = (1.0-t)*nFromHue + t*nToHue;
330cdf0e10cSrcweir             }
331cdf0e10cSrcweir 
332cdf0e10cSrcweir             return HSLColor( nHue,
333cdf0e10cSrcweir                              (1.0-t)*rFrom.getSaturation() + t*rTo.getSaturation(),
334cdf0e10cSrcweir                              (1.0-t)*rFrom.getLuminance() + t*rTo.getLuminance() );
335cdf0e10cSrcweir         }
336cdf0e10cSrcweir 
337cdf0e10cSrcweir 
338cdf0e10cSrcweir         // RGBColor
339cdf0e10cSrcweir         // ===============================================
340cdf0e10cSrcweir 
341cdf0e10cSrcweir 
RGBTriple()342cdf0e10cSrcweir         RGBColor::RGBTriple::RGBTriple() :
343cdf0e10cSrcweir             mnRed(),
344cdf0e10cSrcweir             mnGreen(),
345cdf0e10cSrcweir             mnBlue()
346cdf0e10cSrcweir         {
347cdf0e10cSrcweir         }
348cdf0e10cSrcweir 
RGBTriple(double nRed,double nGreen,double nBlue)349cdf0e10cSrcweir         RGBColor::RGBTriple::RGBTriple( double nRed, double nGreen, double nBlue ) :
350cdf0e10cSrcweir             mnRed( nRed ),
351cdf0e10cSrcweir             mnGreen( nGreen ),
352cdf0e10cSrcweir             mnBlue( nBlue )
353cdf0e10cSrcweir         {
354cdf0e10cSrcweir         }
355cdf0e10cSrcweir 
RGBColor()356cdf0e10cSrcweir         RGBColor::RGBColor() :
357cdf0e10cSrcweir             maRGBTriple( 0.0, 0.0, 0.0 )
358cdf0e10cSrcweir         {
359cdf0e10cSrcweir         }
360cdf0e10cSrcweir 
RGBColor(::cppcanvas::Color::IntSRGBA nRGBColor)361cdf0e10cSrcweir         RGBColor::RGBColor( ::cppcanvas::Color::IntSRGBA nRGBColor ) :
362cdf0e10cSrcweir             maRGBTriple( ::cppcanvas::getRed( nRGBColor ) / 255.0,
363cdf0e10cSrcweir                          ::cppcanvas::getGreen( nRGBColor ) / 255.0,
364cdf0e10cSrcweir                          ::cppcanvas::getBlue( nRGBColor ) / 255.0 )
365cdf0e10cSrcweir         {
366cdf0e10cSrcweir         }
367cdf0e10cSrcweir 
RGBColor(double nRed,double nGreen,double nBlue)368cdf0e10cSrcweir         RGBColor::RGBColor( double nRed, double nGreen, double nBlue ) :
369cdf0e10cSrcweir             maRGBTriple( nRed, nGreen, nBlue )
370cdf0e10cSrcweir         {
371cdf0e10cSrcweir         }
372cdf0e10cSrcweir 
RGBColor(const HSLColor & rColor)373cdf0e10cSrcweir         RGBColor::RGBColor( const HSLColor& rColor ) :
374cdf0e10cSrcweir             maRGBTriple( hsl2rgb( truncateRangeHue( rColor.getHue() ),
375cdf0e10cSrcweir                                   truncateRangeStd( rColor.getSaturation() ),
376cdf0e10cSrcweir                                   truncateRangeStd( rColor.getLuminance() ) ) )
377cdf0e10cSrcweir         {
378cdf0e10cSrcweir         }
379cdf0e10cSrcweir 
getHue() const380cdf0e10cSrcweir         double RGBColor::getHue() const
381cdf0e10cSrcweir         {
382cdf0e10cSrcweir             return rgb2hsl( getRed(),
383cdf0e10cSrcweir                             getGreen(),
384cdf0e10cSrcweir                             getBlue() ).mnHue;
385cdf0e10cSrcweir         }
386cdf0e10cSrcweir 
getSaturation() const387cdf0e10cSrcweir         double RGBColor::getSaturation() const
388cdf0e10cSrcweir         {
389cdf0e10cSrcweir             return rgb2hsl( getRed(),
390cdf0e10cSrcweir                             getGreen(),
391cdf0e10cSrcweir                             getBlue() ).mnSaturation;
392cdf0e10cSrcweir         }
393cdf0e10cSrcweir 
getLuminance() const394cdf0e10cSrcweir         double RGBColor::getLuminance() const
395cdf0e10cSrcweir         {
396cdf0e10cSrcweir             return rgb2hsl( getRed(),
397cdf0e10cSrcweir                             getGreen(),
398cdf0e10cSrcweir                             getBlue() ).mnLuminance;
399cdf0e10cSrcweir         }
400cdf0e10cSrcweir 
getRed() const401cdf0e10cSrcweir         double RGBColor::getRed() const
402cdf0e10cSrcweir         {
403cdf0e10cSrcweir             return maRGBTriple.mnRed;
404cdf0e10cSrcweir         }
405cdf0e10cSrcweir 
getGreen() const406cdf0e10cSrcweir         double RGBColor::getGreen() const
407cdf0e10cSrcweir         {
408cdf0e10cSrcweir             return maRGBTriple.mnGreen;
409cdf0e10cSrcweir         }
410cdf0e10cSrcweir 
getBlue() const411cdf0e10cSrcweir         double RGBColor::getBlue() const
412cdf0e10cSrcweir         {
413cdf0e10cSrcweir             return maRGBTriple.mnBlue;
414cdf0e10cSrcweir         }
415cdf0e10cSrcweir 
getHSLColor() const416cdf0e10cSrcweir         HSLColor RGBColor::getHSLColor() const
417cdf0e10cSrcweir         {
418cdf0e10cSrcweir             HSLColor::HSLTriple aColor( rgb2hsl( getRed(),
419cdf0e10cSrcweir                                                  getGreen(),
420cdf0e10cSrcweir                                                  getBlue() ) );
421cdf0e10cSrcweir             return HSLColor( aColor.mnHue, aColor.mnSaturation, aColor.mnLuminance );
422cdf0e10cSrcweir         }
423cdf0e10cSrcweir 
getIntegerColor() const424cdf0e10cSrcweir         ::cppcanvas::Color::IntSRGBA RGBColor::getIntegerColor() const
425cdf0e10cSrcweir         {
426cdf0e10cSrcweir             return ::cppcanvas::makeColor( colorToInt( getRed() ),
427cdf0e10cSrcweir                                            colorToInt( getGreen() ),
428cdf0e10cSrcweir                                            colorToInt( getBlue() ),
429cdf0e10cSrcweir                                            255 );
430cdf0e10cSrcweir         }
431cdf0e10cSrcweir 
operator +(const RGBColor & rLHS,const RGBColor & rRHS)432cdf0e10cSrcweir         RGBColor operator+( const RGBColor& rLHS, const RGBColor& rRHS )
433cdf0e10cSrcweir         {
434cdf0e10cSrcweir             return RGBColor( rLHS.getRed() + rRHS.getRed(),
435cdf0e10cSrcweir                              rLHS.getGreen() + rRHS.getGreen(),
436cdf0e10cSrcweir                              rLHS.getBlue() + rRHS.getBlue() );
437cdf0e10cSrcweir         }
438cdf0e10cSrcweir 
operator *(const RGBColor & rLHS,const RGBColor & rRHS)439cdf0e10cSrcweir         RGBColor operator*( const RGBColor& rLHS, const RGBColor& rRHS )
440cdf0e10cSrcweir         {
441cdf0e10cSrcweir             return RGBColor( rLHS.getRed() * rRHS.getRed(),
442cdf0e10cSrcweir                              rLHS.getGreen() * rRHS.getGreen(),
443cdf0e10cSrcweir                              rLHS.getBlue() * rRHS.getBlue() );
444cdf0e10cSrcweir         }
445cdf0e10cSrcweir 
operator *(double nFactor,const RGBColor & rRHS)446cdf0e10cSrcweir         RGBColor operator*( double nFactor, const RGBColor& rRHS )
447cdf0e10cSrcweir         {
448cdf0e10cSrcweir             return RGBColor( nFactor * rRHS.getRed(),
449cdf0e10cSrcweir                              nFactor * rRHS.getGreen(),
450cdf0e10cSrcweir                              nFactor * rRHS.getBlue() );
451cdf0e10cSrcweir         }
452cdf0e10cSrcweir 
interpolate(const RGBColor & rFrom,const RGBColor & rTo,double t)453cdf0e10cSrcweir         RGBColor interpolate( const RGBColor& rFrom, const RGBColor& rTo, double t )
454cdf0e10cSrcweir         {
455cdf0e10cSrcweir             return RGBColor( (1.0-t)*rFrom.getRed() + t*rTo.getRed(),
456cdf0e10cSrcweir                              (1.0-t)*rFrom.getGreen() + t*rTo.getGreen(),
457cdf0e10cSrcweir                              (1.0-t)*rFrom.getBlue() + t*rTo.getBlue() );
458cdf0e10cSrcweir         }
459cdf0e10cSrcweir     }
460cdf0e10cSrcweir }
461*d3e5b9daSmseidel 
462*d3e5b9daSmseidel /* vim: set noet sw=4 ts=4: */
463