bump product version to 5.0.4.1
[LibreOffice.git] / basegfx / source / color / bcolormodifier.cxx
blob7727efdb034c7fcb1c3605ee62d68c83ebb616f5
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 #include <osl/diagnose.h>
24 namespace basegfx
26 BColorModifier::~BColorModifier()
29 } // end of namespace basegfx
31 namespace basegfx
33 BColorModifier_gray::~BColorModifier_gray()
37 bool BColorModifier_gray::operator==(const BColorModifier& rCompare) const
39 return 0 != dynamic_cast< const BColorModifier_gray* >(&rCompare);
42 ::basegfx::BColor BColorModifier_gray::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
44 const double fLuminance(aSourceColor.luminance());
46 return ::basegfx::BColor(fLuminance, fLuminance, fLuminance);
48 } // end of namespace basegfx
50 namespace basegfx
52 BColorModifier_invert::~BColorModifier_invert()
56 bool BColorModifier_invert::operator==(const BColorModifier& rCompare) const
58 return 0 != dynamic_cast< const BColorModifier_invert* >(&rCompare);
61 ::basegfx::BColor BColorModifier_invert::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
63 return ::basegfx::BColor(1.0 - aSourceColor.getRed(), 1.0 - aSourceColor.getGreen(), 1.0 - aSourceColor.getBlue());
65 } // end of namespace basegfx
67 namespace basegfx
69 BColorModifier_luminance_to_alpha::~BColorModifier_luminance_to_alpha()
73 bool BColorModifier_luminance_to_alpha::operator==(const BColorModifier& rCompare) const
75 return 0 != dynamic_cast< const BColorModifier_luminance_to_alpha* >(&rCompare);
78 ::basegfx::BColor BColorModifier_luminance_to_alpha::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
80 const double fAlpha(1.0 - ((aSourceColor.getRed() * 0.2125) + (aSourceColor.getGreen() * 0.7154) + (aSourceColor.getBlue() * 0.0721)));
82 return ::basegfx::BColor(fAlpha, fAlpha, fAlpha);
84 } // end of namespace basegfx
86 namespace basegfx
88 BColorModifier_replace::~BColorModifier_replace()
92 bool BColorModifier_replace::operator==(const BColorModifier& rCompare) const
94 const BColorModifier_replace* pCompare = dynamic_cast< const BColorModifier_replace* >(&rCompare);
96 if(!pCompare)
98 return false;
101 return getBColor() == pCompare->getBColor();
104 ::basegfx::BColor BColorModifier_replace::getModifiedColor(const ::basegfx::BColor& /*aSourceColor*/) const
106 return maBColor;
108 } // end of namespace basegfx
110 namespace basegfx
112 BColorModifier_interpolate::~BColorModifier_interpolate()
116 bool BColorModifier_interpolate::operator==(const BColorModifier& rCompare) const
118 const BColorModifier_interpolate* pCompare = dynamic_cast< const BColorModifier_interpolate* >(&rCompare);
120 if(!pCompare)
122 return false;
125 return getBColor() == pCompare->getBColor() && getValue() == pCompare->getValue();
128 ::basegfx::BColor BColorModifier_interpolate::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
130 return interpolate(maBColor, aSourceColor, mfValue);
132 } // end of namespace basegfx
134 namespace basegfx
136 BColorModifier_black_and_white::~BColorModifier_black_and_white()
140 bool BColorModifier_black_and_white::operator==(const BColorModifier& rCompare) const
142 const BColorModifier_black_and_white* pCompare = dynamic_cast< const BColorModifier_black_and_white* >(&rCompare);
144 if(!pCompare)
146 return false;
149 return getValue() == pCompare->getValue();
152 ::basegfx::BColor BColorModifier_black_and_white::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
154 const double fLuminance(aSourceColor.luminance());
156 if(fLuminance < mfValue)
158 return ::basegfx::BColor::getEmptyBColor();
160 else
162 return ::basegfx::BColor(1.0, 1.0, 1.0);
165 } // end of namespace basegfx
167 namespace basegfx
169 BColorModifier_gamma::BColorModifier_gamma(double fValue)
170 : BColorModifier(),
171 mfValue(fValue),
172 mfInvValue(fValue),
173 mbUseIt(!basegfx::fTools::equal(fValue, 1.0) && basegfx::fTools::more(fValue, 0.0) && basegfx::fTools::lessOrEqual(fValue, 10.0))
175 if(mbUseIt)
177 mfInvValue = 1.0 / mfValue;
181 BColorModifier_gamma::~BColorModifier_gamma()
185 bool BColorModifier_gamma::operator==(const BColorModifier& rCompare) const
187 const BColorModifier_gamma* pCompare = dynamic_cast< const BColorModifier_gamma* >(&rCompare);
189 if(!pCompare)
191 return false;
194 // getValue is sufficient, mfInvValue and mbUseIt are only helper values
195 return getValue() == pCompare->getValue();
198 ::basegfx::BColor BColorModifier_gamma::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
200 if(mbUseIt)
202 ::basegfx::BColor aRetval(
203 pow(aSourceColor.getRed(), mfInvValue),
204 pow(aSourceColor.getGreen(), mfInvValue),
205 pow(aSourceColor.getBlue(), mfInvValue));
207 aRetval.clamp();
208 return aRetval;
210 else
212 return aSourceColor;
215 } // end of namespace basegfx
217 namespace basegfx
219 BColorModifier_RGBLuminanceContrast::BColorModifier_RGBLuminanceContrast(double fRed, double fGreen, double fBlue, double fLuminance, double fContrast)
220 : BColorModifier(),
221 mfRed(basegfx::clamp(fRed, -1.0, 1.0)),
222 mfGreen(basegfx::clamp(fGreen, -1.0, 1.0)),
223 mfBlue(basegfx::clamp(fBlue, -1.0, 1.0)),
224 mfLuminance(basegfx::clamp(fLuminance, -1.0, 1.0)),
225 mfContrast(basegfx::clamp(fContrast, -1.0, 1.0)),
226 mfContrastOff(1.0),
227 mfRedOff(0.0),
228 mfGreenOff(0.0),
229 mfBlueOff(0.0),
230 mbUseIt(false)
232 if(!basegfx::fTools::equalZero(mfRed)
233 || !basegfx::fTools::equalZero(mfGreen)
234 || !basegfx::fTools::equalZero(mfBlue)
235 || !basegfx::fTools::equalZero(mfLuminance)
236 || !basegfx::fTools::equalZero(mfContrast))
238 // calculate slope
239 if(mfContrast >= 0.0)
241 mfContrastOff = 128.0 / (128.0 - (mfContrast * 127.0));
243 else
245 mfContrastOff = ( 128.0 + (mfContrast * 127.0)) / 128.0;
248 // calculate unified contrast offset
249 const double fPreparedContrastOff((128.0 - mfContrastOff * 128.0) / 255.0);
250 const double fCombinedOffset(mfLuminance + fPreparedContrastOff);
252 // set full offsets
253 mfRedOff = mfRed + fCombinedOffset;
254 mfGreenOff = mfGreen + fCombinedOffset;
255 mfBlueOff = mfBlue + fCombinedOffset;
257 mbUseIt = true;
261 BColorModifier_RGBLuminanceContrast::~BColorModifier_RGBLuminanceContrast()
265 bool BColorModifier_RGBLuminanceContrast::operator==(const BColorModifier& rCompare) const
267 const BColorModifier_RGBLuminanceContrast* pCompare = dynamic_cast< const BColorModifier_RGBLuminanceContrast* >(&rCompare);
269 if(!pCompare)
271 return false;
274 // no need to compare other values, these are just helpers
275 return getRed() == pCompare->getRed()
276 && getGreen() == pCompare->getGreen()
277 && getBlue() == pCompare->getBlue()
278 && getLuminance() == pCompare->getLuminance()
279 && getContrast() == pCompare->getContrast();
282 ::basegfx::BColor BColorModifier_RGBLuminanceContrast::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
284 if(mbUseIt)
286 return basegfx::BColor(
287 basegfx::clamp(aSourceColor.getRed() * mfContrastOff + mfRedOff, 0.0, 1.0),
288 basegfx::clamp(aSourceColor.getGreen() * mfContrastOff + mfGreenOff, 0.0, 1.0),
289 basegfx::clamp(aSourceColor.getBlue() * mfContrastOff + mfBlueOff, 0.0, 1.0));
291 else
293 return aSourceColor;
296 } // end of namespace basegfx
298 namespace basegfx
300 ::basegfx::BColor BColorModifierStack::getModifiedColor(const ::basegfx::BColor& rSource) const
302 if(maBColorModifiers.empty())
304 return rSource;
307 ::basegfx::BColor aRetval(rSource);
309 for(sal_uInt32 a(maBColorModifiers.size()); a;)
311 a--;
312 aRetval = maBColorModifiers[a]->getModifiedColor(aRetval);
315 return aRetval;
317 } // end of namespace basegfx
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */