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/.
13 #include <docmodel/dllapi.h>
14 #include <tools/color.hxx>
15 #include <docmodel/theme/ThemeColorType.hxx>
16 #include <docmodel/color/Transformation.hxx>
17 #include <o3tl/hash_combine.hxx>
35 enum class SystemColorType
49 GradientActiveCaption
,
50 GradientInactiveCaption
,
70 /** Definition of a color with multiple representations
72 * A color that can be expresses as a RGB, CRGB or HSL representation or
73 * a more abstract representation as for example system color, palette,
74 * theme color or a placeholder. In these representations the
75 * color needs to be additionally
77 * The color can also have transformations defined, which in addition
78 * manipulates the resulting color (i.e. tints, shades, alpha,...).
80 class DOCMODEL_DLLPUBLIC ComplexColor
83 ColorType meType
= ColorType::Unused
;
85 double mnComponent1
= 0.0; // Red, Hue
86 double mnComponent2
= 0.0; // Green, Saturation
87 double mnComponent3
= 0.0; // Blue, Luminance
89 SystemColorType meSystemColorType
= SystemColorType::Unused
;
92 ThemeColorType meThemeColorType
= ThemeColorType::Unknown
;
93 ThemeColorUsage meThemeColorUsage
= ThemeColorUsage::Unknown
;
95 std::vector
<Transformation
> maTransformations
;
100 ColorType
getType() const { return meType
; }
101 void setType(ColorType eType
) { meType
= eType
; }
103 ThemeColorType
getThemeColorType() const { return meThemeColorType
; }
104 bool isValidThemeType() const
106 return meType
== model::ColorType::Theme
&& meThemeColorType
!= ThemeColorType::Unknown
;
109 ThemeColorUsage
getThemeColorUsage() const { return meThemeColorUsage
; }
110 void setThemeColorUsage(ThemeColorUsage eThemeColorUsage
)
112 meThemeColorUsage
= eThemeColorUsage
;
115 SystemColorType
getSystemColorType() const { return meSystemColorType
; }
117 void setSystemColorType(SystemColorType eSystemColorType
)
119 meSystemColorType
= eSystemColorType
;
120 meType
= ColorType::System
;
123 Color
getRGBColor() const { return Color(mnComponent1
, mnComponent2
, mnComponent3
); }
125 std::vector
<Transformation
> const& getTransformations() const { return maTransformations
; }
127 void setTransformations(std::vector
<Transformation
> const& rTransformations
)
129 maTransformations
= rTransformations
;
132 void addTransformation(Transformation
const& rTransform
)
134 maTransformations
.push_back(rTransform
);
137 void removeTransformations(TransformationType eType
)
139 std::erase_if(maTransformations
, [eType
](Transformation
const& rTransform
) {
140 return rTransform
.meType
== eType
;
144 void clearTransformations() { maTransformations
.clear(); }
146 double getRed() const { return mnComponent1
; }
147 double getGreen() const { return mnComponent2
; }
148 double getBlue() const { return mnComponent3
; }
150 void setCRGB(sal_Int32 nR
, sal_Int32 nG
, sal_Int32 nB
)
155 meType
= ColorType::CRGB
;
158 Color
getRGB() const { return Color(mnComponent1
, mnComponent2
, mnComponent3
); }
160 void setColor(Color
const& rColor
)
162 mnComponent1
= rColor
.GetRed();
163 mnComponent2
= rColor
.GetGreen();
164 mnComponent3
= rColor
.GetBlue();
165 maFinalColor
= rColor
;
166 meType
= ColorType::RGB
;
169 void setRGB(sal_Int32 nRGB
)
171 ::Color
aColor(ColorTransparency
, nRGB
);
175 void setHSL(sal_Int32 nH
, sal_Int32 nS
, sal_Int32 nL
)
180 meType
= ColorType::HSL
;
183 void setSystemColor(SystemColorType eSystemColorType
, sal_Int32 nRGB
)
185 maLastColor
= ::Color(ColorTransparency
, nRGB
);
186 meSystemColorType
= eSystemColorType
;
187 meType
= ColorType::System
;
190 void setThemePlaceholder() { meType
= ColorType::Placeholder
; }
192 void setThemeColor(ThemeColorType eType
)
194 meThemeColorType
= eType
;
195 meType
= ColorType::Theme
;
198 bool operator==(const ComplexColor
& rComplexColor
) const
200 return meType
== rComplexColor
.meType
&& mnComponent1
== rComplexColor
.mnComponent1
201 && mnComponent2
== rComplexColor
.mnComponent2
202 && mnComponent3
== rComplexColor
.mnComponent3
203 && meSystemColorType
== rComplexColor
.meSystemColorType
204 && maLastColor
== rComplexColor
.maLastColor
205 && meThemeColorType
== rComplexColor
.meThemeColorType
206 && maTransformations
.size() == rComplexColor
.maTransformations
.size()
207 && std::equal(maTransformations
.begin(), maTransformations
.end(),
208 rComplexColor
.maTransformations
.begin());
211 /** Applies the defined transformations to the input color */
212 Color
applyTransformations(Color
const& rColor
) const
214 Color
aColor(rColor
);
216 for (auto const& rTransform
: maTransformations
)
218 switch (rTransform
.meType
)
220 case TransformationType::Tint
:
221 aColor
.ApplyTintOrShade(rTransform
.mnValue
);
223 case TransformationType::Shade
:
224 aColor
.ApplyTintOrShade(-rTransform
.mnValue
);
226 case TransformationType::LumMod
:
227 aColor
.ApplyLumModOff(rTransform
.mnValue
, 0);
229 case TransformationType::LumOff
:
230 aColor
.ApplyLumModOff(10000, rTransform
.mnValue
);
239 void setFinalColor(Color
const& rColor
) { maFinalColor
= rColor
; }
241 Color
const& getFinalColor() const { return maFinalColor
; }
243 std::size_t getHash() const
245 std::size_t seed
= 0;
246 o3tl::hash_combine(seed
, meType
);
247 o3tl::hash_combine(seed
, mnComponent1
);
248 o3tl::hash_combine(seed
, mnComponent2
);
249 o3tl::hash_combine(seed
, mnComponent3
);
250 o3tl::hash_combine(seed
, meSystemColorType
);
251 o3tl::hash_combine(seed
, sal_uInt32(maLastColor
));
252 for (auto const& rTransform
: maTransformations
)
253 o3tl::hash_combine(seed
, rTransform
);
254 o3tl::hash_combine(seed
, sal_uInt32(maFinalColor
));
258 static model::ComplexColor
createRGB(Color
const& rColor
)
260 model::ComplexColor aComplexColor
;
261 aComplexColor
.setColor(rColor
);
262 return aComplexColor
;
265 static model::ComplexColor
Theme(ThemeColorType eThemeColorType
)
267 model::ComplexColor aComplexColor
;
268 aComplexColor
.setThemeColor(eThemeColorType
);
269 return aComplexColor
;
273 } // end of namespace model
277 template <> struct hash
<model::ComplexColor
>
279 std::size_t operator()(model::ComplexColor
const& rColor
) const { return rColor
.getHash(); }
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */