1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_BASEGFX_COLOR_BCOLOR_HXX
21 #define INCLUDED_BASEGFX_COLOR_BCOLOR_HXX
23 #include <basegfx/tuple/b3dtuple.hxx>
24 #include <com/sun/star/uno/Reference.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
27 #include <basegfx/basegfxdllapi.h>
32 namespace com
{ namespace sun
{ namespace star
{ namespace rendering
{
40 /** Base Color class with three double values
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.
48 class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColor
: public B3DTuple
51 /** Create a Color with red, green and blue components from [0.0 to 1.0]
53 The color is initialized to (0.0, 0.0, 0.0)
64 These parameters are used to initialize the red, green and blue intensities of the color
66 BColor(double fRed
, double fGreen
, double fBlue
)
67 : B3DTuple(fRed
, fGreen
, fBlue
)
73 The parameter is used to initialize the red, green and blue intensities of the color
75 explicit BColor(double fLuminosity
)
76 : B3DTuple(fLuminosity
, fLuminosity
, fLuminosity
)
79 /** Create a copy of a Color
82 The Color which will be copied.
84 BColor(const BColor
& rVec
)
88 /** constructor with tuple to allow copy-constructing
89 from B3DTuple-based classes
91 BColor(const ::basegfx::B3DTuple
& rTuple
)
99 double getRed() const { return mfX
; }
100 double getGreen() const { return mfY
; }
101 double getBlue() const { return mfZ
; }
104 void setRed(double fNew
) { mfX
= fNew
; }
105 void setGreen(double fNew
) { mfY
= fNew
; }
106 void setBlue(double fNew
) { mfZ
= fNew
; }
108 /** *=operator to allow usage from BColor, too
110 BColor
& operator*=( const BColor
& rPnt
)
118 /** *=operator to allow usage from BColor, too
120 BColor
& operator*=(double t
)
128 /** assignment operator to allow assigning the results
129 of B3DTuple calculations
131 BColor
& operator=( const ::basegfx::B3DTuple
& rVec
)
139 // blend to another color using luminance
140 void blend(const BColor
& rColor
)
142 const double fLuminance(luminance());
143 mfX
= rColor
.getRed() * fLuminance
;
144 mfY
= rColor
.getGreen() * fLuminance
;
145 mfZ
= rColor
.getBlue() * fLuminance
;
149 double luminance() const
151 const double fRedWeight(77.0 / 256.0); // 0.30
152 const double fGreenWeight(151.0 / 256.0); // 0.59
153 const double fBlueWeight(28.0 / 256.0); // 0.11
155 return (mfX
* fRedWeight
+ mfY
* fGreenWeight
+ mfZ
* fBlueWeight
);
158 // distances in color space
159 double getDistanceRed(const BColor
& rColor
) const { return (getRed() > rColor
.getRed() ? getRed() - rColor
.getRed() : rColor
.getRed() - getRed()); }
160 double getDistanceGreen(const BColor
& rColor
) const { return (getGreen() > rColor
.getGreen() ? getGreen() - rColor
.getGreen() : rColor
.getGreen() - getGreen()); }
161 double getDistanceBlue(const BColor
& rColor
) const { return (getBlue() > rColor
.getBlue() ? getBlue() - rColor
.getBlue() : rColor
.getBlue() - getBlue()); }
163 double getDistance(const BColor
& rColor
) const
165 const double fDistR(getDistanceRed(rColor
));
166 const double fDistG(getDistanceGreen(rColor
));
167 const double fDistB(getDistanceBlue(rColor
));
169 return sqrt(fDistR
* fDistR
+ fDistG
* fDistG
+ fDistB
* fDistB
);
172 double getMinimumDistance(const BColor
& rColor
) const
174 const double fDistR(getDistanceRed(rColor
));
175 const double fDistG(getDistanceGreen(rColor
));
176 const double fDistB(getDistanceBlue(rColor
));
178 double fRetval(fDistR
< fDistG
? fDistR
: fDistG
);
179 return (fRetval
< fDistB
? fRetval
: fDistB
);
182 double getMaximumDistance(const BColor
& rColor
) const
184 const double fDistR(getDistanceRed(rColor
));
185 const double fDistG(getDistanceGreen(rColor
));
186 const double fDistB(getDistanceBlue(rColor
));
188 double fRetval(fDistR
> fDistG
? fDistR
: fDistG
);
189 return (fRetval
> fDistB
? fRetval
: fDistB
);
192 // clamp color to [0.0..1.0] values in all three intensity components
195 mfX
= basegfx::clamp(mfX
, 0.0, 1.0);
196 mfY
= basegfx::clamp(mfY
, 0.0, 1.0);
197 mfZ
= basegfx::clamp(mfZ
, 0.0, 1.0);
209 static const BColor
& getEmptyBColor()
211 return (const BColor
&) ::basegfx::B3DTuple::getEmptyTuple();
214 com::sun::star::uno::Sequence
< double > colorToDoubleSequence(const com::sun::star::uno::Reference
< com::sun::star::rendering::XGraphicDevice
>& /*xGraphicDevice*/) const
216 com::sun::star::uno::Sequence
< double > aRet(4);
217 double* pRet
= aRet
.getArray();
227 } // end of namespace basegfx
229 #endif // INCLUDED_BASEGFX_COLOR_BCOLOR_HXX
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */