25#include <rtl/math.hxx>
38 double getMagic(
double nLuminance,
double nSaturation )
40 if( nLuminance <= 0.5 )
41 return nLuminance*(1.0 + nSaturation);
43 return nLuminance + nSaturation - nLuminance*nSaturation;
46 HSLColor::HSLTriple
rgb2hsl(
double nRed,
double nGreen,
double nBlue )
49 HSLColor::HSLTriple aRes;
51 const double nMax( ::std::max(nRed,::std::max(nGreen, nBlue)) );
52 const double nMin( ::std::min(nRed,::std::min(nGreen, nBlue)) );
54 const double nDelta( nMax - nMin );
56 aRes.mnLuminance = (nMax + nMin) / 2.0;
58 if( ::basegfx::fTools::equalZero( nDelta ) )
60 aRes.mnSaturation = 0.0;
67 aRes.mnSaturation = aRes.mnLuminance > 0.5 ?
68 nDelta/(2.0-nMax-nMin) :
71 if( rtl::math::approxEqual(nRed, nMax) )
72 aRes.mnHue = (nGreen - nBlue)/nDelta;
73 else if( rtl::math::approxEqual(nGreen, nMax) )
74 aRes.mnHue = 2.0 + (nBlue - nRed)/nDelta;
75 else if( rtl::math::approxEqual(nBlue, nMax) )
76 aRes.mnHue = 4.0 + (nRed - nGreen)/nDelta;
80 if( aRes.mnHue < 0.0 )
87 double hsl2rgbHelper(
double nValue1,
double nValue2,
double nHue )
90 nHue = fmod( nHue, 360.0 );
97 return nValue1 + (nValue2 - nValue1)*nHue/60.0;
98 else if( nHue < 180.0 )
100 else if( nHue < 240.0 )
101 return nValue1 + (nValue2 - nValue1)*(240.0 - nHue)/60.0;
106 RGBColor::RGBTriple
hsl2rgb(
double nHue,
double nSaturation,
double nLuminance )
108 if( ::basegfx::fTools::equalZero( nSaturation ) )
109 return RGBColor::RGBTriple(0.0, 0.0, nLuminance );
111 const double nVal1( getMagic(nLuminance, nSaturation) );
112 const double nVal2( 2.0*nLuminance - nVal1 );
114 RGBColor::RGBTriple aRes;
116 aRes.mnRed = hsl2rgbHelper( nVal2,
119 aRes.mnGreen = hsl2rgbHelper( nVal2,
122 aRes.mnBlue = hsl2rgbHelper( nVal2,
130 double truncateRangeStd(
double nVal )
132 return ::std::max( 0.0,
138 double truncateRangeHue(
double nVal )
140 return ::std::max( 0.0,
149 ::basegfx::fround( truncateRangeStd( nCol ) * 255.0 ) );
166 mnSaturation( nSaturation ),
167 mnLuminance( nLuminance )
177 maHSLTriple( nHue, nSaturation, nLuminance )
183 truncateRangeStd( rColor.
getGreen() ),
184 truncateRangeStd( rColor.
getBlue() ) ) )
198 return !( rLHS == rRHS );
217 const double nFromHue( rFrom.
getHue() );
218 const double nToHue ( rTo.
getHue() );
222 if( nFromHue <= nToHue && !bCCW )
230 nHue = (1.0-
t)*(nFromHue + 360.0) +
t*nToHue;
232 else if( nFromHue > nToHue && bCCW )
240 nHue = (1.0-
t)*nFromHue +
t*(nToHue + 360.0);
248 nHue = (1.0-
t)*nFromHue +
t*nToHue;
287 maRGBTriple( nRed, nGreen, nBlue )
292 maRGBTriple(
hsl2rgb( truncateRangeHue( rColor.getHue() ),
293 truncateRangeStd( rColor.getSaturation() ),
294 truncateRangeStd( rColor.getLuminance() ) ) )
301 return ::cppcanvas::makeColor( colorToInt(
getRed() ),
316 return !( rLHS == rRHS );
double getHue() const
Hue of the color.
double getLuminance() const
Luminance of the color.
double getSaturation() const
Saturation of the color.
::cppcanvas::IntSRGBA getIntegerColor() const
Create an integer sRGBA color.
double getRed() const
Get the RGB red value.
double getGreen() const
Get the RGB green value.
double getBlue() const
Get the RGB blue value.
BColor rgb2hsl(const BColor &rRGBColor)
BColor hsl2rgb(const BColor &rHSLColor)
sal_uInt8 getRed(IntSRGBA nCol)
sal_uInt8 getBlue(IntSRGBA nCol)
sal_uInt8 getGreen(IntSRGBA nCol)
bool operator==(const HSLColor &rLHS, const HSLColor &rRHS)
HSLColor operator+(const HSLColor &rLHS, const HSLColor &rRHS)
bool operator!=(const HSLColor &rLHS, const HSLColor &rRHS)
HSLColor operator*(double nFactor, const HSLColor &rRHS)
HSLColor interpolate(const HSLColor &rFrom, const HSLColor &rTo, double t, bool bCCW)
HSL color linear interpolator.