merged tag ooo/OOO330_m14
[LibreOffice.git] / basegfx / source / color / bcolortools.cxx
blob543097de3d774e7c10119f0abf5961262509f474
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_basegfx.hxx"
31 #include <basegfx/numeric/ftools.hxx>
32 #include <basegfx/color/bcolor.hxx>
33 #include <basegfx/color/bcolortools.hxx>
35 //////////////////////////////////////////////////////////////////////////////
37 namespace basegfx { namespace tools
39 BColor rgb2hsl(const BColor& rRGBColor)
41 const double r=rRGBColor.getRed(), g=rRGBColor.getGreen(), b=rRGBColor.getBlue();
42 const double minVal = ::std::min( ::std::min( r, g ), b );
43 const double maxVal = ::std::max( ::std::max( r, g ), b );
44 const double d = maxVal - minVal;
46 double h=0, s=0, l=0;
48 l = (maxVal + minVal) / 2.0;
50 if( ::basegfx::fTools::equalZero(d) )
52 s = h = 0; // hue undefined (achromatic case)
54 else
56 s = l > 0.5 ? d/(2.0-maxVal-minVal) :
57 d/(maxVal + minVal);
59 if( r == maxVal )
60 h = (g - b)/d;
61 else if( g == maxVal )
62 h = 2.0 + (b - r)/d;
63 else
64 h = 4.0 + (r - g)/d;
66 h *= 60.0;
68 if( h < 0.0 )
69 h += 360.0;
72 return BColor(h,s,l);
75 static inline double hsl2rgbHelper( double nValue1, double nValue2, double nHue )
77 // clamp hue to [0,360]
78 nHue = fmod( nHue, 360.0 );
80 // cope with wrap-arounds
81 if( nHue < 0.0 )
82 nHue += 360.0;
84 if( nHue < 60.0 )
85 return nValue1 + (nValue2 - nValue1)*nHue/60.0;
86 else if( nHue < 180.0 )
87 return nValue2;
88 else if( nHue < 240.0 )
89 return nValue1 + (nValue2 - nValue1)*(240.0 - nHue)/60.0;
90 else
91 return nValue1;
94 BColor hsl2rgb(const BColor& rHSLColor)
96 const double h=rHSLColor.getRed(), s=rHSLColor.getGreen(), l=rHSLColor.getBlue();
98 if( fTools::equalZero(s) )
99 return BColor(l, l, l ); // achromatic case
101 const double nVal1( l <= 0.5 ? l*(1.0 + s) : l + s - l*s );
102 const double nVal2( 2.0*l - nVal1 );
104 return BColor(
105 hsl2rgbHelper(nVal2,
106 nVal1,
107 h + 120.0),
108 hsl2rgbHelper(nVal2,
109 nVal1,
111 hsl2rgbHelper(nVal2,
112 nVal1,
113 h - 120.0) );
116 BColor rgb2hsv(const BColor& rRGBColor)
118 const double r=rRGBColor.getRed(), g=rRGBColor.getGreen(), b=rRGBColor.getBlue();
119 const double maxVal = std::max(std::max(r,g),b);
120 const double minVal = std::min(std::min(r,g),b);
121 const double delta = maxVal-minVal;
123 double h=0, s=0, v=0;
125 v = maxVal;
126 if( fTools::equalZero(v) )
127 s = 0;
128 else
129 s = delta / v;
131 if( !fTools::equalZero(s) )
133 if( maxVal == r )
135 h = (g - b) / delta;
137 else if( maxVal == g )
139 h = 2.0 + (b - r) / delta;
141 else
143 h = 4.0 + (r - g) / delta;
146 h *= 60.0;
148 if( h < 0 )
149 h += 360;
152 return BColor(h,s,v);
155 BColor hsv2rgb(const BColor& rHSVColor)
157 double h=rHSVColor.getRed();
158 const double s=rHSVColor.getGreen(), v=rHSVColor.getBlue();
160 if( fTools::equalZero(s) )
162 // achromatic case: no hue.
163 return BColor(v,v,v);
165 else
167 if( fTools::equal(h,360) )
168 h = 0; // 360 degrees is equivalent to 0 degrees
170 h /= 60.0;
171 const sal_Int32 intval = static_cast< sal_Int32 >( h );
172 const double f = h - intval;
173 const double p = v*(1.0-s);
174 const double q = v*(1.0-(s*f));
175 const double t = v*(1.0-(s*(1.0-f)));
177 /* which hue area? */
178 switch( intval )
180 case 0:
181 return BColor(v,t,p);
183 case 1:
184 return BColor(q,v,p);
186 case 2:
187 return BColor(p,v,t);
189 case 3:
190 return BColor(p,q,v);
192 case 4:
193 return BColor(t,p,v);
195 case 5:
196 return BColor(v,p,q);
198 default:
199 // hue overflow
200 return BColor();
205 BColor rgb2yiq(const BColor& rRGBColor)
207 // from Foley, van Dam, Computer Graphics
208 const double r=rRGBColor.getRed(), g=rRGBColor.getGreen(), b=rRGBColor.getBlue();
209 return BColor(
210 0.299*r + 0.587*g + 0.114*b,
211 0.596*r - 0.274*g - 0.322*b,
212 0.211*r - 0.522*g + 0.311*b);
215 BColor yiq2rgb(const BColor& rYIQColor)
217 // from Foley, van Dam, Computer Graphics
218 const double y=rYIQColor.getRed(), i=rYIQColor.getGreen(), q=rYIQColor.getBlue();
219 return BColor(
220 y + 0.956*i + 0.623*q,
221 y - 0.272*i - 0.648*q,
222 y - 1.105*i + 1.705*q );
225 BColor ciexyz2rgb( const BColor& rXYZColor )
227 // from Poynton color faq, and SMPTE RP 177-1993, Derivation
228 // of Basic Television Color Equations
229 const double x=rXYZColor.getRed(), y=rXYZColor.getGreen(), z=rXYZColor.getBlue();
230 return BColor(
231 3.240479*x - 1.53715*y - 0.498535*z,
232 -0.969256*x + 1.875991*y + 0.041556*z,
233 0.055648*x - 0.204043*y + 1.057311*z );
236 BColor rgb2ciexyz( const BColor& rRGBColor )
238 // from Poynton color faq, and SMPTE RP 177-1993, Derivation
239 // of Basic Television Color Equations
240 const double r=rRGBColor.getRed(), g=rRGBColor.getGreen(), b=rRGBColor.getBlue();
241 return BColor(
242 0.412453*r + 0.35758*g + 0.180423*b,
243 0.212671*r + 0.71516*g + 0.072169*b,
244 0.019334*r + 0.119193*g + 0.950227*b);
247 BColor rgb2ypbpr(const BColor& rRGBColor)
249 const double r=rRGBColor.getRed(), g=rRGBColor.getGreen(), b=rRGBColor.getBlue();
250 return BColor(
251 0.299*r + 0.587*g + 0.114*b,
252 -0.168736*r - 0.331264*g + 0.5*b,
253 0.5*r - 0.418688*g - 0.081312*b);
256 BColor ypbpr2rgb(const BColor& rYPbPrColor)
258 const double y=rYPbPrColor.getRed(), pb=rYPbPrColor.getGreen(), pr=rYPbPrColor.getBlue();
259 return BColor(
260 1.*y + 0.*pb + 1.402*pr,
261 1.*y - 0.344136*pb - 0.714136*pr,
262 1.*y + 1.772*pb + 0.*pr);
265 } } // end of namespace basegfx
267 //////////////////////////////////////////////////////////////////////////////
268 // eof