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/ThemeColor.hxx>
16 #include <com/sun/star/graphic/XGraphic.hpp>
17 #include <o3tl/hash_combine.hxx>
33 enum class SystemColorType
47 GradientActiveCaption
,
48 GradientInactiveCaption
,
68 /** Definition of a color with multiple representations
70 * A color that can be expresses as a RGB, CRGB or HSL representation or
71 * a more abstract representation as for example system color, palette,
72 * scheme (theme) color or a placeholder. In these representations the
73 * color needs to be additionally
75 * The color can also have transformations defined, which in addition
76 * manipulates the resulting color (i.e. tints, shades, alpha,...).
78 class DOCMODEL_DLLPUBLIC ComplexColor
81 ColorType meType
= ColorType::Unused
;
83 sal_Int32 mnComponent1
= 0; // Red, Hue
84 sal_Int32 mnComponent2
= 0; // Green, Saturation
85 sal_Int32 mnComponent3
= 0; // Blue, Luminance
87 SystemColorType meSystemColorType
= SystemColorType::Unused
;
90 ThemeColorType meSchemeType
= ThemeColorType::Unknown
;
91 ThemeColorUsage meThemeColorUsage
= ThemeColorUsage::Unknown
;
92 std::vector
<Transformation
> maTransformations
;
97 ColorType
getType() const { return meType
; }
99 void setType(ColorType eType
) { meType
= eType
; }
101 ThemeColorType
getSchemeType() const { return meSchemeType
; }
103 bool isValidSchemeType() const
105 return meType
== model::ColorType::Scheme
&& meSchemeType
!= ThemeColorType::Unknown
;
108 Color
getRGBColor() const { return Color(mnComponent1
, mnComponent2
, mnComponent3
); }
110 std::vector
<Transformation
> const& getTransformations() const { return maTransformations
; }
112 void setTransformations(std::vector
<Transformation
> const& rTransformations
)
114 maTransformations
= rTransformations
;
117 void addTransformation(Transformation
const& rTransform
)
119 maTransformations
.push_back(rTransform
);
122 void removeTransformations(TransformationType eType
)
124 maTransformations
.erase(std::remove_if(maTransformations
.begin(), maTransformations
.end(),
125 [eType
](Transformation
const& rTransform
) {
126 return rTransform
.meType
== eType
;
128 maTransformations
.end());
131 void clearTransformations() { maTransformations
.clear(); }
133 void setCRGB(sal_Int32 nR
, sal_Int32 nG
, sal_Int32 nB
)
138 meType
= ColorType::CRGB
;
141 void setColor(Color
const& rColor
)
143 mnComponent1
= rColor
.GetRed();
144 mnComponent2
= rColor
.GetGreen();
145 mnComponent3
= rColor
.GetBlue();
146 maFinalColor
= rColor
;
147 meType
= ColorType::RGB
;
150 void setRGB(sal_Int32 nRGB
)
152 ::Color
aColor(ColorTransparency
, nRGB
);
156 void setHSL(sal_Int32 nH
, sal_Int32 nS
, sal_Int32 nL
)
161 meType
= ColorType::HSL
;
164 void setSystemColor(SystemColorType eSystemColorType
, sal_Int32 nRGB
)
166 maLastColor
= ::Color(ColorTransparency
, nRGB
);
167 meSystemColorType
= eSystemColorType
;
168 meType
= ColorType::System
;
171 void setSchemePlaceholder() { meType
= ColorType::Placeholder
; }
173 void setSchemeColor(ThemeColorType eType
)
175 meSchemeType
= eType
;
176 meType
= ColorType::Scheme
;
179 model::ThemeColor
createThemeColor() const
181 model::ThemeColor aThemeColor
;
182 if (meType
== ColorType::Scheme
)
184 aThemeColor
.setType(meSchemeType
);
185 aThemeColor
.setTransformations(maTransformations
);
190 bool operator==(const ComplexColor
& rComplexColor
) const
192 return meType
== rComplexColor
.meType
&& mnComponent1
== rComplexColor
.mnComponent1
193 && mnComponent2
== rComplexColor
.mnComponent2
194 && mnComponent3
== rComplexColor
.mnComponent3
195 && meSystemColorType
== rComplexColor
.meSystemColorType
196 && maLastColor
== rComplexColor
.maLastColor
197 && meSchemeType
== rComplexColor
.meSchemeType
198 && maTransformations
.size() == rComplexColor
.maTransformations
.size()
199 && std::equal(maTransformations
.begin(), maTransformations
.end(),
200 rComplexColor
.maTransformations
.begin());
203 /** Applies the defined transformations to the input color */
204 Color
applyTransformations(Color
const& rColor
) const
206 Color
aColor(rColor
);
208 for (auto const& rTransform
: maTransformations
)
210 switch (rTransform
.meType
)
212 case TransformationType::Tint
:
213 aColor
.ApplyTintOrShade(rTransform
.mnValue
);
215 case TransformationType::Shade
:
216 aColor
.ApplyTintOrShade(-rTransform
.mnValue
);
218 case TransformationType::LumMod
:
219 aColor
.ApplyLumModOff(rTransform
.mnValue
, 0);
221 case TransformationType::LumOff
:
222 aColor
.ApplyLumModOff(10000, rTransform
.mnValue
);
231 void setFinalColor(Color
const& rColor
) { maFinalColor
= rColor
; }
233 Color
const& getFinalColor() const { return maFinalColor
; }
235 std::size_t getHash() const
237 std::size_t seed
= 0;
238 o3tl::hash_combine(seed
, meType
);
239 o3tl::hash_combine(seed
, mnComponent1
);
240 o3tl::hash_combine(seed
, mnComponent2
);
241 o3tl::hash_combine(seed
, mnComponent3
);
242 o3tl::hash_combine(seed
, meSystemColorType
);
243 o3tl::hash_combine(seed
, sal_uInt32(maLastColor
));
244 for (auto const& rTransform
: maTransformations
)
245 o3tl::hash_combine(seed
, rTransform
);
246 o3tl::hash_combine(seed
, sal_uInt32(maFinalColor
));
250 static model::ComplexColor
RGB(Color
const& rColor
)
252 model::ComplexColor aComplexColor
;
253 aComplexColor
.setColor(rColor
);
254 return aComplexColor
;
258 } // end of namespace svx
262 template <> struct hash
<model::ComplexColor
>
264 std::size_t operator()(model::ComplexColor
const& rColor
) const { return rColor
.getHash(); }
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */