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 #include <osl/diagnose.h>
26 BColorModifier::~BColorModifier()
29 } // end of 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
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
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
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
);
101 return getBColor() == pCompare
->getBColor();
104 ::basegfx::BColor
BColorModifier_replace::getModifiedColor(const ::basegfx::BColor
& /*aSourceColor*/) const
108 } // end of 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
);
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
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
);
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();
162 return ::basegfx::BColor(1.0, 1.0, 1.0);
165 } // end of namespace basegfx
169 BColorModifier_gamma::BColorModifier_gamma(double fValue
)
173 mbUseIt(!basegfx::fTools::equal(fValue
, 1.0) && basegfx::fTools::more(fValue
, 0.0) && basegfx::fTools::lessOrEqual(fValue
, 10.0))
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
);
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
202 ::basegfx::BColor
aRetval(
203 pow(aSourceColor
.getRed(), mfInvValue
),
204 pow(aSourceColor
.getGreen(), mfInvValue
),
205 pow(aSourceColor
.getBlue(), mfInvValue
));
215 } // end of namespace basegfx
219 BColorModifier_RGBLuminanceContrast::BColorModifier_RGBLuminanceContrast(double fRed
, double fGreen
, double fBlue
, double fLuminance
, double fContrast
)
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)),
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
))
239 if(mfContrast
>= 0.0)
241 mfContrastOff
= 128.0 / (128.0 - (mfContrast
* 127.0));
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
);
253 mfRedOff
= mfRed
+ fCombinedOffset
;
254 mfGreenOff
= mfGreen
+ fCombinedOffset
;
255 mfBlueOff
= mfBlue
+ fCombinedOffset
;
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
);
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
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));
296 } // end of namespace basegfx
300 ::basegfx::BColor
BColorModifierStack::getModifiedColor(const ::basegfx::BColor
& rSource
) const
302 if(maBColorModifiers
.empty())
307 ::basegfx::BColor
aRetval(rSource
);
309 for(sal_uInt32
a(maBColorModifiers
.size()); a
;)
312 aRetval
= maBColorModifiers
[a
]->getModifiedColor(aRetval
);
317 } // end of namespace basegfx
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */