bump product version to 6.3.0.0.beta1
[LibreOffice.git] / include / basegfx / color / bcolor.hxx
blobef4857bba598f45316fa78de17338a17e213c02d
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 <algorithm>
26 #include <ostream>
28 #include <basegfx/tuple/b3dtuple.hxx>
29 #include <basegfx/basegfxdllapi.h>
31 namespace basegfx
33 /** Base Color class with three double values
35 This class derives all operators and common handling for
36 a 3D data class from B3DTuple. All necessary extensions
37 which are special for colors will be added here.
39 @see B3DTuple
41 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColor : public B3DTuple
43 public:
44 /** Create a Color with red, green and blue components from [0.0 to 1.0]
46 The color is initialized to (0.0, 0.0, 0.0)
48 BColor()
49 : B3DTuple()
52 /** Create a 3D Color
54 @param fRed
55 @param fGreen
56 @param fBlue
57 These parameters are used to initialize the red, green and blue intensities of the color
59 BColor(double fRed, double fGreen, double fBlue)
60 : B3DTuple(fRed, fGreen, fBlue)
63 /** Create a 3D Color
65 @param fLuminosity
66 The parameter is used to initialize the red, green and blue intensities of the color
68 explicit BColor(double fLuminosity)
69 : B3DTuple(fLuminosity, fLuminosity, fLuminosity)
72 /** constructor with tuple to allow copy-constructing
73 from B3DTuple-based classes
75 BColor(const ::basegfx::B3DTuple& rTuple)
76 : B3DTuple(rTuple)
79 // data access read
80 double getRed() const { return mfX; }
81 double getGreen() const { return mfY; }
82 double getBlue() const { return mfZ; }
84 // data access write
85 void setRed(double fNew) { mfX = fNew; }
86 void setGreen(double fNew) { mfY = fNew; }
87 void setBlue(double fNew) { mfZ = fNew; }
89 /** *=operator to allow usage from BColor, too
91 BColor& operator*=( const BColor& rPnt )
93 mfX *= rPnt.mfX;
94 mfY *= rPnt.mfY;
95 mfZ *= rPnt.mfZ;
96 return *this;
99 /** *=operator to allow usage from BColor, too
101 BColor& operator*=(double t)
103 mfX *= t;
104 mfY *= t;
105 mfZ *= t;
106 return *this;
109 /** assignment operator to allow assigning the results
110 of B3DTuple calculations
112 BColor& operator=( const ::basegfx::B3DTuple& rVec )
114 mfX = rVec.getX();
115 mfY = rVec.getY();
116 mfZ = rVec.getZ();
117 return *this;
120 // luminance
121 double luminance() const
123 const double fRedWeight(77.0 / 256.0); // 0.30
124 const double fGreenWeight(151.0 / 256.0); // 0.59
125 const double fBlueWeight(28.0 / 256.0); // 0.11
127 return (mfX * fRedWeight + mfY * fGreenWeight + mfZ * fBlueWeight);
130 // distances in color space
131 double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
132 double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
133 double getDistanceBlue(const BColor& rColor) const { return (getBlue() > rColor.getBlue() ? getBlue() - rColor.getBlue() : rColor.getBlue() - getBlue()); }
135 double getDistance(const BColor& rColor) const
137 const double fDistR(getDistanceRed(rColor));
138 const double fDistG(getDistanceGreen(rColor));
139 const double fDistB(getDistanceBlue(rColor));
141 return sqrt(fDistR * fDistR + fDistG * fDistG + fDistB * fDistB);
144 double getMaximumDistance(const BColor& rColor) const
146 const double fDistR(getDistanceRed(rColor));
147 const double fDistG(getDistanceGreen(rColor));
148 const double fDistB(getDistanceBlue(rColor));
150 double fRetval(std::max(fDistR, fDistG));
151 return std::max(fRetval, fDistB);
154 // clamp color to [0.0..1.0] values in all three intensity components
155 BColor& clamp()
157 mfX = std::clamp(mfX, 0.0, 1.0);
158 mfY = std::clamp(mfY, 0.0, 1.0);
159 mfZ = std::clamp(mfZ, 0.0, 1.0);
160 return *this;
163 void invert()
165 mfX = 1.0 - mfX;
166 mfY = 1.0 - mfY;
167 mfZ = 1.0 - mfZ;
170 static const BColor& getEmptyBColor()
172 return static_cast<const BColor&>( ::basegfx::B3DTuple::getEmptyTuple() );
177 template<typename charT, typename traits>
178 std::basic_ostream<charT, traits> & operator <<(
179 std::basic_ostream<charT, traits> & stream, BColor const & color)
181 return stream
182 << '[' << color.getRed() << ", " << color.getGreen() << ", "
183 << color.getBlue() << ']';
185 } // end of namespace basegfx
187 #endif // INCLUDED_BASEGFX_COLOR_BCOLOR_HXX
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */