fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / basegfx / source / color / bcolortools.cxx
blob838a10d1a116ff1c5dccd1c7f6ff8d2ab0be1f04
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <basegfx/numeric/ftools.hxx>
21 #include <basegfx/color/bcolor.hxx>
22 #include <basegfx/color/bcolortools.hxx>
24 //////////////////////////////////////////////////////////////////////////////
26 namespace basegfx { namespace tools
28 BColor rgb2hsl(const BColor& rRGBColor)
30 const double r=rRGBColor.getRed(), g=rRGBColor.getGreen(), b=rRGBColor.getBlue();
31 const double minVal = ::std::min( ::std::min( r, g ), b );
32 const double maxVal = ::std::max( ::std::max( r, g ), b );
33 const double d = maxVal - minVal;
35 double h=0, s=0, l=0;
37 l = (maxVal + minVal) / 2.0;
39 if( ::basegfx::fTools::equalZero(d) )
41 s = h = 0; // hue undefined (achromatic case)
43 else
45 s = l > 0.5 ? d/(2.0-maxVal-minVal) :
46 d/(maxVal + minVal);
48 if( r == maxVal )
49 h = (g - b)/d;
50 else if( g == maxVal )
51 h = 2.0 + (b - r)/d;
52 else
53 h = 4.0 + (r - g)/d;
55 h *= 60.0;
57 if( h < 0.0 )
58 h += 360.0;
61 return BColor(h,s,l);
64 static inline double hsl2rgbHelper( double nValue1, double nValue2, double nHue )
66 // clamp hue to [0,360]
67 nHue = fmod( nHue, 360.0 );
69 // cope with wrap-arounds
70 if( nHue < 0.0 )
71 nHue += 360.0;
73 if( nHue < 60.0 )
74 return nValue1 + (nValue2 - nValue1)*nHue/60.0;
75 else if( nHue < 180.0 )
76 return nValue2;
77 else if( nHue < 240.0 )
78 return nValue1 + (nValue2 - nValue1)*(240.0 - nHue)/60.0;
79 else
80 return nValue1;
83 BColor hsl2rgb(const BColor& rHSLColor)
85 const double h=rHSLColor.getRed(), s=rHSLColor.getGreen(), l=rHSLColor.getBlue();
87 if( fTools::equalZero(s) )
88 return BColor(l, l, l ); // achromatic case
90 const double nVal1( l <= 0.5 ? l*(1.0 + s) : l + s - l*s );
91 const double nVal2( 2.0*l - nVal1 );
93 return BColor(
94 hsl2rgbHelper(nVal2,
95 nVal1,
96 h + 120.0),
97 hsl2rgbHelper(nVal2,
98 nVal1,
99 h),
100 hsl2rgbHelper(nVal2,
101 nVal1,
102 h - 120.0) );
105 BColor rgb2hsv(const BColor& rRGBColor)
107 const double r=rRGBColor.getRed(), g=rRGBColor.getGreen(), b=rRGBColor.getBlue();
108 const double maxVal = std::max(std::max(r,g),b);
109 const double minVal = std::min(std::min(r,g),b);
110 const double delta = maxVal-minVal;
112 double h=0, s=0, v=0;
114 v = maxVal;
115 if( fTools::equalZero(v) )
116 s = 0;
117 else
118 s = delta / v;
120 if( !fTools::equalZero(s) )
122 if( maxVal == r )
124 h = (g - b) / delta;
126 else if( maxVal == g )
128 h = 2.0 + (b - r) / delta;
130 else
132 h = 4.0 + (r - g) / delta;
135 h *= 60.0;
137 if( h < 0 )
138 h += 360;
141 return BColor(h,s,v);
144 BColor hsv2rgb(const BColor& rHSVColor)
146 double h=rHSVColor.getRed();
147 const double s=rHSVColor.getGreen(), v=rHSVColor.getBlue();
149 if( fTools::equalZero(s) )
151 // achromatic case: no hue.
152 return BColor(v,v,v);
154 else
156 if( fTools::equal(h,360) )
157 h = 0; // 360 degrees is equivalent to 0 degrees
159 h /= 60.0;
160 const sal_Int32 intval = static_cast< sal_Int32 >( h );
161 const double f = h - intval;
162 const double p = v*(1.0-s);
163 const double q = v*(1.0-(s*f));
164 const double t = v*(1.0-(s*(1.0-f)));
166 /* which hue area? */
167 switch( intval )
169 case 0:
170 return BColor(v,t,p);
172 case 1:
173 return BColor(q,v,p);
175 case 2:
176 return BColor(p,v,t);
178 case 3:
179 return BColor(p,q,v);
181 case 4:
182 return BColor(t,p,v);
184 case 5:
185 return BColor(v,p,q);
187 default:
188 // hue overflow
189 return BColor();
194 BColor rgb2ciexyz( const BColor& rRGBColor )
196 // from Poynton color faq, and SMPTE RP 177-1993, Derivation
197 // of Basic Television Color Equations
198 const double r=rRGBColor.getRed(), g=rRGBColor.getGreen(), b=rRGBColor.getBlue();
199 return BColor(
200 0.412453*r + 0.35758*g + 0.180423*b,
201 0.212671*r + 0.71516*g + 0.072169*b,
202 0.019334*r + 0.119193*g + 0.950227*b);
205 } } // end of namespace basegfx
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */