Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / basegfx / color / bcolor.hxx
blob300f8db81e81aaa9d3c475616e2811905734f4ab
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <sal/config.h>
25 #include <ostream>
27 #include <basegfx/tuple/b3dtuple.hxx>
28 #include <vector>
29 #include <basegfx/basegfxdllapi.h>
31 namespace com { namespace sun { namespace star { namespace rendering {
32 class XGraphicDevice;
33 }}}}
35 namespace basegfx
37 /** Base Color class with three double values
39 This class derives all operators and common handling for
40 a 3D data class from B3DTuple. All necessary extensions
41 which are special for colors will be added here.
43 @see B3DTuple
45 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColor : public B3DTuple
47 public:
48 /** Create a Color with red, green and blue components from [0.0 to 1.0]
50 The color is initialized to (0.0, 0.0, 0.0)
52 BColor()
53 : B3DTuple()
56 /** Create a 3D Color
58 @param fRed
59 @param fGreen
60 @param fBlue
61 These parameters are used to initialize the red, green and blue intensities of the color
63 BColor(double fRed, double fGreen, double fBlue)
64 : B3DTuple(fRed, fGreen, fBlue)
67 /** Create a 3D Color
69 @param fLuminosity
70 The parameter is used to initialize the red, green and blue intensities of the color
72 explicit BColor(double fLuminosity)
73 : B3DTuple(fLuminosity, fLuminosity, fLuminosity)
76 /** constructor with tuple to allow copy-constructing
77 from B3DTuple-based classes
79 BColor(const ::basegfx::B3DTuple& rTuple)
80 : B3DTuple(rTuple)
83 // data access read
84 double getRed() const { return mfX; }
85 double getGreen() const { return mfY; }
86 double getBlue() const { return mfZ; }
88 // data access write
89 void setRed(double fNew) { mfX = fNew; }
90 void setGreen(double fNew) { mfY = fNew; }
91 void setBlue(double fNew) { mfZ = fNew; }
93 /** *=operator to allow usage from BColor, too
95 BColor& operator*=( const BColor& rPnt )
97 mfX *= rPnt.mfX;
98 mfY *= rPnt.mfY;
99 mfZ *= rPnt.mfZ;
100 return *this;
103 /** *=operator to allow usage from BColor, too
105 BColor& operator*=(double t)
107 mfX *= t;
108 mfY *= t;
109 mfZ *= t;
110 return *this;
113 /** assignment operator to allow assigning the results
114 of B3DTuple calculations
116 BColor& operator=( const ::basegfx::B3DTuple& rVec )
118 mfX = rVec.getX();
119 mfY = rVec.getY();
120 mfZ = rVec.getZ();
121 return *this;
124 // luminance
125 double luminance() const
127 const double fRedWeight(77.0 / 256.0); // 0.30
128 const double fGreenWeight(151.0 / 256.0); // 0.59
129 const double fBlueWeight(28.0 / 256.0); // 0.11
131 return (mfX * fRedWeight + mfY * fGreenWeight + mfZ * fBlueWeight);
134 // distances in color space
135 double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
136 double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
137 double getDistanceBlue(const BColor& rColor) const { return (getBlue() > rColor.getBlue() ? getBlue() - rColor.getBlue() : rColor.getBlue() - getBlue()); }
139 double getDistance(const BColor& rColor) const
141 const double fDistR(getDistanceRed(rColor));
142 const double fDistG(getDistanceGreen(rColor));
143 const double fDistB(getDistanceBlue(rColor));
145 return sqrt(fDistR * fDistR + fDistG * fDistG + fDistB * fDistB);
148 double getMaximumDistance(const BColor& rColor) const
150 const double fDistR(getDistanceRed(rColor));
151 const double fDistG(getDistanceGreen(rColor));
152 const double fDistB(getDistanceBlue(rColor));
154 double fRetval(fDistR > fDistG ? fDistR : fDistG);
155 return (fRetval > fDistB ? fRetval : fDistB);
158 // clamp color to [0.0..1.0] values in all three intensity components
159 BColor& clamp()
161 mfX = basegfx::clamp(mfX, 0.0, 1.0);
162 mfY = basegfx::clamp(mfY, 0.0, 1.0);
163 mfZ = basegfx::clamp(mfZ, 0.0, 1.0);
164 return *this;
167 void invert()
169 mfX = 1.0 - mfX;
170 mfY = 1.0 - mfY;
171 mfZ = 1.0 - mfZ;
174 static const BColor& getEmptyBColor()
176 return static_cast<const BColor&>( ::basegfx::B3DTuple::getEmptyTuple() );
181 template<typename charT, typename traits>
182 std::basic_ostream<charT, traits> & operator <<(
183 std::basic_ostream<charT, traits> & stream, BColor const & color)
185 return stream
186 << '[' << color.getRed() << ", " << color.getGreen() << ", "
187 << color.getBlue() << ']';
189 } // end of namespace basegfx
191 #endif // INCLUDED_BASEGFX_COLOR_BCOLOR_HXX
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */