bump product version to 7.6.3.2-android
[LibreOffice.git] / include / docmodel / color / ComplexColor.hxx
blob88ff7a42a478af51c8bef9404c0d32dc753aab38
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 */
11 #pragma once
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>
19 namespace model
21 enum class ColorType
23 Unused,
24 RGB,
25 CRGB,
26 HSL,
27 Scheme,
28 Palette,
29 System,
30 Placeholder
33 enum class SystemColorType
35 Unused,
36 DarkShadow3D,
37 Light3D,
38 ActiveBorder,
39 ActiveCaption,
40 AppWorkspace,
41 Background,
42 ButtonFace,
43 ButtonHighlight,
44 ButtonShadow,
45 ButtonText,
46 CaptionText,
47 GradientActiveCaption,
48 GradientInactiveCaption,
49 GrayText,
50 Highlight,
51 HighlightText,
52 HotLight,
53 InactiveBorder,
54 InactiveCaption,
55 InactiveCaptionText,
56 InfoBack,
57 InfoText,
58 Menu,
59 MenuBar,
60 MenuHighlight,
61 MenuText,
62 ScrollBar,
63 Window,
64 WindowFrame,
65 WindowText
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
80 public:
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;
88 ::Color maLastColor;
90 ThemeColorType meSchemeType = ThemeColorType::Unknown;
91 ThemeColorUsage meThemeColorUsage = ThemeColorUsage::Unknown;
92 std::vector<Transformation> maTransformations;
94 ::Color maFinalColor;
96 public:
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)
135 mnComponent1 = nR;
136 mnComponent2 = nG;
137 mnComponent3 = 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);
153 setColor(aColor);
156 void setHSL(sal_Int32 nH, sal_Int32 nS, sal_Int32 nL)
158 mnComponent1 = nH;
159 mnComponent2 = nS;
160 mnComponent3 = 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);
187 return aThemeColor;
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);
214 break;
215 case TransformationType::Shade:
216 aColor.ApplyTintOrShade(-rTransform.mnValue);
217 break;
218 case TransformationType::LumMod:
219 aColor.ApplyLumModOff(rTransform.mnValue, 0);
220 break;
221 case TransformationType::LumOff:
222 aColor.ApplyLumModOff(10000, rTransform.mnValue);
223 break;
224 default:
225 break;
228 return aColor;
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));
247 return seed;
250 static model::ComplexColor RGB(Color const& rColor)
252 model::ComplexColor aComplexColor;
253 aComplexColor.setColor(rColor);
254 return aComplexColor;
258 } // end of namespace svx
260 namespace std
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: */