Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _BGFX_COLOR_BCOLOR_HXX
30 : : #define _BGFX_COLOR_BCOLOR_HXX
31 : :
32 : : #include <basegfx/tuple/b3dtuple.hxx>
33 : : #include <com/sun/star/uno/Reference.hxx>
34 : : #include <com/sun/star/uno/Sequence.hxx>
35 : : #include <vector>
36 : : #include <basegfx/basegfxdllapi.h>
37 : :
38 : : //////////////////////////////////////////////////////////////////////////////
39 : : // predeclarations
40 : :
41 : : namespace com { namespace sun { namespace star { namespace rendering {
42 : : class XGraphicDevice;
43 : : }}}}
44 : :
45 : : //////////////////////////////////////////////////////////////////////////////
46 : :
47 : : namespace basegfx
48 : : {
49 : : /** Base Color class with three double values
50 : :
51 : : This class derives all operators and common handling for
52 : : a 3D data class from B3DTuple. All necessary extensions
53 : : which are special for colors will be added here.
54 : :
55 : : @see B3DTuple
56 : : */
57 : 2258729 : class BASEGFX_DLLPUBLIC BColor : public B3DTuple
58 : : {
59 : : public:
60 : : /** Create a Color with red, green and blue components from [0.0 to 1.0]
61 : :
62 : : The color is initialized to (0.0, 0.0, 0.0)
63 : : */
64 : 193654 : BColor()
65 : 193654 : : B3DTuple()
66 : 193654 : {}
67 : :
68 : : /** Create a 3D Color
69 : :
70 : : @param fRed
71 : : @param fGreen
72 : : @param fBlue
73 : : These parameters are used to initialize the red, green and blue intensities of the color
74 : : */
75 : 311963 : BColor(double fRed, double fGreen, double fBlue)
76 : 311963 : : B3DTuple(fRed, fGreen, fBlue)
77 : 311963 : {}
78 : :
79 : : /** Create a 3D Color
80 : :
81 : : @param fLuminosity
82 : : The parameter is used to initialize the red, green and blue intensities of the color
83 : : */
84 : 595 : explicit BColor(double fLuminosity)
85 : 595 : : B3DTuple(fLuminosity, fLuminosity, fLuminosity)
86 : 595 : {}
87 : :
88 : : /** Create a copy of a Color
89 : :
90 : : @param rVec
91 : : The Color which will be copied.
92 : : */
93 : 1508426 : BColor(const BColor& rVec)
94 : 1508426 : : B3DTuple(rVec)
95 : 1508426 : {}
96 : :
97 : : /** constructor with tuple to allow copy-constructing
98 : : from B3DTuple-based classes
99 : : */
100 : 181100 : BColor(const ::basegfx::B3DTuple& rTuple)
101 : 181100 : : B3DTuple(rTuple)
102 : 181100 : {}
103 : :
104 : 2213449 : ~BColor()
105 : 2213449 : {}
106 : :
107 : : // data access read
108 : 2657241 : double getRed() const { return mfX; }
109 : 2657241 : double getGreen() const { return mfY; }
110 : 2657241 : double getBlue() const { return mfZ; }
111 : :
112 : : // data access write
113 : 136520 : void setRed(double fNew) { mfX = fNew; }
114 : 136520 : void setGreen(double fNew) { mfY = fNew; }
115 : 136520 : void setBlue(double fNew) { mfZ = fNew; }
116 : :
117 : : /** *=operator to allow usage from BColor, too
118 : : */
119 : 47800 : BColor& operator*=( const BColor& rPnt )
120 : : {
121 : 47800 : mfX *= rPnt.mfX;
122 : 47800 : mfY *= rPnt.mfY;
123 : 47800 : mfZ *= rPnt.mfZ;
124 : 47800 : return *this;
125 : : }
126 : :
127 : : /** *=operator to allow usage from BColor, too
128 : : */
129 : : BColor& operator*=(double t)
130 : : {
131 : : mfX *= t;
132 : : mfY *= t;
133 : : mfZ *= t;
134 : : return *this;
135 : : }
136 : :
137 : : /** assignment operator to allow assigning the results
138 : : of B3DTuple calculations
139 : : */
140 : 136536 : BColor& operator=( const ::basegfx::B3DTuple& rVec )
141 : : {
142 : 136536 : mfX = rVec.getX();
143 : 136536 : mfY = rVec.getY();
144 : 136536 : mfZ = rVec.getZ();
145 : 136536 : return *this;
146 : : }
147 : :
148 : : // blend to another color using luminance
149 : : void blend(const BColor& rColor)
150 : : {
151 : : const double fLuminance(luminance());
152 : : mfX = rColor.getRed() * fLuminance;
153 : : mfY = rColor.getGreen() * fLuminance;
154 : : mfZ = rColor.getBlue() * fLuminance;
155 : : }
156 : :
157 : : // luminance
158 : 136718 : double luminance() const
159 : : {
160 : 136718 : const double fRedWeight(77.0 / 256.0);
161 : 136718 : const double fGreenWeight(151.0 / 256.0);
162 : 136718 : const double fBlueWeight(28.0 / 256.0);
163 : :
164 : 136718 : return (mfX * fRedWeight + mfY * fGreenWeight + mfZ * fBlueWeight);
165 : : }
166 : :
167 : : // distances in color space
168 [ + + ]: 1583 : double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
169 [ + + ]: 1583 : double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
170 [ + + ]: 1583 : double getDistanceBlue(const BColor& rColor) const { return (getBlue() > rColor.getBlue() ? getBlue() - rColor.getBlue() : rColor.getBlue() - getBlue()); }
171 : :
172 : : double getDistance(const BColor& rColor) const
173 : : {
174 : : const double fDistR(getDistanceRed(rColor));
175 : : const double fDistG(getDistanceGreen(rColor));
176 : : const double fDistB(getDistanceBlue(rColor));
177 : :
178 : : return sqrt(fDistR * fDistR + fDistG * fDistG + fDistB * fDistB);
179 : : }
180 : :
181 : : double getMinimumDistance(const BColor& rColor) const
182 : : {
183 : : const double fDistR(getDistanceRed(rColor));
184 : : const double fDistG(getDistanceGreen(rColor));
185 : : const double fDistB(getDistanceBlue(rColor));
186 : :
187 : : double fRetval(fDistR < fDistG ? fDistR : fDistG);
188 : : return (fRetval < fDistB ? fRetval : fDistB);
189 : : }
190 : :
191 : 1583 : double getMaximumDistance(const BColor& rColor) const
192 : : {
193 : 1583 : const double fDistR(getDistanceRed(rColor));
194 : 1583 : const double fDistG(getDistanceGreen(rColor));
195 : 1583 : const double fDistB(getDistanceBlue(rColor));
196 : :
197 [ + + ]: 1583 : double fRetval(fDistR > fDistG ? fDistR : fDistG);
198 [ - + ]: 1583 : return (fRetval > fDistB ? fRetval : fDistB);
199 : : }
200 : :
201 : : // clamp color to [0.0..1.0] values in all three intensity components
202 : 2183881 : void clamp()
203 : : {
204 : 2183881 : mfX = basegfx::clamp(mfX, 0.0, 1.0);
205 : 2183881 : mfY = basegfx::clamp(mfY, 0.0, 1.0);
206 : 2183881 : mfZ = basegfx::clamp(mfZ, 0.0, 1.0);
207 : 2183881 : }
208 : :
209 : 0 : void invert()
210 : : {
211 : 0 : mfX = 1.0 - mfX;
212 : 0 : mfY = 1.0 - mfY;
213 : 0 : mfZ = 1.0 - mfZ;
214 : 0 : }
215 : :
216 : : static const BColor& getEmptyBColor()
217 : : {
218 : : return (const BColor&) ::basegfx::B3DTuple::getEmptyTuple();
219 : : }
220 : :
221 : 0 : com::sun::star::uno::Sequence< double > colorToDoubleSequence(const com::sun::star::uno::Reference< com::sun::star::rendering::XGraphicDevice >& /*xGraphicDevice*/) const
222 : : {
223 : 0 : com::sun::star::uno::Sequence< double > aRet(4);
224 [ # # ]: 0 : double* pRet = aRet.getArray();
225 : :
226 : 0 : pRet[0] = mfX;
227 : 0 : pRet[1] = mfY;
228 : 0 : pRet[2] = mfZ;
229 : 0 : pRet[3] = 1.0;
230 : :
231 : 0 : return aRet;
232 : : }
233 : : };
234 : : } // end of namespace basegfx
235 : :
236 : : #endif /* _BGFX_COLOR_BCOLOR_HXX */
237 : :
238 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|