Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / include / basegfx / color / bcolor.hxx
blob457409b2cfde91c8a3d18a0ca27587c9cb6e344a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #ifndef INCLUDED_BASEGFX_COLOR_BCOLOR_HXX
21 #define INCLUDED_BASEGFX_COLOR_BCOLOR_HXX
23 #include <basegfx/tuple/b3dtuple.hxx>
24 #include <com/sun/star/uno/Reference.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <vector>
27 #include <basegfx/basegfxdllapi.h>
30 // predeclarations
32 namespace com { namespace sun { namespace star { namespace rendering {
33 class XGraphicDevice;
34 }}}}
38 namespace basegfx
40 /** Base Color class with three double values
42 This class derives all operators and common handling for
43 a 3D data class from B3DTuple. All necessary extensions
44 which are special for colors will be added here.
46 @see B3DTuple
48 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColor : public B3DTuple
50 public:
51 /** Create a Color with red, green and blue components from [0.0 to 1.0]
53 The color is initialized to (0.0, 0.0, 0.0)
55 BColor()
56 : B3DTuple()
59 /** Create a 3D Color
61 @param fRed
62 @param fGreen
63 @param fBlue
64 These parameters are used to initialize the red, green and blue intensities of the color
66 BColor(double fRed, double fGreen, double fBlue)
67 : B3DTuple(fRed, fGreen, fBlue)
70 /** Create a 3D Color
72 @param fLuminosity
73 The parameter is used to initialize the red, green and blue intensities of the color
75 explicit BColor(double fLuminosity)
76 : B3DTuple(fLuminosity, fLuminosity, fLuminosity)
79 /** Create a copy of a Color
81 @param rVec
82 The Color which will be copied.
84 BColor(const BColor& rVec)
85 : B3DTuple(rVec)
88 /** constructor with tuple to allow copy-constructing
89 from B3DTuple-based classes
91 BColor(const ::basegfx::B3DTuple& rTuple)
92 : B3DTuple(rTuple)
95 ~BColor()
98 // data access read
99 double getRed() const { return mfX; }
100 double getGreen() const { return mfY; }
101 double getBlue() const { return mfZ; }
103 // data access write
104 void setRed(double fNew) { mfX = fNew; }
105 void setGreen(double fNew) { mfY = fNew; }
106 void setBlue(double fNew) { mfZ = fNew; }
108 /** *=operator to allow usage from BColor, too
110 BColor& operator*=( const BColor& rPnt )
112 mfX *= rPnt.mfX;
113 mfY *= rPnt.mfY;
114 mfZ *= rPnt.mfZ;
115 return *this;
118 /** *=operator to allow usage from BColor, too
120 BColor& operator*=(double t)
122 mfX *= t;
123 mfY *= t;
124 mfZ *= t;
125 return *this;
128 /** assignment operator to allow assigning the results
129 of B3DTuple calculations
131 BColor& operator=( const ::basegfx::B3DTuple& rVec )
133 mfX = rVec.getX();
134 mfY = rVec.getY();
135 mfZ = rVec.getZ();
136 return *this;
139 // blend to another color using luminance
140 void blend(const BColor& rColor)
142 const double fLuminance(luminance());
143 mfX = rColor.getRed() * fLuminance;
144 mfY = rColor.getGreen() * fLuminance;
145 mfZ = rColor.getBlue() * fLuminance;
148 // luminance
149 double luminance() const
151 const double fRedWeight(77.0 / 256.0); // 0.30
152 const double fGreenWeight(151.0 / 256.0); // 0.59
153 const double fBlueWeight(28.0 / 256.0); // 0.11
155 return (mfX * fRedWeight + mfY * fGreenWeight + mfZ * fBlueWeight);
158 // distances in color space
159 double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
160 double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
161 double getDistanceBlue(const BColor& rColor) const { return (getBlue() > rColor.getBlue() ? getBlue() - rColor.getBlue() : rColor.getBlue() - getBlue()); }
163 double getDistance(const BColor& rColor) const
165 const double fDistR(getDistanceRed(rColor));
166 const double fDistG(getDistanceGreen(rColor));
167 const double fDistB(getDistanceBlue(rColor));
169 return sqrt(fDistR * fDistR + fDistG * fDistG + fDistB * fDistB);
172 double getMinimumDistance(const BColor& rColor) const
174 const double fDistR(getDistanceRed(rColor));
175 const double fDistG(getDistanceGreen(rColor));
176 const double fDistB(getDistanceBlue(rColor));
178 double fRetval(fDistR < fDistG ? fDistR : fDistG);
179 return (fRetval < fDistB ? fRetval : fDistB);
182 double getMaximumDistance(const BColor& rColor) const
184 const double fDistR(getDistanceRed(rColor));
185 const double fDistG(getDistanceGreen(rColor));
186 const double fDistB(getDistanceBlue(rColor));
188 double fRetval(fDistR > fDistG ? fDistR : fDistG);
189 return (fRetval > fDistB ? fRetval : fDistB);
192 // clamp color to [0.0..1.0] values in all three intensity components
193 BColor& clamp()
195 mfX = basegfx::clamp(mfX, 0.0, 1.0);
196 mfY = basegfx::clamp(mfY, 0.0, 1.0);
197 mfZ = basegfx::clamp(mfZ, 0.0, 1.0);
198 return *this;
201 BColor& invert()
203 mfX = 1.0 - mfX;
204 mfY = 1.0 - mfY;
205 mfZ = 1.0 - mfZ;
206 return *this;
209 static const BColor& getEmptyBColor()
211 return (const BColor&) ::basegfx::B3DTuple::getEmptyTuple();
214 com::sun::star::uno::Sequence< double > colorToDoubleSequence(const com::sun::star::uno::Reference< com::sun::star::rendering::XGraphicDevice >& /*xGraphicDevice*/) const
216 com::sun::star::uno::Sequence< double > aRet(4);
217 double* pRet = aRet.getArray();
219 pRet[0] = mfX;
220 pRet[1] = mfY;
221 pRet[2] = mfZ;
222 pRet[3] = 1.0;
224 return aRet;
227 } // end of namespace basegfx
229 #endif // INCLUDED_BASEGFX_COLOR_BCOLOR_HXX
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */