1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _BGFX_COLOR_BCOLOR_HXX
25 #define _BGFX_COLOR_BCOLOR_HXX
26 
27 #include <basegfx/tuple/b3dtuple.hxx>
28 #include <com/sun/star/uno/Reference.hxx>
29 #include <com/sun/star/uno/Sequence.hxx>
30 #include <vector>
31 
32 //////////////////////////////////////////////////////////////////////////////
33 // predeclarations
34 
35 namespace com { namespace sun { namespace star { namespace rendering {
36     class XGraphicDevice;
37 }}}}
38 
39 //////////////////////////////////////////////////////////////////////////////
40 
41 namespace basegfx
42 {
43 	/** Base Color class with three double values
44 
45 		This class derives all operators and common handling for
46 		a 3D data class from B3DTuple. All necessary extensions
47 		which are special for colors will be added here.
48 
49 		@see B3DTuple
50 	*/
51 	class BColor : public B3DTuple
52 	{
53 	public:
54 		/**	Create a Color with red, green and blue components from [0.0 to 1.0]
55 
56         	The color is initialized to (0.0, 0.0, 0.0)
57 		*/
BColor()58 		BColor()
59 		:	B3DTuple()
60 		{}
61 
62 		/**	Create a 3D Color
63 
64 			@param fRed
65 			@param fGreen
66 			@param fBlue
67 			These parameters are used to initialize the red, green and blue intensities of the color
68 		*/
BColor(double fRed,double fGreen,double fBlue)69 		BColor(double fRed, double fGreen, double fBlue)
70 		:	B3DTuple(fRed, fGreen, fBlue)
71 		{}
72 
73 		/**	Create a 3D Color
74 
75 			@param fLuminosity
76 			The parameter is used to initialize the red, green and blue intensities of the color
77 		*/
BColor(double fLuminosity)78 		BColor(double fLuminosity)
79 		:	B3DTuple(fLuminosity, fLuminosity, fLuminosity)
80 		{}
81 
82 		/**	Create a copy of a Color
83 
84 			@param rVec
85 			The Color which will be copied.
86 		*/
BColor(const BColor & rVec)87 		BColor(const BColor& rVec)
88 		:	B3DTuple(rVec)
89 		{}
90 
91 		/** constructor with tuple to allow copy-constructing
92 			from B3DTuple-based classes
93 		*/
BColor(const::basegfx::B3DTuple & rTuple)94 		BColor(const ::basegfx::B3DTuple& rTuple)
95 		:	B3DTuple(rTuple)
96 		{}
97 
~BColor()98 		~BColor()
99 		{}
100 
101 		// data access read
getRed() const102 		double getRed() const { return mfX; }
getGreen() const103 		double getGreen() const { return mfY; }
getBlue() const104 		double getBlue() const { return mfZ; }
105 
106 		// data access write
setRed(double fNew)107 		void setRed(double fNew) { mfX = fNew; }
setGreen(double fNew)108 		void setGreen(double fNew) { mfY = fNew; }
setBlue(double fNew)109 		void setBlue(double fNew) { mfZ = fNew; }
110 
111 		/** *=operator to allow usage from BColor, too
112 		*/
operator *=(const BColor & rPnt)113 		BColor& operator*=( const BColor& rPnt )
114 		{
115 			mfX *= rPnt.mfX;
116 			mfY *= rPnt.mfY;
117 			mfZ *= rPnt.mfZ;
118 			return *this;
119 		}
120 
121 		/** *=operator to allow usage from BColor, too
122 		*/
operator *=(double t)123 		BColor& operator*=(double t)
124 		{
125 			mfX *= t;
126 			mfY *= t;
127 			mfZ *= t;
128 			return *this;
129 		}
130 
131 		/** assignment operator to allow assigning the results
132 			of B3DTuple calculations
133 		*/
operator =(const::basegfx::B3DTuple & rVec)134 		BColor& operator=( const ::basegfx::B3DTuple& rVec )
135 		{
136 			mfX = rVec.getX();
137 			mfY = rVec.getY();
138 			mfZ = rVec.getZ();
139 			return *this;
140 		}
141 
142 		// blend to another color using luminance
blend(const BColor & rColor)143 		void blend(const BColor& rColor)
144 		{
145 			const double fLuminance(luminance());
146 			mfX = rColor.getRed() * fLuminance;
147 			mfY = rColor.getGreen() * fLuminance;
148 			mfZ = rColor.getBlue() * fLuminance;
149 		}
150 
151 		// luminance
luminance() const152 		double luminance() const
153 		{
154 			const double fRedWeight(77.0 / 256.0);      // 0.30
155 			const double fGreenWeight(151.0 / 256.0);   // 0.59
156 			const double fBlueWeight(28.0 / 256.0);     // 0.11
157 
158 			return (mfX * fRedWeight + mfY * fGreenWeight + mfZ * fBlueWeight);
159 		}
160 
161 		// distances in color space
getDistanceRed(const BColor & rColor) const162 		double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
getDistanceGreen(const BColor & rColor) const163 		double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
getDistanceBlue(const BColor & rColor) const164 		double getDistanceBlue(const BColor& rColor) const { return (getBlue() > rColor.getBlue() ? getBlue() - rColor.getBlue() : rColor.getBlue() - getBlue()); }
165 
getDistance(const BColor & rColor) const166 		double getDistance(const BColor& rColor) const
167 		{
168 			const double fDistR(getDistanceRed(rColor));
169 			const double fDistG(getDistanceGreen(rColor));
170 			const double fDistB(getDistanceBlue(rColor));
171 
172 			return sqrt(fDistR * fDistR + fDistG * fDistG + fDistB * fDistB);
173 		}
174 
getMinimumDistance(const BColor & rColor) const175 		double getMinimumDistance(const BColor& rColor) const
176 		{
177 			const double fDistR(getDistanceRed(rColor));
178 			const double fDistG(getDistanceGreen(rColor));
179 			const double fDistB(getDistanceBlue(rColor));
180 
181 			double fRetval(fDistR < fDistG ? fDistR : fDistG);
182 			return (fRetval < fDistB ? fRetval : fDistB);
183 		}
184 
getMaximumDistance(const BColor & rColor) const185 		double getMaximumDistance(const BColor& rColor) const
186 		{
187 			const double fDistR(getDistanceRed(rColor));
188 			const double fDistG(getDistanceGreen(rColor));
189 			const double fDistB(getDistanceBlue(rColor));
190 
191 			double fRetval(fDistR > fDistG ? fDistR : fDistG);
192 			return (fRetval > fDistB ? fRetval : fDistB);
193 		}
194 
195         // clamp color to [0.0..1.0] values in all three intensity components
clamp()196         BColor& clamp()
197         {
198             mfX = basegfx::clamp(mfX, 0.0, 1.0);
199             mfY = basegfx::clamp(mfY, 0.0, 1.0);
200             mfZ = basegfx::clamp(mfZ, 0.0, 1.0);
201             return *this;
202         }
203 
invert()204         BColor& invert()
205         {
206             mfX = 1.0 - mfX;
207             mfY = 1.0 - mfY;
208             mfZ = 1.0 - mfZ;
209             return *this;
210         }
211 
getEmptyBColor()212 		static const BColor& getEmptyBColor()
213 		{
214 			return (const BColor&) ::basegfx::B3DTuple::getEmptyTuple();
215 		}
216 
colorToDoubleSequence(const com::sun::star::uno::Reference<com::sun::star::rendering::XGraphicDevice> &) const217         com::sun::star::uno::Sequence< double > colorToDoubleSequence(const com::sun::star::uno::Reference< com::sun::star::rendering::XGraphicDevice >& /*xGraphicDevice*/) const
218         {
219             com::sun::star::uno::Sequence< double > aRet(4);
220             double* pRet = aRet.getArray();
221 
222             pRet[0] = mfX;
223             pRet[1] = mfY;
224             pRet[2] = mfZ;
225             pRet[3] = 1.0;
226 
227             return aRet;
228         }
229 	};
230 } // end of namespace basegfx
231 
232 #endif /* _BGFX_COLOR_BCOLOR_HXX */
233 
234 //////////////////////////////////////////////////////////////////////////////
235 // eof
236