LibreOffice Module basegfx (master) 1
bcolor.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
22#include <sal/config.h>
23
24#include <algorithm>
25#include <ostream>
26
29
30namespace basegfx
31{
41 {
42 public:
48 {}
49
57 BColor(double fRed, double fGreen, double fBlue)
58 : B3DTuple(fRed, fGreen, fBlue)
59 {}
60
66 explicit BColor(double fLuminosity)
67 : B3DTuple(fLuminosity, fLuminosity, fLuminosity)
68 {}
69
73 BColor(const ::basegfx::B3DTuple& rTuple)
74 : B3DTuple(rTuple)
75 {}
76
77 // data access read
78 double getRed() const { return mfX; }
79 double getGreen() const { return mfY; }
80 double getBlue() const { return mfZ; }
81
82 // data access write
83 void setRed(double fNew) { mfX = fNew; }
84 void setGreen(double fNew) { mfY = fNew; }
85 void setBlue(double fNew) { mfZ = fNew; }
86
89 BColor& operator*=( const BColor& rPnt )
90 {
91 mfX *= rPnt.mfX;
92 mfY *= rPnt.mfY;
93 mfZ *= rPnt.mfZ;
94 return *this;
95 }
96
99 BColor& operator*=(double t)
100 {
101 mfX *= t;
102 mfY *= t;
103 mfZ *= t;
104 return *this;
105 }
106
110 BColor& operator=( const ::basegfx::B3DTuple& rVec )
111 {
112 mfX = rVec.getX();
113 mfY = rVec.getY();
114 mfZ = rVec.getZ();
115 return *this;
116 }
117
118 // luminance
119 double luminance() const
120 {
121 const double fRedWeight(77.0 / 256.0); // 0.30
122 const double fGreenWeight(151.0 / 256.0); // 0.59
123 const double fBlueWeight(28.0 / 256.0); // 0.11
124
125 return (mfX * fRedWeight + mfY * fGreenWeight + mfZ * fBlueWeight);
126 }
127
128 // distances in color space
129 double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
130 double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
131 double getDistanceBlue(const BColor& rColor) const { return (getBlue() > rColor.getBlue() ? getBlue() - rColor.getBlue() : rColor.getBlue() - getBlue()); }
132
133 double getDistance(const BColor& rColor) const
134 {
135 const double fDistR(getDistanceRed(rColor));
136 const double fDistG(getDistanceGreen(rColor));
137 const double fDistB(getDistanceBlue(rColor));
138
139 return std::hypot(fDistR, fDistG, fDistB);
140 }
141
142 double getMaximumDistance(const BColor& rColor) const
143 {
144 const double fDistR(getDistanceRed(rColor));
145 const double fDistG(getDistanceGreen(rColor));
146 const double fDistB(getDistanceBlue(rColor));
147
148 double fRetval(std::max(fDistR, fDistG));
149 return std::max(fRetval, fDistB);
150 }
151
152 // clamp color to [0.0..1.0] values in all three intensity components
154 {
155 mfX = std::clamp(mfX, 0.0, 1.0);
156 mfY = std::clamp(mfY, 0.0, 1.0);
157 mfZ = std::clamp(mfZ, 0.0, 1.0);
158 return *this;
159 }
160
161 void invert()
162 {
163 mfX = 1.0 - mfX;
164 mfY = 1.0 - mfY;
165 mfZ = 1.0 - mfZ;
166 }
167
168 static const BColor& getEmptyBColor()
169 {
170 return static_cast<const BColor&>( ::basegfx::B3DTuple::getEmptyTuple() );
171 }
172
173 };
174
175 template<typename charT, typename traits>
176 std::basic_ostream<charT, traits> & operator <<(
177 std::basic_ostream<charT, traits> & stream, BColor const & color)
178 {
179 return stream
180 << '[' << color.getRed() << ", " << color.getGreen() << ", "
181 << color.getBlue() << ']';
182 }
183} // end of namespace basegfx
184
185/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
#define BASEGFX_DLLPUBLIC
Definition: basegfxdllapi.h:35
Base class for all Points/Vectors with three double values.
Definition: b3dtuple.hxx:40
static const B3DTuple & getEmptyTuple()
Definition: b3dtuple.cxx:25
Base Color class with three double values.
Definition: bcolor.hxx:41
double getDistance(const BColor &rColor) const
Definition: bcolor.hxx:133
double getBlue() const
Definition: bcolor.hxx:80
BColor()
Create a Color with red, green and blue components from [0.0 to 1.0].
Definition: bcolor.hxx:47
double getDistanceRed(const BColor &rColor) const
Definition: bcolor.hxx:129
void setBlue(double fNew)
Definition: bcolor.hxx:85
double luminance() const
Definition: bcolor.hxx:119
double getMaximumDistance(const BColor &rColor) const
Definition: bcolor.hxx:142
BColor & operator=(const ::basegfx::B3DTuple &rVec)
assignment operator to allow assigning the results of B3DTuple calculations
Definition: bcolor.hxx:110
double getDistanceGreen(const BColor &rColor) const
Definition: bcolor.hxx:130
BColor(const ::basegfx::B3DTuple &rTuple)
constructor with tuple to allow copy-constructing from B3DTuple-based classes
Definition: bcolor.hxx:73
double getDistanceBlue(const BColor &rColor) const
Definition: bcolor.hxx:131
BColor(double fRed, double fGreen, double fBlue)
Create a 3D Color.
Definition: bcolor.hxx:57
void invert()
Definition: bcolor.hxx:161
static const BColor & getEmptyBColor()
Definition: bcolor.hxx:168
double getRed() const
Definition: bcolor.hxx:78
BColor & operator*=(const BColor &rPnt)
*=operator to allow usage from BColor, too
Definition: bcolor.hxx:89
BColor & operator*=(double t)
*=operator to allow usage from BColor, too
Definition: bcolor.hxx:99
BColor & clamp()
Definition: bcolor.hxx:153
void setGreen(double fNew)
Definition: bcolor.hxx:84
double getGreen() const
Definition: bcolor.hxx:79
BColor(double fLuminosity)
Create a 3D Color.
Definition: bcolor.hxx:66
void setRed(double fNew)
Definition: bcolor.hxx:83
Reference< XOutputStream > stream
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &stream, BColor const &color)
Definition: bcolor.hxx:176
sal_uInt8 getRed(IntSRGBA nCol)
sal_uInt8 getBlue(IntSRGBA nCol)
sal_uInt8 getGreen(IntSRGBA nCol)
double mfY
double mfX
#define SAL_WARN_UNUSED