Bump version to 21.06.18.1
[LibreOffice.git] / include / tools / color.hxx
blob99966c65d779736b08b701120775f89dd4d82516
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>
26 #include <osl/endian.h>
28 namespace color
31 constexpr sal_uInt32 extractRGB(sal_uInt32 nColorNumber)
33 return nColorNumber & 0x00FFFFFF;
36 constexpr sal_uInt8 ColorChannelMerge(sal_uInt8 nDst, sal_uInt8 nSrc, sal_uInt8 nSrcTrans)
38 return sal_uInt8(((sal_Int32(nDst) - nSrc) * nSrcTrans + ((nSrc << 8) | nDst)) >> 8);
43 // Color
45 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Color
47 // data intentionally public; read the commit log!
48 public:
49 union
51 sal_uInt32 mValue;
52 struct
54 #ifdef OSL_BIGENDIAN
55 sal_uInt8 A;
56 sal_uInt8 R;
57 sal_uInt8 G;
58 sal_uInt8 B;
59 #else
60 sal_uInt8 B;
61 sal_uInt8 G;
62 sal_uInt8 R;
63 sal_uInt8 A;
64 #endif
68 public:
69 constexpr Color()
70 : mValue(0) // black
73 constexpr Color(sal_uInt32 nColor)
74 : mValue(nColor)
77 constexpr Color(sal_uInt8 nTransparency, sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
78 : mValue(sal_uInt32(nBlue) | (sal_uInt32(nGreen) << 8) | (sal_uInt32(nRed) << 16) | (sal_uInt32(nTransparency) << 24))
81 constexpr Color(sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
82 : mValue(sal_uInt32(nBlue) | (sal_uInt32(nGreen) << 8) | (sal_uInt32(nRed) << 16) | (sal_uInt32(0) << 24))
85 // constructor to create a tools-Color from ::basegfx::BColor
86 explicit Color(const basegfx::BColor& rBColor)
87 : Color(0,
88 sal_uInt8(std::lround(rBColor.getRed() * 255.0)),
89 sal_uInt8(std::lround(rBColor.getGreen() * 255.0)),
90 sal_uInt8(std::lround(rBColor.getBlue() * 255.0)))
93 /** Casts the color to corresponding uInt32.
94 * Primarily used when passing Color objects to UNO API
95 * @return corresponding sal_uInt32
97 constexpr explicit operator sal_uInt32() const
99 return mValue;
102 /** Casts the color to corresponding iInt32.
103 * If there is no transparency, will be positive.
104 * @return corresponding sal_Int32
106 constexpr explicit operator sal_Int32() const
108 return sal_Int32(mValue);
111 /* Basic RGBA operations */
113 /** Gets the red value.
114 * @return R
116 sal_uInt8 GetRed() const
118 return R;
121 /** Gets the green value.
122 * @return G
124 sal_uInt8 GetGreen() const
126 return G;
129 /** Gets the blue value.
130 * @return B
132 sal_uInt8 GetBlue() const
134 return B;
137 /** Gets the transparency value.
138 * @return A
140 sal_uInt8 GetTransparency() const
142 return A;
145 /** Sets the red value.
146 * @param nRed
148 void SetRed(sal_uInt8 nRed)
150 R = nRed;
153 /** Sets the green value.
154 * @param nGreen
156 void SetGreen(sal_uInt8 nGreen)
158 G = nGreen;
161 /** Sets the blue value.
162 * @param nBlue
164 void SetBlue(sal_uInt8 nBlue)
166 B = nBlue;
169 /** Sets the transparency value.
170 * @param nTransparency
172 void SetTransparency(sal_uInt8 nTransparency)
174 A = nTransparency;
177 /** Returns the same color but ignoring the transparency value.
178 * @return RGB version
180 Color GetRGBColor() const
182 return mValue & 0x00FFFFFF;
185 /* Comparison and operators */
187 /** Check if the color RGB value is equal than rColor.
188 * @param rColor
189 * @return is equal
191 bool IsRGBEqual( const Color& rColor ) const
193 return ( mValue & 0x00FFFFFF ) == ( rColor.mValue & 0x00FFFFFF );
196 /** Check if the color value is lower than aCompareColor.
197 * @param aCompareColor
198 * @return is lower
200 bool operator<(const Color& aCompareColor) const
202 return mValue < aCompareColor.mValue;
205 /** Check if the color value is greater than aCompareColor.
206 * @param aCompareColor
207 * @return is greater
209 bool operator>(const Color& aCompareColor) const
211 return mValue > aCompareColor.mValue;
214 /** Check if the color value is equal than rColor.
215 * @param rColor
216 * @return is equal
218 bool operator==(const Color& rColor) const
220 return mValue == rColor.mValue;
223 /** Check if the color value is unequal than rColor.
224 * @param rColor
225 * @return is unequal
227 bool operator!=(const Color& rColor) const
229 return mValue != rColor.mValue;
232 /** Gets the color error compared to another.
233 * It describes how different they are.
234 * It takes the abs of differences in parameters.
235 * @param rCompareColor
236 * @return error
238 sal_uInt16 GetColorError(const Color& rCompareColor) const
240 return static_cast<sal_uInt16>(
241 abs(static_cast<int>(GetBlue()) - rCompareColor.GetBlue()) +
242 abs(static_cast<int>(GetGreen()) - rCompareColor.GetGreen()) +
243 abs(static_cast<int>(GetRed()) - rCompareColor.GetRed()));
246 /* Light and contrast */
248 /** Gets the color luminance. It means perceived brightness.
249 * @return luminance
251 sal_uInt8 GetLuminance() const
253 return sal_uInt8((B * 29UL + G * 151UL + R * 76UL) >> 8);
256 /** Increases the color luminance by cLumInc.
257 * @param cLumInc
259 void IncreaseLuminance(sal_uInt8 cLumInc);
261 /** Decreases the color luminance by cLumDec.
262 * @param cLumDec
264 void DecreaseLuminance(sal_uInt8 cLumDec);
266 /** Decreases color contrast with white by cContDec.
267 * @param cContDec
269 void DecreaseContrast(sal_uInt8 cContDec);
271 /** Comparison with luminance thresholds.
272 * @return is dark
274 bool IsDark() const
276 return sal_uInt8((B * 29UL + G * 151UL + R * 76UL) >> 8) <= 60;
279 /** Comparison with luminance thresholds.
280 * @return is dark
282 bool IsBright() const
284 return sal_uInt8((B * 29UL + G * 151UL + R * 76UL) >> 8) >= 245;
287 /* Color filters */
290 * Apply tint or shade to a color.
292 * The input value is the percentage (in 100th of percent) of how much the
293 * color changes towards the black (shade) or white (tint). If the value
294 * is positive, the color is tinted, if the value is negative, the color is
295 * shaded.
297 void ApplyTintOrShade(sal_Int16 n100thPercent);
299 /** Inverts color. 1 and 0 are switched.
300 * Note that the result will be the complementary color.
301 * For example, if you have red, you will get cyan: FF0000 -> 00FFFF.
303 void Invert()
305 R = ~R;
306 G = ~G;
307 B = ~B;
310 /** Merges color with rMergeColor.
311 * Allows to get resulting color when superposing another.
312 * @param rMergeColor
313 * @param cTransparency
315 void Merge(const Color& rMergeColor, sal_uInt8 cTransparency)
317 R = color::ColorChannelMerge(R, rMergeColor.R, cTransparency);
318 G = color::ColorChannelMerge(G, rMergeColor.G, cTransparency);
319 B = color::ColorChannelMerge(B, rMergeColor.B, cTransparency);
322 /* Change of format */
324 /** Color space conversion tools
325 * The range for h/s/b is:
326 * - Hue: 0-360 degree
327 * - Saturation: 0-100%
328 * - Brightness: 0-100%
329 * @param nHue
330 * @param nSaturation
331 * @param nBrightness
332 * @return rgb color
334 static Color HSBtoRGB(sal_uInt16 nHue, sal_uInt16 nSaturation, sal_uInt16 nBrightness);
336 /** Converts a string into a color. Supports:
337 * #RRGGBB
338 * #rrggbb
339 * #RGB
340 * #rgb
341 * RRGGBB
342 * rrggbb
343 * RGB
344 * rgb
345 * If fails returns Color().
347 static Color STRtoRGB(const OUString& colorname);
349 /** Color space conversion tools
350 * @param nHue
351 * @param nSaturation
352 * @param nBrightness
354 void RGBtoHSB(sal_uInt16& nHue, sal_uInt16& nSaturation, sal_uInt16& nBrightness) const;
356 /* Return color as RGB hex string: rrggbb
357 * for example "00ff00" for green color
358 * @return hex string
360 OUString AsRGBHexString() const;
362 /* Return color as RGB hex string: RRGGBB
363 * for example "00FF00" for green color
364 * @return hex string
366 OUString AsRGBHEXString() const;
368 /* get ::basegfx::BColor from this color
369 * @return basegfx color
371 basegfx::BColor getBColor() const
373 return basegfx::BColor(R / 255.0, G / 255.0, B / 255.0);
377 // to reduce the noise when moving these into and out of Any
378 inline bool operator >>=( const css::uno::Any & rAny, Color & value )
380 sal_Int32 nTmp = {}; // spurious -Werror=maybe-uninitialized
381 if (!(rAny >>= nTmp))
382 return false;
383 value = Color(nTmp);
384 return true;
387 inline void operator <<=( css::uno::Any & rAny, Color value )
389 rAny <<= sal_Int32(value);
392 namespace com::sun::star::uno {
393 template<>
394 inline Any makeAny( Color const & value )
396 return Any(sal_Int32(value));
400 // Test compile time conversion of Color to sal_uInt32
402 static_assert (sal_uInt32(Color(0x00, 0x12, 0x34, 0x56)) == 0x00123456);
403 static_assert (sal_uInt32(Color(0x12, 0x34, 0x56)) == 0x00123456);
405 // Color types
407 constexpr ::Color COL_BLACK ( 0x00, 0x00, 0x00 );
408 constexpr ::Color COL_BLUE ( 0x00, 0x00, 0x80 );
409 constexpr ::Color COL_GREEN ( 0x00, 0x80, 0x00 );
410 constexpr ::Color COL_CYAN ( 0x00, 0x80, 0x80 );
411 constexpr ::Color COL_RED ( 0x80, 0x00, 0x00 );
412 constexpr ::Color COL_MAGENTA ( 0x80, 0x00, 0x80 );
413 constexpr ::Color COL_BROWN ( 0x80, 0x80, 0x00 );
414 constexpr ::Color COL_GRAY ( 0x80, 0x80, 0x80 );
415 constexpr ::Color COL_GRAY3 ( 0xCC, 0xCC, 0xCC );
416 constexpr ::Color COL_GRAY7 ( 0x66, 0x66, 0x66 );
417 constexpr ::Color COL_LIGHTGRAY ( 0xC0, 0xC0, 0xC0 );
418 constexpr ::Color COL_LIGHTBLUE ( 0x00, 0x00, 0xFF );
419 constexpr ::Color COL_LIGHTGREEN ( 0x00, 0xFF, 0x00 );
420 constexpr ::Color COL_LIGHTCYAN ( 0x00, 0xFF, 0xFF );
421 constexpr ::Color COL_LIGHTRED ( 0xFF, 0x00, 0x00 );
422 constexpr ::Color COL_LIGHTMAGENTA ( 0xFF, 0x00, 0xFF );
423 constexpr ::Color COL_LIGHTGRAYBLUE ( 0xE0, 0xE0, 0xFF );
424 constexpr ::Color COL_YELLOW ( 0xFF, 0xFF, 0x00 );
425 constexpr ::Color COL_WHITE ( 0xFF, 0xFF, 0xFF );
426 constexpr ::Color COL_TRANSPARENT ( 0xFF, 0xFF, 0xFF, 0xFF );
427 constexpr ::Color COL_AUTO ( 0xFF, 0xFF, 0xFF, 0xFF );
428 constexpr ::Color COL_AUTHOR1_DARK ( 198, 146, 0 );
429 constexpr ::Color COL_AUTHOR1_NORMAL ( 255, 255, 158 );
430 constexpr ::Color COL_AUTHOR1_LIGHT ( 255, 255, 195 );
431 constexpr ::Color COL_AUTHOR2_DARK ( 6, 70, 162 );
432 constexpr ::Color COL_AUTHOR2_NORMAL ( 216, 232, 255 );
433 constexpr ::Color COL_AUTHOR2_LIGHT ( 233, 242, 255 );
434 constexpr ::Color COL_AUTHOR3_DARK ( 87, 157, 28 );
435 constexpr ::Color COL_AUTHOR3_NORMAL ( 218, 248, 193 );
436 constexpr ::Color COL_AUTHOR3_LIGHT ( 226, 250, 207 );
437 constexpr ::Color COL_AUTHOR4_DARK ( 105, 43, 157 );
438 constexpr ::Color COL_AUTHOR4_NORMAL ( 228, 210, 245 );
439 constexpr ::Color COL_AUTHOR4_LIGHT ( 239, 228, 248 );
440 constexpr ::Color COL_AUTHOR5_DARK ( 197, 0, 11 );
441 constexpr ::Color COL_AUTHOR5_NORMAL ( 254, 205, 208 );
442 constexpr ::Color COL_AUTHOR5_LIGHT ( 255, 227, 229 );
443 constexpr ::Color COL_AUTHOR6_DARK ( 0, 128, 128 );
444 constexpr ::Color COL_AUTHOR6_NORMAL ( 210, 246, 246 );
445 constexpr ::Color COL_AUTHOR6_LIGHT ( 230, 250, 250 );
446 constexpr ::Color COL_AUTHOR7_DARK ( 140, 132, 0 );
447 constexpr ::Color COL_AUTHOR7_NORMAL ( 237, 252, 163 );
448 constexpr ::Color COL_AUTHOR7_LIGHT ( 242, 254, 181 );
449 constexpr ::Color COL_AUTHOR8_DARK ( 53, 85, 107 );
450 constexpr ::Color COL_AUTHOR8_NORMAL ( 211, 222, 232 );
451 constexpr ::Color COL_AUTHOR8_LIGHT ( 226, 234, 241 );
452 constexpr ::Color COL_AUTHOR9_DARK ( 209, 118, 0 );
453 constexpr ::Color COL_AUTHOR9_NORMAL ( 255, 226, 185 );
454 constexpr ::Color COL_AUTHOR9_LIGHT ( 255, 231, 199 );
456 template<typename charT, typename traits>
457 inline std::basic_ostream<charT, traits>& operator <<(std::basic_ostream<charT, traits>& rStream, const Color& rColor)
459 std::ios_base::fmtflags nOrigFlags = rStream.flags();
460 rStream << "c[" << std::hex << std::setfill ('0')
461 << std::setw(2) << static_cast<int>(rColor.GetRed())
462 << std::setw(2) << static_cast<int>(rColor.GetGreen())
463 << std::setw(2) << static_cast<int>(rColor.GetBlue())
464 << std::setw(2) << static_cast<int>(rColor.GetTransparency()) << "]";
465 rStream.setf(nOrigFlags);
466 return rStream;
469 #endif
471 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */