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 <sal/config.h>
24 #include <basegfx/color/bcolormodifier.hxx>
28 BColorModifier::~BColorModifier()
31 } // end of 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
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
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
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
);
103 return getBColor() == pCompare
->getBColor();
106 ::basegfx::BColor
BColorModifier_replace::getModifiedColor(const ::basegfx::BColor
& /*aSourceColor*/) const
110 } // end of 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
);
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
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
);
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();
164 return ::basegfx::BColor(1.0, 1.0, 1.0);
167 } // end of namespace basegfx
171 BColorModifier_gamma::BColorModifier_gamma(double fValue
)
175 mbUseIt(!basegfx::fTools::equal(fValue
, 1.0) && basegfx::fTools::more(fValue
, 0.0) && basegfx::fTools::lessOrEqual(fValue
, 10.0))
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
);
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
204 ::basegfx::BColor
aRetval(
205 pow(aSourceColor
.getRed(), mfInvValue
),
206 pow(aSourceColor
.getGreen(), mfInvValue
),
207 pow(aSourceColor
.getBlue(), mfInvValue
));
217 } // end of namespace basegfx
221 BColorModifier_RGBLuminanceContrast::BColorModifier_RGBLuminanceContrast(double fRed
, double fGreen
, double fBlue
, double fLuminance
, double fContrast
)
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)),
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
))
241 if(mfContrast
>= 0.0)
243 mfContrastOff
= 128.0 / (128.0 - (mfContrast
* 127.0));
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
);
255 mfRedOff
= mfRed
+ fCombinedOffset
;
256 mfGreenOff
= mfGreen
+ fCombinedOffset
;
257 mfBlueOff
= mfBlue
+ fCombinedOffset
;
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
);
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
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));
298 } // end of namespace basegfx
302 ::basegfx::BColor
BColorModifierStack::getModifiedColor(const ::basegfx::BColor
& rSource
) const
304 if(maBColorModifiers
.empty())
309 ::basegfx::BColor
aRetval(rSource
);
311 for(sal_uInt32
a(maBColorModifiers
.size()); a
;)
314 aRetval
= maBColorModifiers
[a
]->getModifiedColor(aRetval
);
319 } // end of namespace basegfx
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */