Don't search for old (<= 2.0) .NET SDK anymore
[LibreOffice.git] / include / tools / color.hxx
blob1b8fb3eecdd1f76d63454b2f2699ef15b13c2f32
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 .
19 #ifndef INCLUDED_TOOLS_COLOR_HXX
20 #define INCLUDED_TOOLS_COLOR_HXX
22 #include <sal/types.h>
23 #include <tools/toolsdllapi.h>
24 #include <com/sun/star/uno/Any.hxx>
25 #include <basegfx/color/bcolor.hxx>
27 class SvStream;
29 constexpr sal_uInt8 COLORDATA_RED( sal_uInt32 n ) { return static_cast<sal_uInt8>(n>>16); }
30 constexpr sal_uInt8 COLORDATA_GREEN( sal_uInt32 n ) { return static_cast<sal_uInt8>(static_cast<sal_uInt16>(n) >> 8); }
31 constexpr sal_uInt8 COLORDATA_BLUE( sal_uInt32 n ) { return static_cast<sal_uInt8>(n); }
32 constexpr sal_uInt8 COLORDATA_TRANSPARENCY( sal_uInt32 n ) { return static_cast<sal_uInt8>(n>>24); }
33 constexpr sal_uInt32 COLORDATA_RGB( sal_uInt32 n ) { return static_cast<sal_uInt32>(n & 0x00FFFFFF); }
35 // Color
37 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Color final
39 sal_uInt32 mnColor;
41 public:
42 constexpr Color()
43 : mnColor(0) // black
45 constexpr Color(sal_uInt32 nColor)
46 : mnColor(nColor)
48 constexpr Color(sal_uInt8 nTransparency, sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
49 : mnColor( sal_uInt32(nBlue) | (sal_uInt32(nGreen) << 8) | (sal_uInt32(nRed) << 16)
50 | (sal_uInt32(nTransparency) << 24) )
52 constexpr Color(sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
53 : Color(0, nRed, nGreen, nBlue)
56 // constructor to create a tools-Color from ::basegfx::BColor
57 explicit Color(const basegfx::BColor& rBColor)
58 : Color(0,
59 sal_uInt8((rBColor.getRed() * 255.0) + 0.5),
60 sal_uInt8((rBColor.getGreen() * 255.0) + 0.5),
61 sal_uInt8((rBColor.getBlue() * 255.0) + 0.5))
64 /** Primarily used when passing Color objects to UNO API */
65 constexpr explicit operator sal_uInt32() const
67 return mnColor;
70 constexpr explicit operator sal_Int32() const
72 return sal_Int32(mnColor);
75 bool operator<(const Color& b) const
77 return mnColor < b.mnColor;
80 void SetRed(sal_uInt8 nRed);
81 sal_uInt8 GetRed() const
83 return COLORDATA_RED(mnColor);
85 void SetGreen(sal_uInt8 nGreen);
86 sal_uInt8 GetGreen() const
88 return COLORDATA_GREEN(mnColor);
90 void SetBlue(sal_uInt8 nBlue);
91 sal_uInt8 GetBlue() const
93 return COLORDATA_BLUE(mnColor);
95 void SetTransparency(sal_uInt8 nTransparency);
96 sal_uInt8 GetTransparency() const
98 return COLORDATA_TRANSPARENCY(mnColor);
101 Color GetRGBColor() const
103 return COLORDATA_RGB(mnColor);
106 sal_uInt8 GetColorError(const Color& rCompareColor) const;
108 sal_uInt8 GetLuminance() const;
109 void IncreaseLuminance(sal_uInt8 cLumInc);
110 void DecreaseLuminance(sal_uInt8 cLumDec);
112 void DecreaseContrast(sal_uInt8 cContDec);
115 * Apply tint or shade to a color.
117 * The input value is the percentage (in 100th of percent) of how much the
118 * color changes towards the black (shade) or white (tint). If the value
119 * is positive, the color is tinted, if the value is negative, the color is
120 * shaded.
122 void ApplyTintOrShade(sal_Int16 n100thPercent);
124 void Invert();
126 void Merge(const Color& rMergeColor, sal_uInt8 cTransparency);
128 bool IsRGBEqual(const Color& rColor) const;
130 // comparison with luminance thresholds
131 bool IsDark() const;
132 bool IsBright() const;
134 // color space conversion tools
135 // the range for h/s/b is:
136 // Hue: 0-360 degree
137 // Saturation: 0-100%
138 // Brightness: 0-100%
139 static Color HSBtoRGB(sal_uInt16 nHue, sal_uInt16 nSaturation, sal_uInt16 nBrightness);
140 void RGBtoHSB(sal_uInt16& nHue, sal_uInt16& nSaturation, sal_uInt16& nBrightness) const;
142 bool operator==(const Color& rColor) const
144 return mnColor == rColor.mnColor;
146 bool operator!=(const Color& rColor) const
148 return !(Color::operator==(rColor));
151 SvStream& Read(SvStream& rIStream);
152 SvStream& Write(SvStream& rOStream) const;
154 TOOLS_DLLPUBLIC friend SvStream& ReadColor(SvStream& rIStream, Color& rColor);
155 TOOLS_DLLPUBLIC friend SvStream& WriteColor(SvStream& rOStream, const Color& rColor);
157 // Return color as RGB hex string
158 // for example "00ff00" for green color
159 OUString AsRGBHexString() const;
161 // get ::basegfx::BColor from this color
162 basegfx::BColor getBColor() const
164 return basegfx::BColor(GetRed() / 255.0, GetGreen() / 255.0, GetBlue() / 255.0);
168 inline void Color::SetRed( sal_uInt8 nRed )
170 mnColor &= 0xFF00FFFF;
171 mnColor |= static_cast<sal_uInt32>(nRed)<<16;
174 inline void Color::SetGreen( sal_uInt8 nGreen )
176 mnColor &= 0xFFFF00FF;
177 mnColor |= static_cast<sal_uInt16>(nGreen)<<8;
180 inline void Color::SetBlue( sal_uInt8 nBlue )
182 mnColor &= 0xFFFFFF00;
183 mnColor |= nBlue;
186 inline void Color::SetTransparency( sal_uInt8 nTransparency )
188 mnColor &= 0x00FFFFFF;
189 mnColor |= static_cast<sal_uInt32>(nTransparency)<<24;
192 inline bool Color::IsRGBEqual( const Color& rColor ) const
194 return COLORDATA_RGB( mnColor ) == COLORDATA_RGB(rColor.mnColor);
197 inline sal_uInt8 Color::GetLuminance() const
199 return static_cast<sal_uInt8>((COLORDATA_BLUE(mnColor) * 29UL +
200 COLORDATA_GREEN(mnColor) * 151UL +
201 COLORDATA_RED(mnColor) * 76UL) >> 8);
204 constexpr sal_uInt8 ColorChannelMerge(sal_uInt8 nDst, sal_uInt8 nSrc, sal_uInt8 nSrcTrans)
206 return static_cast<sal_uInt8>(((static_cast<sal_Int32>(nDst)-nSrc)*nSrcTrans+((nSrc<<8)|nDst))>>8);
209 inline void Color::Merge( const Color& rMergeColor, sal_uInt8 cTransparency )
211 SetRed(ColorChannelMerge(COLORDATA_RED(mnColor), COLORDATA_RED(rMergeColor.mnColor), cTransparency));
212 SetGreen(ColorChannelMerge(COLORDATA_GREEN(mnColor), COLORDATA_GREEN(rMergeColor.mnColor), cTransparency));
213 SetBlue(ColorChannelMerge(COLORDATA_BLUE(mnColor), COLORDATA_BLUE(rMergeColor.mnColor), cTransparency));
216 // to reduce the noise when moving these into and out of Any
217 inline bool operator >>=( const css::uno::Any & rAny, Color & value )
219 sal_Int32 nTmp;
220 if (!(rAny >>= nTmp))
221 return false;
222 value = Color(nTmp);
223 return true;
225 inline void operator <<=( css::uno::Any & rAny, Color value )
227 rAny <<= sal_Int32(value);
229 namespace com { namespace sun { namespace star { namespace uno {
230 template<>
231 inline Any makeAny( Color const & value )
233 return Any(sal_Int32(value));
235 } } } }
237 // Color types
239 constexpr ::Color COL_BLACK ( 0x00, 0x00, 0x00 );
240 constexpr ::Color COL_BLUE ( 0x00, 0x00, 0x80 );
241 constexpr ::Color COL_GREEN ( 0x00, 0x80, 0x00 );
242 constexpr ::Color COL_CYAN ( 0x00, 0x80, 0x80 );
243 constexpr ::Color COL_RED ( 0x80, 0x00, 0x00 );
244 constexpr ::Color COL_RED_FONTCOLOR ( 0xCE, 0x18, 0x1E );
245 constexpr ::Color COL_MAGENTA ( 0x80, 0x00, 0x80 );
246 constexpr ::Color COL_BROWN ( 0x80, 0x80, 0x00 );
247 constexpr ::Color COL_GRAY ( 0x80, 0x80, 0x80 );
248 constexpr ::Color COL_GRAY3 ( 0xCC, 0xCC, 0xCC );
249 constexpr ::Color COL_GRAY7 ( 0x66, 0x66, 0x66 );
250 constexpr ::Color COL_LIGHTGRAY ( 0xC0, 0xC0, 0xC0 );
251 constexpr ::Color COL_LIGHTBLUE ( 0x00, 0x00, 0xFF );
252 constexpr ::Color COL_LIGHTGREEN ( 0x00, 0xFF, 0x00 );
253 constexpr ::Color COL_LIGHTCYAN ( 0x00, 0xFF, 0xFF );
254 constexpr ::Color COL_LIGHTRED ( 0xFF, 0x00, 0x00 );
255 constexpr ::Color COL_LIGHTMAGENTA ( 0xFF, 0x00, 0xFF );
256 constexpr ::Color COL_LIGHTGRAYBLUE ( 0xE0, 0xE0, 0xFF );
257 constexpr ::Color COL_YELLOW ( 0xFF, 0xFF, 0x00 );
258 constexpr ::Color COL_YELLOW_HIGHLIGHT ( 0xFF, 0xF2, 0x00 );
259 constexpr ::Color COL_WHITE ( 0xFF, 0xFF, 0xFF );
260 constexpr ::Color COL_TRANSPARENT ( 0xFF, 0xFF, 0xFF, 0xFF );
261 constexpr ::Color COL_AUTO ( 0xFF, 0xFF, 0xFF, 0xFF );
262 constexpr ::Color COL_AUTHOR1_DARK ( 198, 146, 0 );
263 constexpr ::Color COL_AUTHOR1_NORMAL ( 255, 255, 158 );
264 constexpr ::Color COL_AUTHOR1_LIGHT ( 255, 255, 195 );
265 constexpr ::Color COL_AUTHOR2_DARK ( 6, 70, 162 );
266 constexpr ::Color COL_AUTHOR2_NORMAL ( 216, 232, 255 );
267 constexpr ::Color COL_AUTHOR2_LIGHT ( 233, 242, 255 );
268 constexpr ::Color COL_AUTHOR3_DARK ( 87, 157, 28 );
269 constexpr ::Color COL_AUTHOR3_NORMAL ( 218, 248, 193 );
270 constexpr ::Color COL_AUTHOR3_LIGHT ( 226, 250, 207 );
271 constexpr ::Color COL_AUTHOR4_DARK ( 105, 43, 157 );
272 constexpr ::Color COL_AUTHOR4_NORMAL ( 228, 210, 245 );
273 constexpr ::Color COL_AUTHOR4_LIGHT ( 239, 228, 248 );
274 constexpr ::Color COL_AUTHOR5_DARK ( 197, 0, 11 );
275 constexpr ::Color COL_AUTHOR5_NORMAL ( 254, 205, 208 );
276 constexpr ::Color COL_AUTHOR5_LIGHT ( 255, 227, 229 );
277 constexpr ::Color COL_AUTHOR6_DARK ( 0, 128, 128 );
278 constexpr ::Color COL_AUTHOR6_NORMAL ( 210, 246, 246 );
279 constexpr ::Color COL_AUTHOR6_LIGHT ( 230, 250, 250 );
280 constexpr ::Color COL_AUTHOR7_DARK ( 140, 132, 0 );
281 constexpr ::Color COL_AUTHOR7_NORMAL ( 237, 252, 163 );
282 constexpr ::Color COL_AUTHOR7_LIGHT ( 242, 254, 181 );
283 constexpr ::Color COL_AUTHOR8_DARK ( 53, 85, 107 );
284 constexpr ::Color COL_AUTHOR8_NORMAL ( 211, 222, 232 );
285 constexpr ::Color COL_AUTHOR8_LIGHT ( 226, 234, 241 );
286 constexpr ::Color COL_AUTHOR9_DARK ( 209, 118, 0 );
287 constexpr ::Color COL_AUTHOR9_NORMAL ( 255, 226, 185 );
288 constexpr ::Color COL_AUTHOR9_LIGHT ( 255, 231, 199 );
290 #endif
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */