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 : 264 : 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 : 264 : BColor()
65 : 264 : : B3DTuple()
66 : 264 : {}
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 : 489 : BColor(double fRed, double fGreen, double fBlue)
76 : 489 : : B3DTuple(fRed, fGreen, fBlue)
77 : 489 : {}
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 : : explicit BColor(double fLuminosity)
85 : : : B3DTuple(fLuminosity, fLuminosity, fLuminosity)
86 : : {}
87 : :
88 : : /** Create a copy of a Color
89 : :
90 : : @param rVec
91 : : The Color which will be copied.
92 : : */
93 : 18386 : BColor(const BColor& rVec)
94 : 18386 : : B3DTuple(rVec)
95 : 18386 : {}
96 : :
97 : : /** constructor with tuple to allow copy-constructing
98 : : from B3DTuple-based classes
99 : : */
100 : 0 : BColor(const ::basegfx::B3DTuple& rTuple)
101 : 0 : : B3DTuple(rTuple)
102 : 0 : {}
103 : :
104 : 792 : ~BColor()
105 : 792 : {}
106 : :
107 : : // data access read
108 : 1281 : double getRed() const { return mfX; }
109 : 1281 : double getGreen() const { return mfY; }
110 : 1281 : double getBlue() const { return mfZ; }
111 : :
112 : : // data access write
113 : : void setRed(double fNew) { mfX = fNew; }
114 : : void setGreen(double fNew) { mfY = fNew; }
115 : : void setBlue(double fNew) { mfZ = fNew; }
116 : :
117 : : /** *=operator to allow usage from BColor, too
118 : : */
119 : : BColor& operator*=( const BColor& rPnt )
120 : : {
121 : : mfX *= rPnt.mfX;
122 : : mfY *= rPnt.mfY;
123 : : mfZ *= rPnt.mfZ;
124 : : 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 : : BColor& operator=( const ::basegfx::B3DTuple& rVec )
141 : : {
142 : : mfX = rVec.getX();
143 : : mfY = rVec.getY();
144 : : mfZ = rVec.getZ();
145 : : 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 : 0 : double luminance() const
159 : : {
160 : 0 : const double fRedWeight(77.0 / 256.0);
161 : 0 : const double fGreenWeight(151.0 / 256.0);
162 : 0 : const double fBlueWeight(28.0 / 256.0);
163 : :
164 : 0 : return (mfX * fRedWeight + mfY * fGreenWeight + mfZ * fBlueWeight);
165 : : }
166 : :
167 : : // distances in color space
168 : : double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
169 : : double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
170 : : 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 : : double getMaximumDistance(const BColor& rColor) const
192 : : {
193 : : const double fDistR(getDistanceRed(rColor));
194 : : const double fDistG(getDistanceGreen(rColor));
195 : : const double fDistB(getDistanceBlue(rColor));
196 : :
197 : : double fRetval(fDistR > fDistG ? fDistR : fDistG);
198 : : return (fRetval > fDistB ? fRetval : fDistB);
199 : : }
200 : :
201 : : // clamp color to [0.0..1.0] values in all three intensity components
202 : : void clamp()
203 : : {
204 : : mfX = basegfx::clamp(mfX, 0.0, 1.0);
205 : : mfY = basegfx::clamp(mfY, 0.0, 1.0);
206 : : mfZ = basegfx::clamp(mfZ, 0.0, 1.0);
207 : : }
208 : :
209 : : void invert()
210 : : {
211 : : mfX = 1.0 - mfX;
212 : : mfY = 1.0 - mfY;
213 : : mfZ = 1.0 - mfZ;
214 : : }
215 : :
216 : 66 : static const BColor& getEmptyBColor()
217 : : {
218 : 66 : return (const BColor&) ::basegfx::B3DTuple::getEmptyTuple();
219 : : }
220 : :
221 : : com::sun::star::uno::Sequence< double > colorToDoubleSequence(const com::sun::star::uno::Reference< com::sun::star::rendering::XGraphicDevice >& /*xGraphicDevice*/) const
222 : : {
223 : : com::sun::star::uno::Sequence< double > aRet(4);
224 : : double* pRet = aRet.getArray();
225 : :
226 : : pRet[0] = mfX;
227 : : pRet[1] = mfY;
228 : : pRet[2] = mfZ;
229 : : pRet[3] = 1.0;
230 : :
231 : : 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: */
|