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()
32 BColorModifier_gray::~BColorModifier_gray()
36 bool BColorModifier_gray::operator==(const BColorModifier
& rCompare
) const
38 return dynamic_cast< const BColorModifier_gray
* >(&rCompare
) != nullptr;
41 ::basegfx::BColor
BColorModifier_gray::getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const
43 const double fLuminance(aSourceColor
.luminance());
45 return ::basegfx::BColor(fLuminance
, fLuminance
, fLuminance
);
48 BColorModifier_invert::~BColorModifier_invert()
52 bool BColorModifier_invert::operator==(const BColorModifier
& rCompare
) const
54 return dynamic_cast< const BColorModifier_invert
* >(&rCompare
) != nullptr;
57 ::basegfx::BColor
BColorModifier_invert::getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const
59 return ::basegfx::BColor(1.0 - aSourceColor
.getRed(), 1.0 - aSourceColor
.getGreen(), 1.0 - aSourceColor
.getBlue());
62 BColorModifier_luminance_to_alpha::~BColorModifier_luminance_to_alpha()
66 bool BColorModifier_luminance_to_alpha::operator==(const BColorModifier
& rCompare
) const
68 return dynamic_cast< const BColorModifier_luminance_to_alpha
* >(&rCompare
) != nullptr;
71 ::basegfx::BColor
BColorModifier_luminance_to_alpha::getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const
73 const double fAlpha(1.0 - ((aSourceColor
.getRed() * 0.2125) + (aSourceColor
.getGreen() * 0.7154) + (aSourceColor
.getBlue() * 0.0721)));
75 return ::basegfx::BColor(fAlpha
, fAlpha
, fAlpha
);
78 BColorModifier_replace::~BColorModifier_replace()
82 bool BColorModifier_replace::operator==(const BColorModifier
& rCompare
) const
84 const BColorModifier_replace
* pCompare
= dynamic_cast< const BColorModifier_replace
* >(&rCompare
);
91 return getBColor() == pCompare
->getBColor();
94 ::basegfx::BColor
BColorModifier_replace::getModifiedColor(const ::basegfx::BColor
& /*aSourceColor*/) const
99 BColorModifier_interpolate::~BColorModifier_interpolate()
103 bool BColorModifier_interpolate::operator==(const BColorModifier
& rCompare
) const
105 const BColorModifier_interpolate
* pCompare
= dynamic_cast< const BColorModifier_interpolate
* >(&rCompare
);
112 return maBColor
== pCompare
->maBColor
&& mfValue
== pCompare
->mfValue
;
115 ::basegfx::BColor
BColorModifier_interpolate::getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const
117 return interpolate(maBColor
, aSourceColor
, mfValue
);
120 BColorModifier_black_and_white::~BColorModifier_black_and_white()
124 bool BColorModifier_black_and_white::operator==(const BColorModifier
& rCompare
) const
126 const BColorModifier_black_and_white
* pCompare
= dynamic_cast< const BColorModifier_black_and_white
* >(&rCompare
);
133 return mfValue
== pCompare
->mfValue
;
136 ::basegfx::BColor
BColorModifier_black_and_white::getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const
138 const double fLuminance(aSourceColor
.luminance());
140 if(fLuminance
< mfValue
)
142 return ::basegfx::BColor::getEmptyBColor();
146 return ::basegfx::BColor(1.0, 1.0, 1.0);
150 BColorModifier_gamma::BColorModifier_gamma(double fValue
)
154 mbUseIt(!basegfx::fTools::equal(fValue
, 1.0) && basegfx::fTools::more(fValue
, 0.0) && basegfx::fTools::lessOrEqual(fValue
, 10.0))
158 mfInvValue
= 1.0 / mfValue
;
162 BColorModifier_gamma::~BColorModifier_gamma()
166 bool BColorModifier_gamma::operator==(const BColorModifier
& rCompare
) const
168 const BColorModifier_gamma
* pCompare
= dynamic_cast< const BColorModifier_gamma
* >(&rCompare
);
175 // getValue is sufficient, mfInvValue and mbUseIt are only helper values
176 return mfValue
== pCompare
->mfValue
;
179 ::basegfx::BColor
BColorModifier_gamma::getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const
183 ::basegfx::BColor
aRetval(
184 pow(aSourceColor
.getRed(), mfInvValue
),
185 pow(aSourceColor
.getGreen(), mfInvValue
),
186 pow(aSourceColor
.getBlue(), mfInvValue
));
197 BColorModifier_RGBLuminanceContrast::BColorModifier_RGBLuminanceContrast(double fRed
, double fGreen
, double fBlue
, double fLuminance
, double fContrast
)
199 mfRed(std::clamp(fRed
, -1.0, 1.0)),
200 mfGreen(std::clamp(fGreen
, -1.0, 1.0)),
201 mfBlue(std::clamp(fBlue
, -1.0, 1.0)),
202 mfLuminance(std::clamp(fLuminance
, -1.0, 1.0)),
203 mfContrast(std::clamp(fContrast
, -1.0, 1.0)),
210 if(basegfx::fTools::equalZero(mfRed
)
211 && basegfx::fTools::equalZero(mfGreen
)
212 && basegfx::fTools::equalZero(mfBlue
)
213 && basegfx::fTools::equalZero(mfLuminance
)
214 && basegfx::fTools::equalZero(mfContrast
))
218 if(mfContrast
>= 0.0)
220 mfContrastOff
= 128.0 / (128.0 - (mfContrast
* 127.0));
224 mfContrastOff
= ( 128.0 + (mfContrast
* 127.0)) / 128.0;
227 // calculate unified contrast offset
228 const double fPreparedContrastOff((128.0 - mfContrastOff
* 128.0) / 255.0);
229 const double fCombinedOffset(mfLuminance
+ fPreparedContrastOff
);
232 mfRedOff
= mfRed
+ fCombinedOffset
;
233 mfGreenOff
= mfGreen
+ fCombinedOffset
;
234 mfBlueOff
= mfBlue
+ fCombinedOffset
;
239 BColorModifier_RGBLuminanceContrast::~BColorModifier_RGBLuminanceContrast()
243 bool BColorModifier_RGBLuminanceContrast::operator==(const BColorModifier
& rCompare
) const
245 const BColorModifier_RGBLuminanceContrast
* pCompare
= dynamic_cast< const BColorModifier_RGBLuminanceContrast
* >(&rCompare
);
252 // no need to compare other values, these are just helpers
253 return mfRed
== pCompare
->mfRed
254 && mfGreen
== pCompare
->mfGreen
255 && mfBlue
== pCompare
->mfBlue
256 && mfLuminance
== pCompare
->mfLuminance
257 && mfContrast
== pCompare
->mfContrast
;
260 ::basegfx::BColor
BColorModifier_RGBLuminanceContrast::getModifiedColor(const ::basegfx::BColor
& aSourceColor
) const
264 return basegfx::BColor(
265 std::clamp(aSourceColor
.getRed() * mfContrastOff
+ mfRedOff
, 0.0, 1.0),
266 std::clamp(aSourceColor
.getGreen() * mfContrastOff
+ mfGreenOff
, 0.0, 1.0),
267 std::clamp(aSourceColor
.getBlue() * mfContrastOff
+ mfBlueOff
, 0.0, 1.0));
275 ::basegfx::BColor
BColorModifierStack::getModifiedColor(const ::basegfx::BColor
& rSource
) const
277 if(maBColorModifiers
.empty())
282 ::basegfx::BColor
aRetval(rSource
);
284 for(sal_uInt32
a(maBColorModifiers
.size()); a
;)
287 aRetval
= maBColorModifiers
[a
]->getModifiedColor(aRetval
);
292 } // end of namespace basegfx
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */