summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2015-05-07 13:55:59 +0900
committerTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2015-05-07 14:10:51 +0900
commit5984cc83fe756f7483d1ac582b8adbef5042be8b (patch)
tree11a06b6c812e1a7b5e398ebbbb059ffc60c24872
parent6719b36a66171f59b383ccc5e7497d208bd566aa (diff)
Add ability to change tint/shade of a color.
Change-Id: I6933393732d23fe9386cb8b768676887c026bd39
-rw-r--r--include/tools/color.hxx10
-rw-r--r--tools/source/generic/color.cxx30
2 files changed, 40 insertions, 0 deletions
diff --git a/include/tools/color.hxx b/include/tools/color.hxx
index f940dbcf5688..c382f870e259 100644
--- a/include/tools/color.hxx
+++ b/include/tools/color.hxx
@@ -161,6 +161,16 @@ public:
void IncreaseLuminance(sal_uInt8 cLumInc);
void DecreaseLuminance(sal_uInt8 cLumDec);
+ /**
+ * Apply tint or shade to a color.
+ *
+ * The input value is the percentage (in 100th of percent) of how much the
+ * color changes towards the black (shade) or white (tint). If the value
+ * is positive, the color is tinted, if the value is negative, the color is
+ * shaded.
+ **/
+ void ApplyTintOrShade(sal_Int16 n100thPercent);
+
void DecreaseContrast(sal_uInt8 cContDec);
void Invert();
diff --git a/tools/source/generic/color.cxx b/tools/source/generic/color.cxx
index 741cb159a4bf..453feae9c2e1 100644
--- a/tools/source/generic/color.cxx
+++ b/tools/source/generic/color.cxx
@@ -29,6 +29,8 @@
#include <tools/rcid.h>
#include <tools/resid.hxx>
#include <tools/rc.h>
+#include <tools/helpers.hxx>
+#include <basegfx/color/bcolortools.hxx>
static inline long _FRound( double fVal )
{
@@ -113,6 +115,34 @@ bool Color::IsBright() const
return GetLuminance() >= 245;
}
+void Color::ApplyTintOrShade(sal_Int16 n100thPercent)
+{
+ if (n100thPercent > 0)
+ {
+ basegfx::BColor aBColor = basegfx::tools::rgb2hsl(getBColor());
+
+ double fFactor = std::abs(n100thPercent) / 10000.0;
+ aBColor.setBlue(aBColor.getBlue() * fFactor + (100.0 - aBColor.getBlue()));
+ aBColor = basegfx::tools::hsl2rgb(aBColor);
+
+ SetRed(sal_uInt8((aBColor.getRed() * 255.0) + 0.5));
+ SetGreen(sal_uInt8((aBColor.getGreen() * 255.0) + 0.5));
+ SetBlue(sal_uInt8((aBColor.getBlue() * 255.0) + 0.5));
+ }
+ else if (n100thPercent < 0)
+ {
+ basegfx::BColor aBColor = basegfx::tools::rgb2hsl(getBColor());
+
+ double fFactor = std::abs(n100thPercent) / 10000.0;
+ aBColor.setBlue(aBColor.getBlue() * fFactor);
+ aBColor = basegfx::tools::hsl2rgb(aBColor);
+
+ SetRed(sal_uInt8((aBColor.getRed() * 255.0) + 0.5));
+ SetGreen(sal_uInt8((aBColor.getGreen() * 255.0) + 0.5));
+ SetBlue(sal_uInt8((aBColor.getBlue() * 255.0) + 0.5));
+ }
+}
+
// color space conversion
void Color::RGBtoHSB( sal_uInt16& nHue, sal_uInt16& nSat, sal_uInt16& nBri ) const