Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / basegfx / source / color / bcolormodifier.cxx
blobc1f70267769ccdbcdcfcb7352e45bbc60ca9782b
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/color/bcolormodifier.hxx>
22 //////////////////////////////////////////////////////////////////////////////
24 namespace basegfx
26 BColorModifier::~BColorModifier()
29 } // end of namespace basegfx
31 //////////////////////////////////////////////////////////////////////////////
33 namespace basegfx
35 BColorModifier_gray::~BColorModifier_gray()
39 bool BColorModifier_gray::operator==(const BColorModifier& rCompare) const
41 return 0 != dynamic_cast< const BColorModifier_gray* >(&rCompare);
44 ::basegfx::BColor BColorModifier_gray::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
46 const double fLuminance(aSourceColor.luminance());
48 return ::basegfx::BColor(fLuminance, fLuminance, fLuminance);
50 } // end of namespace basegfx
52 //////////////////////////////////////////////////////////////////////////////
54 namespace basegfx
56 BColorModifier_invert::~BColorModifier_invert()
60 bool BColorModifier_invert::operator==(const BColorModifier& rCompare) const
62 return 0 != dynamic_cast< const BColorModifier_invert* >(&rCompare);
65 ::basegfx::BColor BColorModifier_invert::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
67 return ::basegfx::BColor(1.0 - aSourceColor.getRed(), 1.0 - aSourceColor.getGreen(), 1.0 - aSourceColor.getBlue());
69 } // end of namespace basegfx
71 //////////////////////////////////////////////////////////////////////////////
73 namespace basegfx
75 BColorModifier_luminance_to_alpha::~BColorModifier_luminance_to_alpha()
79 bool BColorModifier_luminance_to_alpha::operator==(const BColorModifier& rCompare) const
81 return 0 != dynamic_cast< const BColorModifier_luminance_to_alpha* >(&rCompare);
84 ::basegfx::BColor BColorModifier_luminance_to_alpha::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
86 const double fAlpha(1.0 - ((aSourceColor.getRed() * 0.2125) + (aSourceColor.getGreen() * 0.7154) + (aSourceColor.getBlue() * 0.0721)));
88 return ::basegfx::BColor(fAlpha, fAlpha, fAlpha);
90 } // end of namespace basegfx
92 //////////////////////////////////////////////////////////////////////////////
94 namespace basegfx
96 BColorModifier_replace::~BColorModifier_replace()
100 bool BColorModifier_replace::operator==(const BColorModifier& rCompare) const
102 const BColorModifier_replace* pCompare = dynamic_cast< const BColorModifier_replace* >(&rCompare);
104 if(!pCompare)
106 return false;
109 return getBColor() == pCompare->getBColor();
112 ::basegfx::BColor BColorModifier_replace::getModifiedColor(const ::basegfx::BColor& /*aSourceColor*/) const
114 return maBColor;
116 } // end of namespace basegfx
118 //////////////////////////////////////////////////////////////////////////////
120 namespace basegfx
122 BColorModifier_interpolate::~BColorModifier_interpolate()
126 bool BColorModifier_interpolate::operator==(const BColorModifier& rCompare) const
128 const BColorModifier_interpolate* pCompare = dynamic_cast< const BColorModifier_interpolate* >(&rCompare);
130 if(!pCompare)
132 return false;
135 return getBColor() == pCompare->getBColor() && getValue() == pCompare->getValue();
138 ::basegfx::BColor BColorModifier_interpolate::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
140 return interpolate(maBColor, aSourceColor, mfValue);
142 } // end of namespace basegfx
144 //////////////////////////////////////////////////////////////////////////////
146 namespace basegfx
148 BColorModifier_black_and_white::~BColorModifier_black_and_white()
152 bool BColorModifier_black_and_white::operator==(const BColorModifier& rCompare) const
154 const BColorModifier_black_and_white* pCompare = dynamic_cast< const BColorModifier_black_and_white* >(&rCompare);
156 if(!pCompare)
158 return false;
161 return getValue() == pCompare->getValue();
164 ::basegfx::BColor BColorModifier_black_and_white::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
166 const double fLuminance(aSourceColor.luminance());
168 if(fLuminance < mfValue)
170 return ::basegfx::BColor::getEmptyBColor();
172 else
174 return ::basegfx::BColor(1.0, 1.0, 1.0);
177 } // end of namespace basegfx
179 //////////////////////////////////////////////////////////////////////////////
181 namespace basegfx
183 BColorModifier_gamma::BColorModifier_gamma(double fValue)
184 : BColorModifier(),
185 mfValue(fValue),
186 mfInvValue(fValue),
187 mbUseIt(!basegfx::fTools::equal(fValue, 1.0) && basegfx::fTools::more(fValue, 0.0) && basegfx::fTools::lessOrEqual(fValue, 10.0))
189 if(mbUseIt)
191 mfInvValue = 1.0 / mfValue;
195 BColorModifier_gamma::~BColorModifier_gamma()
199 bool BColorModifier_gamma::operator==(const BColorModifier& rCompare) const
201 const BColorModifier_gamma* pCompare = dynamic_cast< const BColorModifier_gamma* >(&rCompare);
203 if(!pCompare)
205 return false;
208 // getValue is sufficient, mfInvValue and mbUseIt are only helper values
209 return getValue() == pCompare->getValue();
212 ::basegfx::BColor BColorModifier_gamma::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
214 if(mbUseIt)
216 ::basegfx::BColor aRetval(
217 pow(aSourceColor.getRed(), mfInvValue),
218 pow(aSourceColor.getGreen(), mfInvValue),
219 pow(aSourceColor.getBlue(), mfInvValue));
221 aRetval.clamp();
222 return aRetval;
224 else
226 return aSourceColor;
229 } // end of namespace basegfx
231 //////////////////////////////////////////////////////////////////////////////
233 namespace basegfx
235 BColorModifier_RGBLuminanceContrast::BColorModifier_RGBLuminanceContrast(double fRed, double fGreen, double fBlue, double fLuminance, double fContrast)
236 : BColorModifier(),
237 mfRed(basegfx::clamp(fRed, -1.0, 1.0)),
238 mfGreen(basegfx::clamp(fGreen, -1.0, 1.0)),
239 mfBlue(basegfx::clamp(fBlue, -1.0, 1.0)),
240 mfLuminance(basegfx::clamp(fLuminance, -1.0, 1.0)),
241 mfContrast(basegfx::clamp(fContrast, -1.0, 1.0)),
242 mfContrastOff(1.0),
243 mfRedOff(0.0),
244 mfGreenOff(0.0),
245 mfBlueOff(0.0),
246 mbUseIt(false)
248 if(!basegfx::fTools::equalZero(mfRed)
249 || !basegfx::fTools::equalZero(mfGreen)
250 || !basegfx::fTools::equalZero(mfBlue)
251 || !basegfx::fTools::equalZero(mfLuminance)
252 || !basegfx::fTools::equalZero(mfContrast))
254 // calculate slope
255 if(mfContrast >= 0.0)
257 mfContrastOff = 128.0 / (128.0 - (mfContrast * 127.0));
259 else
261 mfContrastOff = ( 128.0 + (mfContrast * 127.0)) / 128.0;
264 // calculate unified contrast offset
265 const double fPreparedContrastOff((128.0 - mfContrastOff * 128.0) / 255.0);
266 const double fCombinedOffset(mfLuminance + fPreparedContrastOff);
268 // set full offsets
269 mfRedOff = mfRed + fCombinedOffset;
270 mfGreenOff = mfGreen + fCombinedOffset;
271 mfBlueOff = mfBlue + fCombinedOffset;
273 mbUseIt = true;
277 BColorModifier_RGBLuminanceContrast::~BColorModifier_RGBLuminanceContrast()
281 bool BColorModifier_RGBLuminanceContrast::operator==(const BColorModifier& rCompare) const
283 const BColorModifier_RGBLuminanceContrast* pCompare = dynamic_cast< const BColorModifier_RGBLuminanceContrast* >(&rCompare);
285 if(!pCompare)
287 return false;
290 // no need to compare other values, these are just helpers
291 return getRed() == pCompare->getRed()
292 && getGreen() == pCompare->getGreen()
293 && getBlue() == pCompare->getBlue()
294 && getLuminance() == pCompare->getLuminance()
295 && getContrast() == pCompare->getContrast();
298 ::basegfx::BColor BColorModifier_RGBLuminanceContrast::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
300 if(mbUseIt)
302 return basegfx::BColor(
303 basegfx::clamp(aSourceColor.getRed() * mfContrastOff + mfRedOff, 0.0, 1.0),
304 basegfx::clamp(aSourceColor.getGreen() * mfContrastOff + mfGreenOff, 0.0, 1.0),
305 basegfx::clamp(aSourceColor.getBlue() * mfContrastOff + mfBlueOff, 0.0, 1.0));
307 else
309 return aSourceColor;
312 } // end of namespace basegfx
314 //////////////////////////////////////////////////////////////////////////////
316 namespace basegfx
318 ::basegfx::BColor BColorModifierStack::getModifiedColor(const ::basegfx::BColor& rSource) const
320 if(maBColorModifiers.empty())
322 return rSource;
325 ::basegfx::BColor aRetval(rSource);
327 for(sal_uInt32 a(maBColorModifiers.size()); a;)
329 a--;
330 aRetval = maBColorModifiers[a]->getModifiedColor(aRetval);
333 return aRetval;
335 } // end of namespace basegfx
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */