Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef _BGFX_COLOR_BCOLOR_HXX
21 : #define _BGFX_COLOR_BCOLOR_HXX
22 :
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>
28 :
29 : //////////////////////////////////////////////////////////////////////////////
30 : // predeclarations
31 :
32 : namespace com { namespace sun { namespace star { namespace rendering {
33 : class XGraphicDevice;
34 : }}}}
35 :
36 : //////////////////////////////////////////////////////////////////////////////
37 :
38 : namespace basegfx
39 : {
40 : /** Base Color class with three double values
41 :
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.
45 :
46 : @see B3DTuple
47 : */
48 23 : class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColor : public B3DTuple
49 : {
50 : public:
51 : /** Create a Color with red, green and blue components from [0.0 to 1.0]
52 :
53 : The color is initialized to (0.0, 0.0, 0.0)
54 : */
55 88 : BColor()
56 88 : : B3DTuple()
57 88 : {}
58 :
59 : /** Create a 3D Color
60 :
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
65 : */
66 2825 : BColor(double fRed, double fGreen, double fBlue)
67 2825 : : B3DTuple(fRed, fGreen, fBlue)
68 2825 : {}
69 :
70 : /** Create a 3D Color
71 :
72 : @param fLuminosity
73 : The parameter is used to initialize the red, green and blue intensities of the color
74 : */
75 2 : explicit BColor(double fLuminosity)
76 2 : : B3DTuple(fLuminosity, fLuminosity, fLuminosity)
77 2 : {}
78 :
79 : /** Create a copy of a Color
80 :
81 : @param rVec
82 : The Color which will be copied.
83 : */
84 8837 : BColor(const BColor& rVec)
85 8837 : : B3DTuple(rVec)
86 8837 : {}
87 :
88 : /** constructor with tuple to allow copy-constructing
89 : from B3DTuple-based classes
90 : */
91 19 : BColor(const ::basegfx::B3DTuple& rTuple)
92 19 : : B3DTuple(rTuple)
93 19 : {}
94 :
95 11591 : ~BColor()
96 11591 : {}
97 :
98 : // data access read
99 2676 : double getRed() const { return mfX; }
100 2676 : double getGreen() const { return mfY; }
101 2676 : double getBlue() const { return mfZ; }
102 :
103 : // data access write
104 0 : void setRed(double fNew) { mfX = fNew; }
105 0 : void setGreen(double fNew) { mfY = fNew; }
106 0 : void setBlue(double fNew) { mfZ = fNew; }
107 :
108 : /** *=operator to allow usage from BColor, too
109 : */
110 0 : BColor& operator*=( const BColor& rPnt )
111 : {
112 0 : mfX *= rPnt.mfX;
113 0 : mfY *= rPnt.mfY;
114 0 : mfZ *= rPnt.mfZ;
115 0 : return *this;
116 : }
117 :
118 : /** *=operator to allow usage from BColor, too
119 : */
120 : BColor& operator*=(double t)
121 : {
122 : mfX *= t;
123 : mfY *= t;
124 : mfZ *= t;
125 : return *this;
126 : }
127 :
128 : /** assignment operator to allow assigning the results
129 : of B3DTuple calculations
130 : */
131 0 : BColor& operator=( const ::basegfx::B3DTuple& rVec )
132 : {
133 0 : mfX = rVec.getX();
134 0 : mfY = rVec.getY();
135 0 : mfZ = rVec.getZ();
136 0 : return *this;
137 : }
138 :
139 : // blend to another color using luminance
140 : void blend(const BColor& rColor)
141 : {
142 : const double fLuminance(luminance());
143 : mfX = rColor.getRed() * fLuminance;
144 : mfY = rColor.getGreen() * fLuminance;
145 : mfZ = rColor.getBlue() * fLuminance;
146 : }
147 :
148 : // luminance
149 0 : double luminance() const
150 : {
151 0 : const double fRedWeight(77.0 / 256.0); // 0.30
152 0 : const double fGreenWeight(151.0 / 256.0); // 0.59
153 0 : const double fBlueWeight(28.0 / 256.0); // 0.11
154 :
155 0 : return (mfX * fRedWeight + mfY * fGreenWeight + mfZ * fBlueWeight);
156 : }
157 :
158 : // distances in color space
159 3 : double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
160 3 : double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
161 3 : double getDistanceBlue(const BColor& rColor) const { return (getBlue() > rColor.getBlue() ? getBlue() - rColor.getBlue() : rColor.getBlue() - getBlue()); }
162 :
163 0 : double getDistance(const BColor& rColor) const
164 : {
165 0 : const double fDistR(getDistanceRed(rColor));
166 0 : const double fDistG(getDistanceGreen(rColor));
167 0 : const double fDistB(getDistanceBlue(rColor));
168 :
169 0 : return sqrt(fDistR * fDistR + fDistG * fDistG + fDistB * fDistB);
170 : }
171 :
172 : double getMinimumDistance(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 : double fRetval(fDistR < fDistG ? fDistR : fDistG);
179 : return (fRetval < fDistB ? fRetval : fDistB);
180 : }
181 :
182 3 : double getMaximumDistance(const BColor& rColor) const
183 : {
184 3 : const double fDistR(getDistanceRed(rColor));
185 3 : const double fDistG(getDistanceGreen(rColor));
186 3 : const double fDistB(getDistanceBlue(rColor));
187 :
188 3 : double fRetval(fDistR > fDistG ? fDistR : fDistG);
189 3 : return (fRetval > fDistB ? fRetval : fDistB);
190 : }
191 :
192 : // clamp color to [0.0..1.0] values in all three intensity components
193 0 : void clamp()
194 : {
195 0 : mfX = basegfx::clamp(mfX, 0.0, 1.0);
196 0 : mfY = basegfx::clamp(mfY, 0.0, 1.0);
197 0 : mfZ = basegfx::clamp(mfZ, 0.0, 1.0);
198 0 : }
199 :
200 0 : void invert()
201 : {
202 0 : mfX = 1.0 - mfX;
203 0 : mfY = 1.0 - mfY;
204 0 : mfZ = 1.0 - mfZ;
205 0 : }
206 :
207 : static const BColor& getEmptyBColor()
208 : {
209 : return (const BColor&) ::basegfx::B3DTuple::getEmptyTuple();
210 : }
211 :
212 0 : com::sun::star::uno::Sequence< double > colorToDoubleSequence(const com::sun::star::uno::Reference< com::sun::star::rendering::XGraphicDevice >& /*xGraphicDevice*/) const
213 : {
214 0 : com::sun::star::uno::Sequence< double > aRet(4);
215 0 : double* pRet = aRet.getArray();
216 :
217 0 : pRet[0] = mfX;
218 0 : pRet[1] = mfY;
219 0 : pRet[2] = mfZ;
220 0 : pRet[3] = 1.0;
221 :
222 0 : return aRet;
223 : }
224 : };
225 : } // end of namespace basegfx
226 :
227 : #endif /* _BGFX_COLOR_BCOLOR_HXX */
228 :
229 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|