Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / basegfx / source / color / bcolormodifier.cxx
blob182083f6fbdb8ed55f44d1e077bc885c5c767041
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 <sal/config.h>
22 #include <algorithm>
24 #include <basegfx/color/bcolormodifier.hxx>
26 namespace basegfx
28 BColorModifier::~BColorModifier()
31 } // end of namespace basegfx
33 namespace basegfx
35 BColorModifier_gray::~BColorModifier_gray()
39 bool BColorModifier_gray::operator==(const BColorModifier& rCompare) const
41 return dynamic_cast< const BColorModifier_gray* >(&rCompare) != nullptr;
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 namespace basegfx
54 BColorModifier_invert::~BColorModifier_invert()
58 bool BColorModifier_invert::operator==(const BColorModifier& rCompare) const
60 return dynamic_cast< const BColorModifier_invert* >(&rCompare) != nullptr;
63 ::basegfx::BColor BColorModifier_invert::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
65 return ::basegfx::BColor(1.0 - aSourceColor.getRed(), 1.0 - aSourceColor.getGreen(), 1.0 - aSourceColor.getBlue());
67 } // end of namespace basegfx
69 namespace basegfx
71 BColorModifier_luminance_to_alpha::~BColorModifier_luminance_to_alpha()
75 bool BColorModifier_luminance_to_alpha::operator==(const BColorModifier& rCompare) const
77 return dynamic_cast< const BColorModifier_luminance_to_alpha* >(&rCompare) != nullptr;
80 ::basegfx::BColor BColorModifier_luminance_to_alpha::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
82 const double fAlpha(1.0 - ((aSourceColor.getRed() * 0.2125) + (aSourceColor.getGreen() * 0.7154) + (aSourceColor.getBlue() * 0.0721)));
84 return ::basegfx::BColor(fAlpha, fAlpha, fAlpha);
86 } // end of namespace basegfx
88 namespace basegfx
90 BColorModifier_replace::~BColorModifier_replace()
94 bool BColorModifier_replace::operator==(const BColorModifier& rCompare) const
96 const BColorModifier_replace* pCompare = dynamic_cast< const BColorModifier_replace* >(&rCompare);
98 if(!pCompare)
100 return false;
103 return getBColor() == pCompare->getBColor();
106 ::basegfx::BColor BColorModifier_replace::getModifiedColor(const ::basegfx::BColor& /*aSourceColor*/) const
108 return maBColor;
110 } // end of namespace basegfx
112 namespace basegfx
114 BColorModifier_interpolate::~BColorModifier_interpolate()
118 bool BColorModifier_interpolate::operator==(const BColorModifier& rCompare) const
120 const BColorModifier_interpolate* pCompare = dynamic_cast< const BColorModifier_interpolate* >(&rCompare);
122 if(!pCompare)
124 return false;
127 return getBColor() == pCompare->getBColor() && getValue() == pCompare->getValue();
130 ::basegfx::BColor BColorModifier_interpolate::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
132 return interpolate(maBColor, aSourceColor, mfValue);
134 } // end of namespace basegfx
136 namespace basegfx
138 BColorModifier_black_and_white::~BColorModifier_black_and_white()
142 bool BColorModifier_black_and_white::operator==(const BColorModifier& rCompare) const
144 const BColorModifier_black_and_white* pCompare = dynamic_cast< const BColorModifier_black_and_white* >(&rCompare);
146 if(!pCompare)
148 return false;
151 return getValue() == pCompare->getValue();
154 ::basegfx::BColor BColorModifier_black_and_white::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
156 const double fLuminance(aSourceColor.luminance());
158 if(fLuminance < mfValue)
160 return ::basegfx::BColor::getEmptyBColor();
162 else
164 return ::basegfx::BColor(1.0, 1.0, 1.0);
167 } // end of namespace basegfx
169 namespace basegfx
171 BColorModifier_gamma::BColorModifier_gamma(double fValue)
172 : BColorModifier(),
173 mfValue(fValue),
174 mfInvValue(fValue),
175 mbUseIt(!basegfx::fTools::equal(fValue, 1.0) && basegfx::fTools::more(fValue, 0.0) && basegfx::fTools::lessOrEqual(fValue, 10.0))
177 if(mbUseIt)
179 mfInvValue = 1.0 / mfValue;
183 BColorModifier_gamma::~BColorModifier_gamma()
187 bool BColorModifier_gamma::operator==(const BColorModifier& rCompare) const
189 const BColorModifier_gamma* pCompare = dynamic_cast< const BColorModifier_gamma* >(&rCompare);
191 if(!pCompare)
193 return false;
196 // getValue is sufficient, mfInvValue and mbUseIt are only helper values
197 return getValue() == pCompare->getValue();
200 ::basegfx::BColor BColorModifier_gamma::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
202 if(mbUseIt)
204 ::basegfx::BColor aRetval(
205 pow(aSourceColor.getRed(), mfInvValue),
206 pow(aSourceColor.getGreen(), mfInvValue),
207 pow(aSourceColor.getBlue(), mfInvValue));
209 aRetval.clamp();
210 return aRetval;
212 else
214 return aSourceColor;
217 } // end of namespace basegfx
219 namespace basegfx
221 BColorModifier_RGBLuminanceContrast::BColorModifier_RGBLuminanceContrast(double fRed, double fGreen, double fBlue, double fLuminance, double fContrast)
222 : BColorModifier(),
223 mfRed(std::clamp(fRed, -1.0, 1.0)),
224 mfGreen(std::clamp(fGreen, -1.0, 1.0)),
225 mfBlue(std::clamp(fBlue, -1.0, 1.0)),
226 mfLuminance(std::clamp(fLuminance, -1.0, 1.0)),
227 mfContrast(std::clamp(fContrast, -1.0, 1.0)),
228 mfContrastOff(1.0),
229 mfRedOff(0.0),
230 mfGreenOff(0.0),
231 mfBlueOff(0.0),
232 mbUseIt(false)
234 if(!basegfx::fTools::equalZero(mfRed)
235 || !basegfx::fTools::equalZero(mfGreen)
236 || !basegfx::fTools::equalZero(mfBlue)
237 || !basegfx::fTools::equalZero(mfLuminance)
238 || !basegfx::fTools::equalZero(mfContrast))
240 // calculate slope
241 if(mfContrast >= 0.0)
243 mfContrastOff = 128.0 / (128.0 - (mfContrast * 127.0));
245 else
247 mfContrastOff = ( 128.0 + (mfContrast * 127.0)) / 128.0;
250 // calculate unified contrast offset
251 const double fPreparedContrastOff((128.0 - mfContrastOff * 128.0) / 255.0);
252 const double fCombinedOffset(mfLuminance + fPreparedContrastOff);
254 // set full offsets
255 mfRedOff = mfRed + fCombinedOffset;
256 mfGreenOff = mfGreen + fCombinedOffset;
257 mfBlueOff = mfBlue + fCombinedOffset;
259 mbUseIt = true;
263 BColorModifier_RGBLuminanceContrast::~BColorModifier_RGBLuminanceContrast()
267 bool BColorModifier_RGBLuminanceContrast::operator==(const BColorModifier& rCompare) const
269 const BColorModifier_RGBLuminanceContrast* pCompare = dynamic_cast< const BColorModifier_RGBLuminanceContrast* >(&rCompare);
271 if(!pCompare)
273 return false;
276 // no need to compare other values, these are just helpers
277 return getRed() == pCompare->getRed()
278 && getGreen() == pCompare->getGreen()
279 && getBlue() == pCompare->getBlue()
280 && getLuminance() == pCompare->getLuminance()
281 && getContrast() == pCompare->getContrast();
284 ::basegfx::BColor BColorModifier_RGBLuminanceContrast::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
286 if(mbUseIt)
288 return basegfx::BColor(
289 std::clamp(aSourceColor.getRed() * mfContrastOff + mfRedOff, 0.0, 1.0),
290 std::clamp(aSourceColor.getGreen() * mfContrastOff + mfGreenOff, 0.0, 1.0),
291 std::clamp(aSourceColor.getBlue() * mfContrastOff + mfBlueOff, 0.0, 1.0));
293 else
295 return aSourceColor;
298 } // end of namespace basegfx
300 namespace basegfx
302 ::basegfx::BColor BColorModifierStack::getModifiedColor(const ::basegfx::BColor& rSource) const
304 if(maBColorModifiers.empty())
306 return rSource;
309 ::basegfx::BColor aRetval(rSource);
311 for(sal_uInt32 a(maBColorModifiers.size()); a;)
313 a--;
314 aRetval = maBColorModifiers[a]->getModifiedColor(aRetval);
317 return aRetval;
319 } // end of namespace basegfx
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */