xref: /trunk/main/basegfx/inc/basegfx/color/bcolor.hxx (revision 914d351e5f5b84e4342a86d6ab8d4aca7308b9bd)
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 #include <basegfx/basegfxdllapi.h>
32 
33 //////////////////////////////////////////////////////////////////////////////
34 // predeclarations
35 
36 namespace com { namespace sun { namespace star { namespace rendering {
37     class XGraphicDevice;
38 }}}}
39 
40 //////////////////////////////////////////////////////////////////////////////
41 
42 namespace basegfx
43 {
44     /** Base Color class with three double values
45 
46         This class derives all operators and common handling for
47         a 3D data class from B3DTuple. All necessary extensions
48         which are special for colors will be added here.
49 
50         @see B3DTuple
51     */
52     class BASEGFX_DLLPUBLIC BColor : public B3DTuple
53     {
54     public:
55         /** Create a Color with red, green and blue components from [0.0 to 1.0]
56 
57             The color is initialized to (0.0, 0.0, 0.0)
58         */
BColor()59         BColor()
60         :   B3DTuple()
61         {}
62 
63         /** Create a 3D Color
64 
65             @param fRed
66             @param fGreen
67             @param fBlue
68             These parameters are used to initialize the red, green and blue intensities of the color
69         */
BColor(double fRed,double fGreen,double fBlue)70         BColor(double fRed, double fGreen, double fBlue)
71         :   B3DTuple(fRed, fGreen, fBlue)
72         {}
73 
74         /** Create a 3D Color
75 
76             @param fLuminosity
77             The parameter is used to initialize the red, green and blue intensities of the color
78         */
BColor(double fLuminosity)79         BColor(double fLuminosity)
80         :   B3DTuple(fLuminosity, fLuminosity, fLuminosity)
81         {}
82 
83         /** Create a copy of a Color
84 
85             @param rVec
86             The Color which will be copied.
87         */
BColor(const BColor & rVec)88         BColor(const BColor& rVec)
89         :   B3DTuple(rVec)
90         {}
91 
92         /** constructor with tuple to allow copy-constructing
93             from B3DTuple-based classes
94         */
BColor(const::basegfx::B3DTuple & rTuple)95         BColor(const ::basegfx::B3DTuple& rTuple)
96         :   B3DTuple(rTuple)
97         {}
98 
~BColor()99         ~BColor()
100         {}
101 
102         // data access read
getRed() const103         double getRed() const { return mfX; }
getGreen() const104         double getGreen() const { return mfY; }
getBlue() const105         double getBlue() const { return mfZ; }
106 
107         // data access write
setRed(double fNew)108         void setRed(double fNew) { mfX = fNew; }
setGreen(double fNew)109         void setGreen(double fNew) { mfY = fNew; }
setBlue(double fNew)110         void setBlue(double fNew) { mfZ = fNew; }
111 
112         /** *=operator to allow usage from BColor, too
113         */
operator *=(const BColor & rPnt)114         BColor& operator*=( const BColor& rPnt )
115         {
116             mfX *= rPnt.mfX;
117             mfY *= rPnt.mfY;
118             mfZ *= rPnt.mfZ;
119             return *this;
120         }
121 
122         /** *=operator to allow usage from BColor, too
123         */
operator *=(double t)124         BColor& operator*=(double t)
125         {
126             mfX *= t;
127             mfY *= t;
128             mfZ *= t;
129             return *this;
130         }
131 
132         /** assignment operator to allow assigning the results
133             of B3DTuple calculations
134         */
operator =(const::basegfx::B3DTuple & rVec)135         BColor& operator=( const ::basegfx::B3DTuple& rVec )
136         {
137             mfX = rVec.getX();
138             mfY = rVec.getY();
139             mfZ = rVec.getZ();
140             return *this;
141         }
142 
143         // blend to another color using luminance
blend(const BColor & rColor)144         void blend(const BColor& rColor)
145         {
146             const double fLuminance(luminance());
147             mfX = rColor.getRed() * fLuminance;
148             mfY = rColor.getGreen() * fLuminance;
149             mfZ = rColor.getBlue() * fLuminance;
150         }
151 
152         // luminance
luminance() const153         double luminance() const
154         {
155             const double fRedWeight(77.0 / 256.0);      // 0.30
156             const double fGreenWeight(151.0 / 256.0);   // 0.59
157             const double fBlueWeight(28.0 / 256.0);     // 0.11
158 
159             return (mfX * fRedWeight + mfY * fGreenWeight + mfZ * fBlueWeight);
160         }
161 
162         // distances in color space
getDistanceRed(const BColor & rColor) const163         double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
getDistanceGreen(const BColor & rColor) const164         double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
getDistanceBlue(const BColor & rColor) const165         double getDistanceBlue(const BColor& rColor) const { return (getBlue() > rColor.getBlue() ? getBlue() - rColor.getBlue() : rColor.getBlue() - getBlue()); }
166 
getDistance(const BColor & rColor) const167         double getDistance(const BColor& rColor) const
168         {
169             const double fDistR(getDistanceRed(rColor));
170             const double fDistG(getDistanceGreen(rColor));
171             const double fDistB(getDistanceBlue(rColor));
172 
173             return sqrt(fDistR * fDistR + fDistG * fDistG + fDistB * fDistB);
174         }
175 
getMinimumDistance(const BColor & rColor) const176         double getMinimumDistance(const BColor& rColor) const
177         {
178             const double fDistR(getDistanceRed(rColor));
179             const double fDistG(getDistanceGreen(rColor));
180             const double fDistB(getDistanceBlue(rColor));
181 
182             double fRetval(fDistR < fDistG ? fDistR : fDistG);
183             return (fRetval < fDistB ? fRetval : fDistB);
184         }
185 
getMaximumDistance(const BColor & rColor) const186         double getMaximumDistance(const BColor& rColor) const
187         {
188             const double fDistR(getDistanceRed(rColor));
189             const double fDistG(getDistanceGreen(rColor));
190             const double fDistB(getDistanceBlue(rColor));
191 
192             double fRetval(fDistR > fDistG ? fDistR : fDistG);
193             return (fRetval > fDistB ? fRetval : fDistB);
194         }
195 
196         // clamp color to [0.0..1.0] values in all three intensity components
clamp()197         BColor& clamp()
198         {
199             mfX = basegfx::clamp(mfX, 0.0, 1.0);
200             mfY = basegfx::clamp(mfY, 0.0, 1.0);
201             mfZ = basegfx::clamp(mfZ, 0.0, 1.0);
202             return *this;
203         }
204 
invert()205         BColor& invert()
206         {
207             mfX = 1.0 - mfX;
208             mfY = 1.0 - mfY;
209             mfZ = 1.0 - mfZ;
210             return *this;
211         }
212 
getEmptyBColor()213         static const BColor& getEmptyBColor()
214         {
215             return (const BColor&) ::basegfx::B3DTuple::getEmptyTuple();
216         }
217 
colorToDoubleSequence(const com::sun::star::uno::Reference<com::sun::star::rendering::XGraphicDevice> &) const218         com::sun::star::uno::Sequence< double > colorToDoubleSequence(const com::sun::star::uno::Reference< com::sun::star::rendering::XGraphicDevice >& /*xGraphicDevice*/) const
219         {
220             com::sun::star::uno::Sequence< double > aRet(4);
221             double* pRet = aRet.getArray();
222 
223             pRet[0] = mfX;
224             pRet[1] = mfY;
225             pRet[2] = mfZ;
226             pRet[3] = 1.0;
227 
228             return aRet;
229         }
230     };
231 } // end of namespace basegfx
232 
233 #endif /* _BGFX_COLOR_BCOLOR_HXX */
234 
235 //////////////////////////////////////////////////////////////////////////////
236 // eof
237