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/.
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 <config_global.h>
26 #include <basegfx/color/bcolor.hxx>
27 #include <osl/endian.h>
32 constexpr sal_uInt32
extractRGB(sal_uInt32 nColorNumber
)
34 return nColorNumber
& 0x00FFFFFF;
37 constexpr sal_uInt8
ColorChannelMerge(sal_uInt8 nDst
, sal_uInt8 nSrc
, sal_uInt8 nSrcTrans
)
39 return sal_uInt8(((sal_Int32(nDst
) - nSrc
) * nSrcTrans
+ ((nSrc
<< 8) | nDst
)) >> 8);
44 /** used to deliberately select the right constructor */
45 enum ColorTransparencyTag
{ ColorTransparency
= 0 };
46 enum ColorAlphaTag
{ ColorAlpha
= 0 };
50 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Color
76 #if HAVE_CPP_CONSTEVAL
81 Color(const sal_uInt32 nColor
)
84 assert(nColor
<= 0xffffff && "don't pass transparency to this constructor, use the Color(ColorTransparencyTag,...) or Color(ColorAlphaTag,...) constructor to make it explicit");
87 constexpr Color(enum ColorTransparencyTag
, sal_uInt32 nColor
)
92 constexpr Color(enum ColorAlphaTag
, sal_uInt32 nColor
)
93 : mValue((nColor
& 0xffffff) | ((255 - (nColor
>> 24)) << 24))
97 constexpr Color(enum ColorTransparencyTag
, sal_uInt8 nTransparency
, sal_uInt8 nRed
, sal_uInt8 nGreen
, sal_uInt8 nBlue
)
98 : mValue(sal_uInt32(nBlue
) | (sal_uInt32(nGreen
) << 8) | (sal_uInt32(nRed
) << 16) | (sal_uInt32(nTransparency
) << 24))
101 constexpr Color(enum ColorAlphaTag
, sal_uInt8 nAlpha
, sal_uInt8 nRed
, sal_uInt8 nGreen
, sal_uInt8 nBlue
)
102 : Color(ColorTransparency
, 255 - nAlpha
, nRed
, nGreen
, nBlue
)
105 constexpr Color(sal_uInt8 nRed
, sal_uInt8 nGreen
, sal_uInt8 nBlue
)
106 : Color(ColorTransparency
, 0, nRed
, nGreen
, nBlue
)
109 // constructor to create a tools-Color from ::basegfx::BColor
110 explicit Color(const basegfx::BColor
& rBColor
)
111 : Color(ColorTransparency
, 0,
112 sal_uInt8(std::lround(rBColor
.getRed() * 255.0)),
113 sal_uInt8(std::lround(rBColor
.getGreen() * 255.0)),
114 sal_uInt8(std::lround(rBColor
.getBlue() * 255.0)))
117 /** Casts the color to corresponding uInt32.
118 * Primarily used when passing Color objects to UNO API
119 * @return corresponding sal_uInt32
121 constexpr explicit operator sal_uInt32() const
126 /** Casts the color to corresponding iInt32.
127 * If there is no transparency, will be positive.
128 * @return corresponding sal_Int32
130 constexpr explicit operator sal_Int32() const
132 return sal_Int32(mValue
);
135 /* Basic RGBA operations */
137 /** Gets the red value.
140 sal_uInt8
GetRed() const
145 /** Gets the green value.
148 sal_uInt8
GetGreen() const
153 /** Gets the blue value.
156 sal_uInt8
GetBlue() const
161 /** Gets the alpha value.
164 sal_uInt8
GetAlpha() const
169 /** Is the color transparent?
171 bool IsTransparent() const
173 return GetAlpha() != 255;
176 /** Is the color fully transparent i.e. 100% transparency ?
178 bool IsFullyTransparent() const
183 /** Sets the red value.
186 void SetRed(sal_uInt8 nRed
)
191 /** Sets the green value.
194 void SetGreen(sal_uInt8 nGreen
)
199 /** Sets the blue value.
202 void SetBlue(sal_uInt8 nBlue
)
207 /** Sets the alpha value.
210 void SetAlpha(sal_uInt8 nAlpha
)
215 /** Returns the same color but ignoring the transparency value.
216 * @return RGB version
218 Color
GetRGBColor() const
223 /* Comparison and operators */
225 /** Check if the color RGB value is equal than rColor.
229 bool IsRGBEqual( const Color
& rColor
) const
231 return ( mValue
& 0x00FFFFFF ) == ( rColor
.mValue
& 0x00FFFFFF );
234 /** Check if the color value is lower than aCompareColor.
235 * @param aCompareColor
238 bool operator<(const Color
& aCompareColor
) const
240 return mValue
< aCompareColor
.mValue
;
243 /** Check if the color value is greater than aCompareColor.
244 * @param aCompareColor
247 bool operator>(const Color
& aCompareColor
) const
249 return mValue
> aCompareColor
.mValue
;
252 /** Check if the color value is equal than rColor.
256 bool operator==(const Color
& rColor
) const
258 return mValue
== rColor
.mValue
;
261 /** Check if the color value is unequal than rColor.
265 bool operator!=(const Color
& rColor
) const
267 return mValue
!= rColor
.mValue
;
270 /** Gets the color error compared to another.
271 * It describes how different they are.
272 * It takes the abs of differences in parameters.
273 * @param rCompareColor
276 sal_uInt16
GetColorError(const Color
& rCompareColor
) const
278 return static_cast<sal_uInt16
>(
279 abs(static_cast<int>(GetBlue()) - rCompareColor
.GetBlue()) +
280 abs(static_cast<int>(GetGreen()) - rCompareColor
.GetGreen()) +
281 abs(static_cast<int>(GetRed()) - rCompareColor
.GetRed()));
284 /* Light and contrast */
286 /** Gets the color luminance. It means perceived brightness.
289 sal_uInt8
GetLuminance() const
291 return sal_uInt8((B
* 29UL + G
* 151UL + R
* 76UL) >> 8);
294 /** Increases the color luminance by cLumInc.
297 void IncreaseLuminance(sal_uInt8 cLumInc
);
299 /** Decreases the color luminance by cLumDec.
302 void DecreaseLuminance(sal_uInt8 cLumDec
);
304 /** Decreases color contrast with white by cContDec.
307 void DecreaseContrast(sal_uInt8 cContDec
);
309 /** Comparison with luminance thresholds.
314 // 62 is the number that means it also triggers on Ubuntu in dark mode
315 return GetLuminance() <= 62;
318 /** Comparison with luminance thresholds.
321 bool IsBright() const
323 return GetLuminance() >= 245;
329 * Apply tint or shade to a color.
331 * The input value is the percentage (in 100th of percent) of how much the
332 * color changes towards the black (shade) or white (tint). If the value
333 * is positive, the color is tinted, if the value is negative, the color is
336 void ApplyTintOrShade(sal_Int16 n100thPercent
);
339 * Apply luminance offset and/or modulation.
341 * The input values are in percentages (in 100th percents). 100% modulation and 0% offset
342 * results in no change.
344 void ApplyLumModOff(sal_Int16 nMod
, sal_Int16 nOff
);
346 /** Inverts color. 1 and 0 are switched.
347 * Note that the result will be the complementary color.
348 * For example, if you have red, you will get cyan: FF0000 -> 00FFFF.
357 /** Merges color with rMergeColor.
358 * Allows to get resulting color when superposing another.
360 * @param cTransparency
362 void Merge(const Color
& rMergeColor
, sal_uInt8 cTransparency
)
364 R
= color::ColorChannelMerge(R
, rMergeColor
.R
, cTransparency
);
365 G
= color::ColorChannelMerge(G
, rMergeColor
.G
, cTransparency
);
366 B
= color::ColorChannelMerge(B
, rMergeColor
.B
, cTransparency
);
369 /* Change of format */
371 /** Color space conversion tools
372 * The range for h/s/b is:
373 * - Hue: 0-360 degree
374 * - Saturation: 0-100%
375 * - Brightness: 0-100%
381 static Color
HSBtoRGB(sal_uInt16 nHue
, sal_uInt16 nSaturation
, sal_uInt16 nBrightness
);
383 /** Converts a string into a color. Supports:
392 * If fails returns Color().
394 static Color
STRtoRGB(std::u16string_view colorname
);
396 /** Color space conversion tools
401 void RGBtoHSB(sal_uInt16
& nHue
, sal_uInt16
& nSaturation
, sal_uInt16
& nBrightness
) const;
403 /* Return color as RGB hex string: rrggbb
404 * for example "00ff00" for green color
407 OUString
AsRGBHexString() const;
409 /* Return color as RGB hex string: RRGGBB
410 * for example "00FF00" for green color
413 OUString
AsRGBHEXString() const;
415 /* get ::basegfx::BColor from this color
416 * @return basegfx color
418 basegfx::BColor
getBColor() const
420 return basegfx::BColor(R
/ 255.0, G
/ 255.0, B
/ 255.0);
424 // to reduce the noise when moving these into and out of Any
425 inline bool operator >>=( const css::uno::Any
& rAny
, Color
& value
)
427 sal_Int32 nTmp
= {}; // spurious -Werror=maybe-uninitialized
428 if (!(rAny
>>= nTmp
))
430 value
= Color(ColorTransparency
, nTmp
);
434 inline void operator <<=( css::uno::Any
& rAny
, Color value
)
436 rAny
<<= sal_Int32(value
);
439 namespace com::sun::star::uno
{
440 template<> inline Any::Any(Color
const & value
): Any(sal_Int32(value
)) {}
443 // Test compile time conversion of Color to sal_uInt32
445 static_assert (sal_uInt32(Color(ColorTransparency
, 0x00, 0x12, 0x34, 0x56)) == 0x00123456);
446 static_assert (sal_uInt32(Color(0x12, 0x34, 0x56)) == 0x00123456);
450 inline constexpr ::Color
COL_TRANSPARENT ( ColorTransparency
, 0xFF, 0xFF, 0xFF, 0xFF );
451 inline constexpr ::Color
COL_AUTO ( ColorTransparency
, 0xFF, 0xFF, 0xFF, 0xFF );
452 inline constexpr ::Color
COL_BLACK ( 0x00, 0x00, 0x00 );
453 inline constexpr ::Color
COL_BLUE ( 0x00, 0x00, 0x80 );
454 inline constexpr ::Color
COL_GREEN ( 0x00, 0x80, 0x00 );
455 inline constexpr ::Color
COL_CYAN ( 0x00, 0x80, 0x80 );
456 inline constexpr ::Color
COL_RED ( 0x80, 0x00, 0x00 );
457 inline constexpr ::Color
COL_MAGENTA ( 0x80, 0x00, 0x80 );
458 inline constexpr ::Color
COL_BROWN ( 0x80, 0x80, 0x00 );
459 inline constexpr ::Color
COL_GRAY ( 0x80, 0x80, 0x80 );
460 inline constexpr ::Color
COL_GRAY3 ( 0xCC, 0xCC, 0xCC );
461 inline constexpr ::Color
COL_GRAY7 ( 0x66, 0x66, 0x66 );
462 inline constexpr ::Color
COL_LIGHTGRAY ( 0xC0, 0xC0, 0xC0 );
463 inline constexpr ::Color
COL_LIGHTBLUE ( 0x00, 0x00, 0xFF );
464 inline constexpr ::Color
COL_LIGHTGREEN ( 0x00, 0xFF, 0x00 );
465 inline constexpr ::Color
COL_LIGHTCYAN ( 0x00, 0xFF, 0xFF );
466 inline constexpr ::Color
COL_LIGHTRED ( 0xFF, 0x00, 0x00 );
467 inline constexpr ::Color
COL_LIGHTMAGENTA ( 0xFF, 0x00, 0xFF );
468 inline constexpr ::Color
COL_LIGHTGRAYBLUE ( 0xE0, 0xE0, 0xFF );
469 inline constexpr ::Color
COL_YELLOW ( 0xFF, 0xFF, 0x00 );
470 inline constexpr ::Color
COL_WHITE ( 0xFF, 0xFF, 0xFF );
471 inline constexpr ::Color
COL_AUTHOR1_DARK ( 0xC6, 0x92, 0x00 );
472 inline constexpr ::Color
COL_AUTHOR1_NORMAL ( 0xFF, 0xFF, 0x9E );
473 inline constexpr ::Color
COL_AUTHOR1_LIGHT ( 0xFF, 0xFF, 0xC3 );
474 inline constexpr ::Color
COL_AUTHOR2_DARK ( 0x06, 0x46, 0xA2 );
475 inline constexpr ::Color
COL_AUTHOR2_NORMAL ( 0xD8, 0xE8, 0xFF );
476 inline constexpr ::Color
COL_AUTHOR2_LIGHT ( 0xE9, 0xF2, 0xFF );
477 inline constexpr ::Color
COL_AUTHOR3_DARK ( 0x57, 0x9D, 0x1C );
478 inline constexpr ::Color
COL_AUTHOR3_NORMAL ( 0xDA, 0xF8, 0xC1 );
479 inline constexpr ::Color
COL_AUTHOR3_LIGHT ( 0xE2, 0xFA, 0xCF );
480 inline constexpr ::Color
COL_AUTHOR4_DARK ( 0x69, 0x2B, 0x9D );
481 inline constexpr ::Color
COL_AUTHOR4_NORMAL ( 0xE4, 0xD2, 0xF5 );
482 inline constexpr ::Color
COL_AUTHOR4_LIGHT ( 0xEF, 0xE4, 0xF8 );
483 inline constexpr ::Color
COL_AUTHOR5_DARK ( 0xC5, 0x00, 0x0B );
484 inline constexpr ::Color
COL_AUTHOR5_NORMAL ( 0xFE, 0xCD, 0xD0 );
485 inline constexpr ::Color
COL_AUTHOR5_LIGHT ( 0xFF, 0xE3, 0xE5 );
486 inline constexpr ::Color
COL_AUTHOR6_DARK ( 0x00, 0x80, 0x80 );
487 inline constexpr ::Color
COL_AUTHOR6_NORMAL ( 0xD2, 0xF6, 0xF6 );
488 inline constexpr ::Color
COL_AUTHOR6_LIGHT ( 0xE6, 0xFA, 0xFA );
489 inline constexpr ::Color
COL_AUTHOR7_DARK ( 0x8C, 0x84, 0x00 );
490 inline constexpr ::Color
COL_AUTHOR7_NORMAL ( 0xED, 0xFC, 0xA3 );
491 inline constexpr ::Color
COL_AUTHOR7_LIGHT ( 0xF2, 0xFE, 0xB5 );
492 inline constexpr ::Color
COL_AUTHOR8_DARK ( 0x35, 0x55, 0x6B );
493 inline constexpr ::Color
COL_AUTHOR8_NORMAL ( 0xD3, 0xDE, 0xE8 );
494 inline constexpr ::Color
COL_AUTHOR8_LIGHT ( 0xE2, 0xEA, 0xF1 );
495 inline constexpr ::Color
COL_AUTHOR9_DARK ( 0xD1, 0x76, 0x00 );
496 inline constexpr ::Color
COL_AUTHOR9_NORMAL ( 0xFF, 0xE2, 0xB9 );
497 inline constexpr ::Color
COL_AUTHOR9_LIGHT ( 0xFF, 0xE7, 0xC7 );
498 inline constexpr ::Color
COL_AUTHOR_TABLE_INS ( 0xE1, 0xF2, 0xFA );
499 inline constexpr ::Color
COL_AUTHOR_TABLE_DEL ( 0xFC, 0xE6, 0xF4 );
501 template<typename charT
, typename traits
>
502 inline std::basic_ostream
<charT
, traits
>& operator <<(std::basic_ostream
<charT
, traits
>& rStream
, const Color
& rColor
)
504 std::ios_base::fmtflags nOrigFlags
= rStream
.flags();
505 rStream
<< "rgba[" << std::hex
<< std::setfill ('0')
506 << std::setw(2) << static_cast<int>(rColor
.GetRed())
507 << std::setw(2) << static_cast<int>(rColor
.GetGreen())
508 << std::setw(2) << static_cast<int>(rColor
.GetBlue())
509 << std::setw(2) << static_cast<int>(rColor
.GetAlpha()) << "]";
510 rStream
.setf(nOrigFlags
);
516 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */