summaryrefslogtreecommitdiffstats
path: root/basegfx/source/color
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 16:51:28 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 16:51:28 +0000
commit940b4d1848e8c70ab7642901a68594e8016caffc (patch)
treeeb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /basegfx/source/color
parentInitial commit. (diff)
downloadlibreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.tar.xz
libreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.zip
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'basegfx/source/color')
-rw-r--r--basegfx/source/color/bcolormodifier.cxx322
-rw-r--r--basegfx/source/color/bcolortools.cxx195
2 files changed, 517 insertions, 0 deletions
diff --git a/basegfx/source/color/bcolormodifier.cxx b/basegfx/source/color/bcolormodifier.cxx
new file mode 100644
index 000000000..e2a256728
--- /dev/null
+++ b/basegfx/source/color/bcolormodifier.cxx
@@ -0,0 +1,322 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <config_options.h>
+#include <sal/config.h>
+
+#include <algorithm>
+
+#include <basegfx/color/bcolormodifier.hxx>
+
+namespace basegfx
+{
+ BColorModifier::~BColorModifier()
+ {
+ }
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ BColorModifier_gray::~BColorModifier_gray()
+ {
+ }
+
+ bool BColorModifier_gray::operator==(const BColorModifier& rCompare) const
+ {
+ return dynamic_cast< const BColorModifier_gray* >(&rCompare) != nullptr;
+ }
+
+ ::basegfx::BColor BColorModifier_gray::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
+ {
+ const double fLuminance(aSourceColor.luminance());
+
+ return ::basegfx::BColor(fLuminance, fLuminance, fLuminance);
+ }
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ BColorModifier_invert::~BColorModifier_invert()
+ {
+ }
+
+ bool BColorModifier_invert::operator==(const BColorModifier& rCompare) const
+ {
+ return dynamic_cast< const BColorModifier_invert* >(&rCompare) != nullptr;
+ }
+
+ ::basegfx::BColor BColorModifier_invert::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
+ {
+ return ::basegfx::BColor(1.0 - aSourceColor.getRed(), 1.0 - aSourceColor.getGreen(), 1.0 - aSourceColor.getBlue());
+ }
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ BColorModifier_luminance_to_alpha::~BColorModifier_luminance_to_alpha()
+ {
+ }
+
+ bool BColorModifier_luminance_to_alpha::operator==(const BColorModifier& rCompare) const
+ {
+ return dynamic_cast< const BColorModifier_luminance_to_alpha* >(&rCompare) != nullptr;
+ }
+
+ ::basegfx::BColor BColorModifier_luminance_to_alpha::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
+ {
+ const double fAlpha(1.0 - ((aSourceColor.getRed() * 0.2125) + (aSourceColor.getGreen() * 0.7154) + (aSourceColor.getBlue() * 0.0721)));
+
+ return ::basegfx::BColor(fAlpha, fAlpha, fAlpha);
+ }
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ BColorModifier_replace::~BColorModifier_replace()
+ {
+ }
+
+ bool BColorModifier_replace::operator==(const BColorModifier& rCompare) const
+ {
+ const BColorModifier_replace* pCompare = dynamic_cast< const BColorModifier_replace* >(&rCompare);
+
+ if(!pCompare)
+ {
+ return false;
+ }
+
+ return getBColor() == pCompare->getBColor();
+ }
+
+ ::basegfx::BColor BColorModifier_replace::getModifiedColor(const ::basegfx::BColor& /*aSourceColor*/) const
+ {
+ return maBColor;
+ }
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ BColorModifier_interpolate::~BColorModifier_interpolate()
+ {
+ }
+
+ bool BColorModifier_interpolate::operator==(const BColorModifier& rCompare) const
+ {
+ const BColorModifier_interpolate* pCompare = dynamic_cast< const BColorModifier_interpolate* >(&rCompare);
+
+ if(!pCompare)
+ {
+ return false;
+ }
+
+ return maBColor == pCompare->maBColor && mfValue == pCompare->mfValue;
+ }
+
+ ::basegfx::BColor BColorModifier_interpolate::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
+ {
+ return interpolate(maBColor, aSourceColor, mfValue);
+ }
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ BColorModifier_black_and_white::~BColorModifier_black_and_white()
+ {
+ }
+
+ bool BColorModifier_black_and_white::operator==(const BColorModifier& rCompare) const
+ {
+ const BColorModifier_black_and_white* pCompare = dynamic_cast< const BColorModifier_black_and_white* >(&rCompare);
+
+ if(!pCompare)
+ {
+ return false;
+ }
+
+ return mfValue == pCompare->mfValue;
+ }
+
+ ::basegfx::BColor BColorModifier_black_and_white::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
+ {
+ const double fLuminance(aSourceColor.luminance());
+
+ if(fLuminance < mfValue)
+ {
+ return ::basegfx::BColor::getEmptyBColor();
+ }
+ else
+ {
+ return ::basegfx::BColor(1.0, 1.0, 1.0);
+ }
+ }
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ BColorModifier_gamma::BColorModifier_gamma(double fValue)
+ : BColorModifier(),
+ mfValue(fValue),
+ mfInvValue(fValue),
+ mbUseIt(!basegfx::fTools::equal(fValue, 1.0) && basegfx::fTools::more(fValue, 0.0) && basegfx::fTools::lessOrEqual(fValue, 10.0))
+ {
+ if(mbUseIt)
+ {
+ mfInvValue = 1.0 / mfValue;
+ }
+ }
+
+ BColorModifier_gamma::~BColorModifier_gamma()
+ {
+ }
+
+ bool BColorModifier_gamma::operator==(const BColorModifier& rCompare) const
+ {
+ const BColorModifier_gamma* pCompare = dynamic_cast< const BColorModifier_gamma* >(&rCompare);
+
+ if(!pCompare)
+ {
+ return false;
+ }
+
+ // getValue is sufficient, mfInvValue and mbUseIt are only helper values
+ return mfValue == pCompare->mfValue;
+ }
+
+ ::basegfx::BColor BColorModifier_gamma::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
+ {
+ if(mbUseIt)
+ {
+ ::basegfx::BColor aRetval(
+ pow(aSourceColor.getRed(), mfInvValue),
+ pow(aSourceColor.getGreen(), mfInvValue),
+ pow(aSourceColor.getBlue(), mfInvValue));
+
+ aRetval.clamp();
+ return aRetval;
+ }
+ else
+ {
+ return aSourceColor;
+ }
+ }
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ BColorModifier_RGBLuminanceContrast::BColorModifier_RGBLuminanceContrast(double fRed, double fGreen, double fBlue, double fLuminance, double fContrast)
+ : BColorModifier(),
+ mfRed(std::clamp(fRed, -1.0, 1.0)),
+ mfGreen(std::clamp(fGreen, -1.0, 1.0)),
+ mfBlue(std::clamp(fBlue, -1.0, 1.0)),
+ mfLuminance(std::clamp(fLuminance, -1.0, 1.0)),
+ mfContrast(std::clamp(fContrast, -1.0, 1.0)),
+ mfContrastOff(1.0),
+ mfRedOff(0.0),
+ mfGreenOff(0.0),
+ mfBlueOff(0.0),
+ mbUseIt(false)
+ {
+ if(!(!basegfx::fTools::equalZero(mfRed)
+ || !basegfx::fTools::equalZero(mfGreen)
+ || !basegfx::fTools::equalZero(mfBlue)
+ || !basegfx::fTools::equalZero(mfLuminance)
+ || !basegfx::fTools::equalZero(mfContrast)))
+ return;
+
+ // calculate slope
+ if(mfContrast >= 0.0)
+ {
+ mfContrastOff = 128.0 / (128.0 - (mfContrast * 127.0));
+ }
+ else
+ {
+ mfContrastOff = ( 128.0 + (mfContrast * 127.0)) / 128.0;
+ }
+
+ // calculate unified contrast offset
+ const double fPreparedContrastOff((128.0 - mfContrastOff * 128.0) / 255.0);
+ const double fCombinedOffset(mfLuminance + fPreparedContrastOff);
+
+ // set full offsets
+ mfRedOff = mfRed + fCombinedOffset;
+ mfGreenOff = mfGreen + fCombinedOffset;
+ mfBlueOff = mfBlue + fCombinedOffset;
+
+ mbUseIt = true;
+ }
+
+ BColorModifier_RGBLuminanceContrast::~BColorModifier_RGBLuminanceContrast()
+ {
+ }
+
+ bool BColorModifier_RGBLuminanceContrast::operator==(const BColorModifier& rCompare) const
+ {
+ const BColorModifier_RGBLuminanceContrast* pCompare = dynamic_cast< const BColorModifier_RGBLuminanceContrast* >(&rCompare);
+
+ if(!pCompare)
+ {
+ return false;
+ }
+
+ // no need to compare other values, these are just helpers
+ return mfRed == pCompare->mfRed
+ && mfGreen == pCompare->mfGreen
+ && mfBlue == pCompare->mfBlue
+ && mfLuminance == pCompare->mfLuminance
+ && mfContrast == pCompare->mfContrast;
+ }
+
+ ::basegfx::BColor BColorModifier_RGBLuminanceContrast::getModifiedColor(const ::basegfx::BColor& aSourceColor) const
+ {
+ if(mbUseIt)
+ {
+ return basegfx::BColor(
+ std::clamp(aSourceColor.getRed() * mfContrastOff + mfRedOff, 0.0, 1.0),
+ std::clamp(aSourceColor.getGreen() * mfContrastOff + mfGreenOff, 0.0, 1.0),
+ std::clamp(aSourceColor.getBlue() * mfContrastOff + mfBlueOff, 0.0, 1.0));
+ }
+ else
+ {
+ return aSourceColor;
+ }
+ }
+} // end of namespace basegfx
+
+namespace basegfx
+{
+ ::basegfx::BColor BColorModifierStack::getModifiedColor(const ::basegfx::BColor& rSource) const
+ {
+ if(maBColorModifiers.empty())
+ {
+ return rSource;
+ }
+
+ ::basegfx::BColor aRetval(rSource);
+
+ for(sal_uInt32 a(maBColorModifiers.size()); a;)
+ {
+ a--;
+ aRetval = maBColorModifiers[a]->getModifiedColor(aRetval);
+ }
+
+ return aRetval;
+ }
+} // end of namespace basegfx
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basegfx/source/color/bcolortools.cxx b/basegfx/source/color/bcolortools.cxx
new file mode 100644
index 000000000..727b9c781
--- /dev/null
+++ b/basegfx/source/color/bcolortools.cxx
@@ -0,0 +1,195 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <basegfx/numeric/ftools.hxx>
+#include <basegfx/color/bcolor.hxx>
+#include <basegfx/color/bcolortools.hxx>
+#include <rtl/math.hxx>
+
+namespace basegfx::utils
+{
+ BColor rgb2hsl(const BColor& rRGBColor)
+ {
+ const double r=rRGBColor.getRed(), g=rRGBColor.getGreen(), b=rRGBColor.getBlue();
+ const double minVal = std::min( std::min( r, g ), b );
+ const double maxVal = std::max( std::max( r, g ), b );
+ const double d = maxVal - minVal;
+
+ double h=0, s=0, l=0;
+
+ l = (maxVal + minVal) / 2.0;
+
+ if( ::basegfx::fTools::equalZero(d) )
+ {
+ s = h = 0; // hue undefined (achromatic case)
+ }
+ else
+ {
+ s = l > 0.5 ? d/(2.0-maxVal-minVal) :
+ d/(maxVal + minVal);
+
+ if( rtl::math::approxEqual(r, maxVal) )
+ h = (g - b)/d;
+ else if( rtl::math::approxEqual(g, maxVal) )
+ h = 2.0 + (b - r)/d;
+ else
+ h = 4.0 + (r - g)/d;
+
+ h *= 60.0;
+
+ if( h < 0.0 )
+ h += 360.0;
+ }
+
+ return BColor(h,s,l);
+ }
+
+ static double hsl2rgbHelper( double nValue1, double nValue2, double nHue )
+ {
+ // clamp hue to [0,360]
+ nHue = fmod( nHue, 360.0 );
+
+ // cope with wrap-arounds
+ if( nHue < 0.0 )
+ nHue += 360.0;
+
+ if( nHue < 60.0 )
+ return nValue1 + (nValue2 - nValue1)*nHue/60.0;
+ else if( nHue < 180.0 )
+ return nValue2;
+ else if( nHue < 240.0 )
+ return nValue1 + (nValue2 - nValue1)*(240.0 - nHue)/60.0;
+ else
+ return nValue1;
+ }
+
+ BColor hsl2rgb(const BColor& rHSLColor)
+ {
+ const double h=rHSLColor.getRed(), s=rHSLColor.getGreen(), l=rHSLColor.getBlue();
+
+ if( fTools::equalZero(s) )
+ return BColor(l, l, l ); // achromatic case
+
+ const double nVal1( l <= 0.5 ? l*(1.0 + s) : l + s - l*s );
+ const double nVal2( 2.0*l - nVal1 );
+
+ return BColor(
+ hsl2rgbHelper(nVal2,
+ nVal1,
+ h + 120.0),
+ hsl2rgbHelper(nVal2,
+ nVal1,
+ h),
+ hsl2rgbHelper(nVal2,
+ nVal1,
+ h - 120.0) );
+ }
+
+ BColor rgb2hsv(const BColor& rRGBColor)
+ {
+ const double r=rRGBColor.getRed(), g=rRGBColor.getGreen(), b=rRGBColor.getBlue();
+ const double maxVal = std::max(std::max(r,g),b);
+ const double minVal = std::min(std::min(r,g),b);
+ const double delta = maxVal-minVal;
+
+ double h=0, s=0, v=0;
+
+ v = maxVal;
+ if( fTools::equalZero(v) )
+ s = 0;
+ else
+ s = delta / v;
+
+ if( !fTools::equalZero(s) )
+ {
+ if( rtl::math::approxEqual(maxVal, r) )
+ {
+ h = (g - b) / delta;
+ }
+ else if( rtl::math::approxEqual(maxVal, g) )
+ {
+ h = 2.0 + (b - r) / delta;
+ }
+ else
+ {
+ h = 4.0 + (r - g) / delta;
+ }
+
+ h *= 60.0;
+
+ if( h < 0 )
+ h += 360;
+ }
+
+ return BColor(h,s,v);
+ }
+
+ BColor hsv2rgb(const BColor& rHSVColor)
+ {
+ double h=rHSVColor.getRed();
+ const double s=rHSVColor.getGreen(), v=rHSVColor.getBlue();
+
+ if( fTools::equalZero(s) )
+ {
+ // achromatic case: no hue.
+ return BColor(v,v,v);
+ }
+ else
+ {
+ if( fTools::equal(h,360) )
+ h = 0; // 360 degrees is equivalent to 0 degrees
+
+ h /= 60.0;
+ const sal_Int32 intval = static_cast< sal_Int32 >( h );
+ const double f = h - intval;
+ const double p = v*(1.0-s);
+ const double q = v*(1.0-(s*f));
+ const double t = v*(1.0-(s*(1.0-f)));
+
+ /* which hue area? */
+ switch( intval )
+ {
+ case 0:
+ return BColor(v,t,p);
+
+ case 1:
+ return BColor(q,v,p);
+
+ case 2:
+ return BColor(p,v,t);
+
+ case 3:
+ return BColor(p,q,v);
+
+ case 4:
+ return BColor(t,p,v);
+
+ case 5:
+ return BColor(v,p,q);
+
+ default:
+ // hue overflow
+ return BColor();
+ }
+ }
+ }
+
+} // end of namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */