1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19 #ifndef INCLUDED_TOOLS_COLOR_HXX
20 #define INCLUDED_TOOLS_COLOR_HXX
21
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>
28
29 namespace color
30 {
31
extractRGB(sal_uInt32 nColorNumber)32 constexpr sal_uInt32 extractRGB(sal_uInt32 nColorNumber)
33 {
34 return nColorNumber & 0x00FFFFFF;
35 }
36
ColorChannelMerge(sal_uInt8 nDst,sal_uInt8 nSrc,sal_uInt8 nSrcTrans)37 constexpr sal_uInt8 ColorChannelMerge(sal_uInt8 nDst, sal_uInt8 nSrc, sal_uInt8 nSrcTrans)
38 {
39 return sal_uInt8(((sal_Int32(nDst) - nSrc) * nSrcTrans + ((nSrc << 8) | nDst)) >> 8);
40 }
41
42 }
43
44 /** used to deliberately select the right constructor */
45 enum ColorTransparencyTag { ColorTransparency = 0 };
46 enum ColorAlphaTag { ColorAlpha = 0 };
47
48 // Color
49
50 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Color
51 {
52 union
53 {
54 sal_uInt32 mValue;
55 struct
56 {
57 #ifdef OSL_BIGENDIAN
58 sal_uInt8 T;
59 sal_uInt8 R;
60 sal_uInt8 G;
61 sal_uInt8 B;
62 #else
63 sal_uInt8 B;
64 sal_uInt8 G;
65 sal_uInt8 R;
66 sal_uInt8 T;
67 #endif
68 };
69 };
70
71 public:
Color()72 constexpr Color()
73 : mValue(0) // black
74 {}
75
76 #if HAVE_CPP_CONSTEVAL
77 consteval
78 #else
79 constexpr
80 #endif
Color(const sal_uInt32 nColor)81 Color(const sal_uInt32 nColor)
82 : mValue(nColor)
83 {
84 assert(nColor <= 0xffffff && "don't pass transparency to this constructor, use the Color(ColorTransparencyTag,...) or Color(ColorAlphaTag,...) constructor to make it explicit");
85 }
86
Color(enum ColorTransparencyTag,sal_uInt32 nColor)87 constexpr Color(enum ColorTransparencyTag, sal_uInt32 nColor)
88 : mValue(nColor)
89 {
90 }
91
Color(enum ColorAlphaTag,sal_uInt32 nColor)92 constexpr Color(enum ColorAlphaTag, sal_uInt32 nColor)
93 : mValue((nColor & 0xffffff) | ((255 - (nColor >> 24)) << 24))
94 {
95 }
96
Color(enum ColorTransparencyTag,sal_uInt8 nTransparency,sal_uInt8 nRed,sal_uInt8 nGreen,sal_uInt8 nBlue)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))
99 {}
100
Color(enum ColorAlphaTag,sal_uInt8 nAlpha,sal_uInt8 nRed,sal_uInt8 nGreen,sal_uInt8 nBlue)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)
103 {}
104
Color(sal_uInt8 nRed,sal_uInt8 nGreen,sal_uInt8 nBlue)105 constexpr Color(sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
106 : Color(ColorTransparency, 0, nRed, nGreen, nBlue)
107 {}
108
109 // constructor to create a tools-Color from ::basegfx::BColor
Color(const basegfx::BColor & rBColor)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)))
115 {}
116
117 /** Casts the color to corresponding uInt32.
118 * Primarily used when passing Color objects to UNO API
119 * @return corresponding sal_uInt32
120 */
operator sal_uInt32() const121 constexpr explicit operator sal_uInt32() const
122 {
123 return mValue;
124 }
125
126 /** Casts the color to corresponding iInt32.
127 * If there is no transparency, will be positive.
128 * @return corresponding sal_Int32
129 */
operator sal_Int32() const130 constexpr explicit operator sal_Int32() const
131 {
132 return sal_Int32(mValue);
133 }
134
135 /* Basic RGBA operations */
136
137 /** Gets the red value.
138 * @return R
139 */
GetRed() const140 sal_uInt8 GetRed() const
141 {
142 return R;
143 }
144
145 /** Gets the green value.
146 * @return G
147 */
GetGreen() const148 sal_uInt8 GetGreen() const
149 {
150 return G;
151 }
152
153 /** Gets the blue value.
154 * @return B
155 */
GetBlue() const156 sal_uInt8 GetBlue() const
157 {
158 return B;
159 }
160
161 /** Gets the alpha value.
162 * @return A
163 */
GetAlpha() const164 sal_uInt8 GetAlpha() const
165 {
166 return 255 - T;
167 }
168
169 /** Is the color transparent?
170 */
IsTransparent() const171 bool IsTransparent() const
172 {
173 return GetAlpha() != 255;
174 }
175
176 /** Is the color fully transparent i.e. 100% transparency ?
177 */
IsFullyTransparent() const178 bool IsFullyTransparent() const
179 {
180 return GetAlpha() == 0;
181 }
182
183 /** Sets the red value.
184 * @param nRed
185 */
SetRed(sal_uInt8 nRed)186 void SetRed(sal_uInt8 nRed)
187 {
188 R = nRed;
189 }
190
191 /** Sets the green value.
192 * @param nGreen
193 */
SetGreen(sal_uInt8 nGreen)194 void SetGreen(sal_uInt8 nGreen)
195 {
196 G = nGreen;
197 }
198
199 /** Sets the blue value.
200 * @param nBlue
201 */
SetBlue(sal_uInt8 nBlue)202 void SetBlue(sal_uInt8 nBlue)
203 {
204 B = nBlue;
205 }
206
207 /** Sets the alpha value.
208 * @param nAlpha
209 */
SetAlpha(sal_uInt8 nAlpha)210 void SetAlpha(sal_uInt8 nAlpha)
211 {
212 T = 255 - nAlpha;
213 }
214
215 /** Returns the same color but ignoring the transparency value.
216 * @return RGB version
217 */
GetRGBColor() const218 Color GetRGBColor() const
219 {
220 return { GetRed(), GetGreen(), GetBlue() };
221 }
222
223 /* Comparison and operators */
224
225 /** Check if the color RGB value is equal than rColor.
226 * @param rColor
227 * @return is equal
228 */
IsRGBEqual(const Color & rColor) const229 bool IsRGBEqual( const Color& rColor ) const
230 {
231 return ( mValue & 0x00FFFFFF ) == ( rColor.mValue & 0x00FFFFFF );
232 }
233
234 /** Check if the color value is lower than aCompareColor.
235 * @param aCompareColor
236 * @return is lower
237 */
operator <(const Color & aCompareColor) const238 bool operator<(const Color& aCompareColor) const
239 {
240 return mValue < aCompareColor.mValue;
241 }
242
243 /** Check if the color value is greater than aCompareColor.
244 * @param aCompareColor
245 * @return is greater
246 */
operator >(const Color & aCompareColor) const247 bool operator>(const Color& aCompareColor) const
248 {
249 return mValue > aCompareColor.mValue;
250 }
251
252 /** Check if the color value is equal than rColor.
253 * @param rColor
254 * @return is equal
255 */
operator ==(const Color & rColor) const256 bool operator==(const Color& rColor) const
257 {
258 return mValue == rColor.mValue;
259 }
260
261 /** Gets the color error compared to another.
262 * It describes how different they are.
263 * It takes the abs of differences in parameters.
264 * @param rCompareColor
265 * @return error
266 */
GetColorError(const Color & rCompareColor) const267 sal_uInt16 GetColorError(const Color& rCompareColor) const
268 {
269 return static_cast<sal_uInt16>(
270 abs(static_cast<int>(GetBlue()) - rCompareColor.GetBlue()) +
271 abs(static_cast<int>(GetGreen()) - rCompareColor.GetGreen()) +
272 abs(static_cast<int>(GetRed()) - rCompareColor.GetRed()));
273 }
274
275 /* Light and contrast */
276
277 /** Gets the color luminance. It means perceived brightness.
278 * @return luminance
279 */
GetLuminance() const280 sal_uInt8 GetLuminance() const
281 {
282 return sal_uInt8((GetBlue() * 29UL + GetGreen() * 151UL + GetRed() * 76UL) >> 8);
283 }
284
285 /** Increases the color luminance by cLumInc.
286 * @param cLumInc
287 */
288 void IncreaseLuminance(sal_uInt8 cLumInc);
289
290 /** Decreases the color luminance by cLumDec.
291 * @param cLumDec
292 */
293 void DecreaseLuminance(sal_uInt8 cLumDec);
294
295 /** Decreases color contrast with white by cContDec.
296 * @param cContDec
297 */
298 void DecreaseContrast(sal_uInt8 cContDec);
299
300 /** Comparison with luminance thresholds.
301 * @return is dark
302 */
IsDark() const303 bool IsDark() const
304 {
305 // tdf#156182, and band aid for follow-up issues
306 if (mValue == 0x729fcf) // COL_DEFAULT_SHAPE_FILLING
307 return GetLuminance() <= 62;
308 else
309 return GetLuminance() <= 156;
310 }
311
312 /** Comparison with luminance thresholds.
313 * @return is dark
314 */
IsBright() const315 bool IsBright() const
316 {
317 return GetLuminance() >= 245;
318 }
319
320 /* Color filters */
321
322 /**
323 * Apply tint or shade to a color.
324 *
325 * The input value is the percentage (in 100th of percent) of how much the
326 * color changes towards the black (shade) or white (tint). If the value
327 * is positive, the color is tinted, if the value is negative, the color is
328 * shaded.
329 **/
330 void ApplyTintOrShade(sal_Int16 n100thPercent);
331
332 /**
333 * Apply luminance offset and/or modulation.
334 *
335 * The input values are in percentages (in 100th percents). 100% modulation and 0% offset
336 * results in no change.
337 */
338 void ApplyLumModOff(sal_Int16 nMod, sal_Int16 nOff);
339
340 /** Inverts color. 1 and 0 are switched.
341 * Note that the result will be the complementary color.
342 * For example, if you have red, you will get cyan: FF0000 -> 00FFFF.
343 */
Invert()344 void Invert()
345 {
346 SetRed(~GetRed());
347 SetGreen(~GetGreen());
348 SetBlue(~GetBlue());
349 }
350
351 /** Merges color with rMergeColor.
352 * Allows to get resulting color when superposing another.
353 * @param rMergeColor
354 * @param cTransparency
355 */
Merge(const Color & rMergeColor,sal_uInt8 cTransparency)356 void Merge(const Color& rMergeColor, sal_uInt8 cTransparency)
357 {
358 SetRed(color::ColorChannelMerge(GetRed(), rMergeColor.GetRed(), cTransparency));
359 SetGreen(color::ColorChannelMerge(GetGreen(), rMergeColor.GetGreen(), cTransparency));
360 SetBlue(color::ColorChannelMerge(GetBlue(), rMergeColor.GetBlue(), cTransparency));
361 }
362
363 /* Change of format */
364
365 /** Color space conversion tools
366 * The range for h/s/b is:
367 * - Hue: 0-360 degree
368 * - Saturation: 0-100%
369 * - Brightness: 0-100%
370 * @param nHue
371 * @param nSaturation
372 * @param nBrightness
373 * @return rgb color
374 */
375 static Color HSBtoRGB(sal_uInt16 nHue, sal_uInt16 nSaturation, sal_uInt16 nBrightness);
376
377 /** Converts a string into a color. Supports:
378 * #RRGGBB
379 * #rrggbb
380 * #RGB
381 * #rgb
382 * RRGGBB
383 * rrggbb
384 * RGB
385 * rgb
386 * If fails returns Color().
387 */
388 static Color STRtoRGB(std::u16string_view colorname);
389
390 /** Color space conversion tools
391 * @param nHue
392 * @param nSaturation
393 * @param nBrightness
394 */
395 void RGBtoHSB(sal_uInt16& nHue, sal_uInt16& nSaturation, sal_uInt16& nBrightness) const;
396
397 /* Return color as RGB hex string: rrggbb
398 * for example "00ff00" for green color
399 * @return hex string
400 */
401 OUString AsRGBHexString() const;
402
403 /* Return color as RGB hex string: RRGGBB
404 * for example "00FF00" for green color
405 * @return hex string
406 */
407 OUString AsRGBHEXString() const;
408
409 /* get ::basegfx::BColor from this color
410 * @return basegfx color
411 */
getBColor() const412 basegfx::BColor getBColor() const
413 {
414 basegfx::BColor aColor(GetRed() / 255.0, GetGreen() / 255.0, GetBlue() / 255.0);
415 if (mValue == Color(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF).mValue)
416 aColor.setAutomatic(true);
417 return aColor;
418 }
419 };
420
421 // to reduce the noise when moving these into and out of Any
operator >>=(const css::uno::Any & rAny,Color & value)422 inline bool operator >>=( const css::uno::Any & rAny, Color & value )
423 {
424 sal_Int32 nTmp = {}; // spurious -Werror=maybe-uninitialized
425 if (!(rAny >>= nTmp))
426 return false;
427 value = Color(ColorTransparency, nTmp);
428 return true;
429 }
430
operator <<=(css::uno::Any & rAny,Color value)431 inline void operator <<=( css::uno::Any & rAny, Color value )
432 {
433 rAny <<= sal_Int32(value);
434 }
435
436 namespace com::sun::star::uno {
Any(Color const & value)437 template<> inline Any::Any(Color const & value): Any(sal_Int32(value)) {}
438 }
439
440 // Test compile time conversion of Color to sal_uInt32
441
442 static_assert (sal_uInt32(Color(ColorTransparency, 0x00, 0x12, 0x34, 0x56)) == 0x00123456);
443 static_assert (sal_uInt32(Color(0x12, 0x34, 0x56)) == 0x00123456);
444
445 // Color types
446
447 inline constexpr ::Color COL_TRANSPARENT ( ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF );
448 inline constexpr ::Color COL_AUTO ( ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF );
449 // These are used when drawing to the separate alpha channel we use in vcl
450 inline constexpr ::Color COL_ALPHA_TRANSPARENT ( 0x00, 0x00, 0x00 );
451 inline constexpr ::Color COL_ALPHA_OPAQUE ( 0xff, 0xff, 0xff );
452
453 inline constexpr ::Color COL_BLACK ( 0x00, 0x00, 0x00 );
454 inline constexpr ::Color COL_BLUE ( 0x00, 0x00, 0x80 );
455 inline constexpr ::Color COL_GREEN ( 0x00, 0x80, 0x00 );
456 inline constexpr ::Color COL_CYAN ( 0x00, 0x80, 0x80 );
457 inline constexpr ::Color COL_RED ( 0x80, 0x00, 0x00 );
458 inline constexpr ::Color COL_MAGENTA ( 0x80, 0x00, 0x80 );
459 inline constexpr ::Color COL_BROWN ( 0x80, 0x80, 0x00 );
460 inline constexpr ::Color COL_GRAY ( 0x80, 0x80, 0x80 );
461 inline constexpr ::Color COL_GRAY3 ( 0xCC, 0xCC, 0xCC );
462 inline constexpr ::Color COL_GRAY7 ( 0x66, 0x66, 0x66 );
463 inline constexpr ::Color COL_LIGHTGRAY ( 0xC0, 0xC0, 0xC0 );
464 inline constexpr ::Color COL_LIGHTBLUE ( 0x00, 0x00, 0xFF );
465 inline constexpr ::Color COL_LIGHTGREEN ( 0x00, 0xFF, 0x00 );
466 inline constexpr ::Color COL_LIGHTCYAN ( 0x00, 0xFF, 0xFF );
467 inline constexpr ::Color COL_LIGHTRED ( 0xFF, 0x00, 0x00 );
468 inline constexpr ::Color COL_LIGHTMAGENTA ( 0xFF, 0x00, 0xFF );
469 inline constexpr ::Color COL_LIGHTGRAYBLUE ( 0xE0, 0xE0, 0xFF );
470 inline constexpr ::Color COL_YELLOW ( 0xFF, 0xFF, 0x00 );
471 inline constexpr ::Color COL_WHITE ( 0xFF, 0xFF, 0xFF );
472 inline constexpr ::Color COL_AUTHOR1_DARK ( 0xC6, 0x92, 0x00 );
473 inline constexpr ::Color COL_AUTHOR1_NORMAL ( 0xFF, 0xFF, 0x9E );
474 inline constexpr ::Color COL_AUTHOR1_LIGHT ( 0xFF, 0xFF, 0xC3 );
475 inline constexpr ::Color COL_AUTHOR2_DARK ( 0x06, 0x46, 0xA2 );
476 inline constexpr ::Color COL_AUTHOR2_NORMAL ( 0xD8, 0xE8, 0xFF );
477 inline constexpr ::Color COL_AUTHOR2_LIGHT ( 0xE9, 0xF2, 0xFF );
478 inline constexpr ::Color COL_AUTHOR3_DARK ( 0x57, 0x9D, 0x1C );
479 inline constexpr ::Color COL_AUTHOR3_NORMAL ( 0xDA, 0xF8, 0xC1 );
480 inline constexpr ::Color COL_AUTHOR3_LIGHT ( 0xE2, 0xFA, 0xCF );
481 inline constexpr ::Color COL_AUTHOR4_DARK ( 0x69, 0x2B, 0x9D );
482 inline constexpr ::Color COL_AUTHOR4_NORMAL ( 0xE4, 0xD2, 0xF5 );
483 inline constexpr ::Color COL_AUTHOR4_LIGHT ( 0xEF, 0xE4, 0xF8 );
484 inline constexpr ::Color COL_AUTHOR5_DARK ( 0xC5, 0x00, 0x0B );
485 inline constexpr ::Color COL_AUTHOR5_NORMAL ( 0xFE, 0xCD, 0xD0 );
486 inline constexpr ::Color COL_AUTHOR5_LIGHT ( 0xFF, 0xE3, 0xE5 );
487 inline constexpr ::Color COL_AUTHOR6_DARK ( 0x00, 0x80, 0x80 );
488 inline constexpr ::Color COL_AUTHOR6_NORMAL ( 0xD2, 0xF6, 0xF6 );
489 inline constexpr ::Color COL_AUTHOR6_LIGHT ( 0xE6, 0xFA, 0xFA );
490 inline constexpr ::Color COL_AUTHOR7_DARK ( 0x8C, 0x84, 0x00 );
491 inline constexpr ::Color COL_AUTHOR7_NORMAL ( 0xED, 0xFC, 0xA3 );
492 inline constexpr ::Color COL_AUTHOR7_LIGHT ( 0xF2, 0xFE, 0xB5 );
493 inline constexpr ::Color COL_AUTHOR8_DARK ( 0x35, 0x55, 0x6B );
494 inline constexpr ::Color COL_AUTHOR8_NORMAL ( 0xD3, 0xDE, 0xE8 );
495 inline constexpr ::Color COL_AUTHOR8_LIGHT ( 0xE2, 0xEA, 0xF1 );
496 inline constexpr ::Color COL_AUTHOR9_DARK ( 0xD1, 0x76, 0x00 );
497 inline constexpr ::Color COL_AUTHOR9_NORMAL ( 0xFF, 0xE2, 0xB9 );
498 inline constexpr ::Color COL_AUTHOR9_LIGHT ( 0xFF, 0xE7, 0xC7 );
499 inline constexpr ::Color COL_AUTHOR_TABLE_INS ( 0xE1, 0xF2, 0xFA );
500 inline constexpr ::Color COL_AUTHOR_TABLE_DEL ( 0xFC, 0xE6, 0xF4 );
501
502 template<typename charT, typename traits>
operator <<(std::basic_ostream<charT,traits> & rStream,const Color & rColor)503 inline std::basic_ostream<charT, traits>& operator <<(std::basic_ostream<charT, traits>& rStream, const Color& rColor)
504 {
505 std::ios_base::fmtflags nOrigFlags = rStream.flags();
506 rStream << "rgba[" << std::hex << std::setfill ('0')
507 << std::setw(2) << static_cast<int>(rColor.GetRed())
508 << std::setw(2) << static_cast<int>(rColor.GetGreen())
509 << std::setw(2) << static_cast<int>(rColor.GetBlue())
510 << std::setw(2) << static_cast<int>(rColor.GetAlpha()) << "]";
511 rStream.setf(nOrigFlags);
512 return rStream;
513 }
514
515 #endif
516
517 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
518