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 .
22 #include <sal/config.h>
27 #include <basegfx/tuple/b3dtuple.hxx>
28 #include <basegfx/basegfxdllapi.h>
32 /** Base Color class with three double values
34 This class derives all operators and common handling for
35 a 3D data class from B3DTuple. All necessary extensions
36 which are special for colors will be added here.
40 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColor
: public B3DTuple
43 /** Create a Color with red, green and blue components from [0.0 to 1.0]
45 The color is initialized to (0.0, 0.0, 0.0)
56 These parameters are used to initialize the red, green and blue intensities of the color
58 BColor(double fRed
, double fGreen
, double fBlue
)
59 : B3DTuple(fRed
, fGreen
, fBlue
)
65 The parameter is used to initialize the red, green and blue intensities of the color
67 explicit BColor(double fLuminosity
)
68 : B3DTuple(fLuminosity
, fLuminosity
, fLuminosity
)
71 /** constructor with tuple to allow copy-constructing
72 from B3DTuple-based classes
74 BColor(const ::basegfx::B3DTuple
& rTuple
)
79 double getRed() const { return mfX
; }
80 double getGreen() const { return mfY
; }
81 double getBlue() const { return mfZ
; }
84 void setRed(double fNew
) { mfX
= fNew
; }
85 void setGreen(double fNew
) { mfY
= fNew
; }
86 void setBlue(double fNew
) { mfZ
= fNew
; }
88 /** *=operator to allow usage from BColor, too
90 BColor
& operator*=( const BColor
& rPnt
)
98 /** *=operator to allow usage from BColor, too
100 BColor
& operator*=(double t
)
108 /** assignment operator to allow assigning the results
109 of B3DTuple calculations
111 BColor
& operator=( const ::basegfx::B3DTuple
& rVec
)
120 double luminance() const
122 const double fRedWeight(77.0 / 256.0); // 0.30
123 const double fGreenWeight(151.0 / 256.0); // 0.59
124 const double fBlueWeight(28.0 / 256.0); // 0.11
126 return (mfX
* fRedWeight
+ mfY
* fGreenWeight
+ mfZ
* fBlueWeight
);
129 // distances in color space
130 double getDistanceRed(const BColor
& rColor
) const { return (getRed() > rColor
.getRed() ? getRed() - rColor
.getRed() : rColor
.getRed() - getRed()); }
131 double getDistanceGreen(const BColor
& rColor
) const { return (getGreen() > rColor
.getGreen() ? getGreen() - rColor
.getGreen() : rColor
.getGreen() - getGreen()); }
132 double getDistanceBlue(const BColor
& rColor
) const { return (getBlue() > rColor
.getBlue() ? getBlue() - rColor
.getBlue() : rColor
.getBlue() - getBlue()); }
134 double getDistance(const BColor
& rColor
) const
136 const double fDistR(getDistanceRed(rColor
));
137 const double fDistG(getDistanceGreen(rColor
));
138 const double fDistB(getDistanceBlue(rColor
));
140 return sqrt(fDistR
* fDistR
+ fDistG
* fDistG
+ fDistB
* fDistB
);
143 double getMaximumDistance(const BColor
& rColor
) const
145 const double fDistR(getDistanceRed(rColor
));
146 const double fDistG(getDistanceGreen(rColor
));
147 const double fDistB(getDistanceBlue(rColor
));
149 double fRetval(std::max(fDistR
, fDistG
));
150 return std::max(fRetval
, fDistB
);
153 // clamp color to [0.0..1.0] values in all three intensity components
156 mfX
= std::clamp(mfX
, 0.0, 1.0);
157 mfY
= std::clamp(mfY
, 0.0, 1.0);
158 mfZ
= std::clamp(mfZ
, 0.0, 1.0);
169 static const BColor
& getEmptyBColor()
171 return static_cast<const BColor
&>( ::basegfx::B3DTuple::getEmptyTuple() );
176 template<typename charT
, typename traits
>
177 std::basic_ostream
<charT
, traits
> & operator <<(
178 std::basic_ostream
<charT
, traits
> & stream
, BColor
const & color
)
181 << '[' << color
.getRed() << ", " << color
.getGreen() << ", "
182 << color
.getBlue() << ']';
184 } // end of namespace basegfx
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */