Get the style color and number just once
[LibreOffice.git] / include / basegfx / color / bcolor.hxx
blob46a656b2307e5981bb35652976bd048a654ec87e
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 #pragma once
22 #include <sal/config.h>
24 #include <algorithm>
25 #include <ostream>
27 #include <basegfx/tuple/b3dtuple.hxx>
28 #include <basegfx/basegfxdllapi.h>
30 namespace basegfx
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.
38 @see B3DTuple
40 class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC BColor : public B3DTuple
42 bool bAutomatic = false;
44 public:
45 /** Create a Color with red, green and blue components from [0.0 to 1.0]
47 The color is initialized to (0.0, 0.0, 0.0)
49 BColor()
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 mnX; }
81 double getGreen() const { return mnY; }
82 double getBlue() const { return mnZ; }
83 bool isAutomatic() const { return bAutomatic; }
85 // data access write
86 void setRed(double fNew) { mnX = fNew; }
87 void setGreen(double fNew) { mnY = fNew; }
88 void setBlue(double fNew) { mnZ = fNew; }
89 void setAutomatic(bool bNew) { bAutomatic = bNew; }
91 /** *=operator to allow usage from BColor, too
93 BColor& operator*=( const BColor& rPnt )
95 mnX *= rPnt.mnX;
96 mnY *= rPnt.mnY;
97 mnZ *= rPnt.mnZ;
98 return *this;
101 /** *=operator to allow usage from BColor, too
103 BColor& operator*=(double t)
105 mnX *= t;
106 mnY *= t;
107 mnZ *= t;
108 return *this;
111 /** assignment operator to allow assigning the results
112 of B3DTuple calculations
114 BColor& operator=( const ::basegfx::B3DTuple& rVec )
116 mnX = rVec.getX();
117 mnY = rVec.getY();
118 mnZ = rVec.getZ();
119 return *this;
122 // luminance
123 double luminance() const
125 const double fRedWeight(77.0 / 256.0); // 0.30
126 const double fGreenWeight(151.0 / 256.0); // 0.59
127 const double fBlueWeight(28.0 / 256.0); // 0.11
129 return (mnX * fRedWeight + mnY * fGreenWeight + mnZ * fBlueWeight);
132 // distances in color space
133 double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
134 double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
135 double getDistanceBlue(const BColor& rColor) const { return (getBlue() > rColor.getBlue() ? getBlue() - rColor.getBlue() : rColor.getBlue() - getBlue()); }
137 double getDistance(const BColor& rColor) const
139 const double fDistR(getDistanceRed(rColor));
140 const double fDistG(getDistanceGreen(rColor));
141 const double fDistB(getDistanceBlue(rColor));
143 return std::hypot(fDistR, fDistG, fDistB);
146 double getMaximumDistance(const BColor& rColor) const
148 const double fDistR(getDistanceRed(rColor));
149 const double fDistG(getDistanceGreen(rColor));
150 const double fDistB(getDistanceBlue(rColor));
152 double fRetval(std::max(fDistR, fDistG));
153 return std::max(fRetval, fDistB);
156 // clamp color to [0.0..1.0] values in all three intensity components
157 BColor& clamp()
159 mnX = std::clamp(mnX, 0.0, 1.0);
160 mnY = std::clamp(mnY, 0.0, 1.0);
161 mnZ = std::clamp(mnZ, 0.0, 1.0);
162 return *this;
165 void invert()
167 mnX = 1.0 - mnX;
168 mnY = 1.0 - mnY;
169 mnZ = 1.0 - mnZ;
172 static const BColor& getEmptyBColor()
174 static BColor const singleton;
175 return singleton;
180 template<typename charT, typename traits>
181 std::basic_ostream<charT, traits> & operator <<(
182 std::basic_ostream<charT, traits> & stream, BColor const & color)
184 return stream
185 << '[' << color.getRed() << ", " << color.getGreen() << ", "
186 << color.getBlue() << ']';
188 } // end of namespace basegfx
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */