1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 //////////////////////////////////////////////////////////////////////////////
26 BColorModifier::~BColorModifier()
29 } // end of namespace basegfx
31 //////////////////////////////////////////////////////////////////////////////
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 //////////////////////////////////////////////////////////////////////////////
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 //////////////////////////////////////////////////////////////////////////////
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 //////////////////////////////////////////////////////////////////////////////
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
);
109 return getBColor() == pCompare
->getBColor();
112 ::basegfx::BColor
BColorModifier_replace::getModifiedColor(const ::basegfx::BColor
& /*aSourceColor*/) const
116 } // end of namespace basegfx
118 //////////////////////////////////////////////////////////////////////////////
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
);
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 //////////////////////////////////////////////////////////////////////////////
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
);
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();
174 return ::basegfx::BColor(1.0, 1.0, 1.0);
177 } // end of namespace basegfx
179 //////////////////////////////////////////////////////////////////////////////
183 BColorModifier_gamma::BColorModifier_gamma(double fValue
)
187 mbUseIt(!basegfx::fTools::equal(fValue
, 1.0) && basegfx::fTools::more(fValue
, 0.0) && basegfx::fTools::lessOrEqual(fValue
, 10.0))
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
);
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
216 ::basegfx::BColor
aRetval(
217 pow(aSourceColor
.getRed(), mfInvValue
),
218 pow(aSourceColor
.getGreen(), mfInvValue
),
219 pow(aSourceColor
.getBlue(), mfInvValue
));
229 } // end of namespace basegfx
231 //////////////////////////////////////////////////////////////////////////////
235 BColorModifier_RGBLuminanceContrast::BColorModifier_RGBLuminanceContrast(double fRed
, double fGreen
, double fBlue
, double fLuminance
, double fContrast
)
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)),
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
))
255 if(mfContrast
>= 0.0)
257 mfContrastOff
= 128.0 / (128.0 - (mfContrast
* 127.0));
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
);
269 mfRedOff
= mfRed
+ fCombinedOffset
;
270 mfGreenOff
= mfGreen
+ fCombinedOffset
;
271 mfBlueOff
= mfBlue
+ fCombinedOffset
;
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
);
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
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));
312 } // end of namespace basegfx
314 //////////////////////////////////////////////////////////////////////////////
318 ::basegfx::BColor
BColorModifierStack::getModifiedColor(const ::basegfx::BColor
& rSource
) const
320 if(maBColorModifiers
.empty())
325 ::basegfx::BColor
aRetval(rSource
);
327 for(sal_uInt32
a(maBColorModifiers
.size()); a
;)
330 aRetval
= maBColorModifiers
[a
]->getModifiedColor(aRetval
);
335 } // end of namespace basegfx
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */