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 T == 255;
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 {R, G, B};
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 /** Check if the color value is unequal than rColor.
262 * @param rColor
263 * @return is unequal
264 */
operator !=(const Color & rColor) const265 bool operator!=(const Color& rColor) const
266 {
267 return mValue != rColor.mValue;
268 }
269
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
274 * @return error
275 */
GetColorError(const Color & rCompareColor) const276 sal_uInt16 GetColorError(const Color& rCompareColor) const
277 {
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()));
282 }
283
284 /* Light and contrast */
285
286 /** Gets the color luminance. It means perceived brightness.
287 * @return luminance
288 */
GetLuminance() const289 sal_uInt8 GetLuminance() const
290 {
291 return sal_uInt8((B * 29UL + G * 151UL + R * 76UL) >> 8);
292 }
293
294 /** Increases the color luminance by cLumInc.
295 * @param cLumInc
296 */
297 void IncreaseLuminance(sal_uInt8 cLumInc);
298
299 /** Decreases the color luminance by cLumDec.
300 * @param cLumDec
301 */
302 void DecreaseLuminance(sal_uInt8 cLumDec);
303
304 /** Decreases color contrast with white by cContDec.
305 * @param cContDec
306 */
307 void DecreaseContrast(sal_uInt8 cContDec);
308
309 /** Comparison with luminance thresholds.
310 * @return is dark
311 */
IsDark() const312 bool IsDark() const
313 {
314 return GetLuminance() <= 60;
315 }
316
317 /** Comparison with luminance thresholds.
318 * @return is dark
319 */
IsBright() const320 bool IsBright() const
321 {
322 return GetLuminance() >= 245;
323 }
324
325 /* Color filters */
326
327 /**
328 * Apply tint or shade to a color.
329 *
330 * The input value is the percentage (in 100th of percent) of how much the
331 * color changes towards the black (shade) or white (tint). If the value
332 * is positive, the color is tinted, if the value is negative, the color is
333 * shaded.
334 **/
335 void ApplyTintOrShade(sal_Int16 n100thPercent);
336
337 /** Inverts color. 1 and 0 are switched.
338 * Note that the result will be the complementary color.
339 * For example, if you have red, you will get cyan: FF0000 -> 00FFFF.
340 */
Invert()341 void Invert()
342 {
343 R = ~R;
344 G = ~G;
345 B = ~B;
346 }
347
348 /** Merges color with rMergeColor.
349 * Allows to get resulting color when superposing another.
350 * @param rMergeColor
351 * @param cTransparency
352 */
Merge(const Color & rMergeColor,sal_uInt8 cTransparency)353 void Merge(const Color& rMergeColor, sal_uInt8 cTransparency)
354 {
355 R = color::ColorChannelMerge(R, rMergeColor.R, cTransparency);
356 G = color::ColorChannelMerge(G, rMergeColor.G, cTransparency);
357 B = color::ColorChannelMerge(B, rMergeColor.B, cTransparency);
358 }
359
360 /* Change of format */
361
362 /** Color space conversion tools
363 * The range for h/s/b is:
364 * - Hue: 0-360 degree
365 * - Saturation: 0-100%
366 * - Brightness: 0-100%
367 * @param nHue
368 * @param nSaturation
369 * @param nBrightness
370 * @return rgb color
371 */
372 static Color HSBtoRGB(sal_uInt16 nHue, sal_uInt16 nSaturation, sal_uInt16 nBrightness);
373
374 /** Converts a string into a color. Supports:
375 * #RRGGBB
376 * #rrggbb
377 * #RGB
378 * #rgb
379 * RRGGBB
380 * rrggbb
381 * RGB
382 * rgb
383 * If fails returns Color().
384 */
385 static Color STRtoRGB(const OUString& colorname);
386
387 /** Color space conversion tools
388 * @param nHue
389 * @param nSaturation
390 * @param nBrightness
391 */
392 void RGBtoHSB(sal_uInt16& nHue, sal_uInt16& nSaturation, sal_uInt16& nBrightness) const;
393
394 /* Return color as RGB hex string: rrggbb
395 * for example "00ff00" for green color
396 * @return hex string
397 */
398 OUString AsRGBHexString() const;
399
400 /* Return color as RGB hex string: RRGGBB
401 * for example "00FF00" for green color
402 * @return hex string
403 */
404 OUString AsRGBHEXString() const;
405
406 /* get ::basegfx::BColor from this color
407 * @return basegfx color
408 */
getBColor() const409 basegfx::BColor getBColor() const
410 {
411 return basegfx::BColor(R / 255.0, G / 255.0, B / 255.0);
412 }
413 };
414
415 // to reduce the noise when moving these into and out of Any
operator >>=(const css::uno::Any & rAny,Color & value)416 inline bool operator >>=( const css::uno::Any & rAny, Color & value )
417 {
418 sal_Int32 nTmp = {}; // spurious -Werror=maybe-uninitialized
419 if (!(rAny >>= nTmp))
420 return false;
421 value = Color(ColorTransparency, nTmp);
422 return true;
423 }
424
operator <<=(css::uno::Any & rAny,Color value)425 inline void operator <<=( css::uno::Any & rAny, Color value )
426 {
427 rAny <<= sal_Int32(value);
428 }
429
430 namespace com::sun::star::uno {
431 template<>
makeAny(Color const & value)432 inline Any makeAny( Color const & value )
433 {
434 return Any(sal_Int32(value));
435 }
436 }
437
438 // Test compile time conversion of Color to sal_uInt32
439
440 static_assert (sal_uInt32(Color(ColorTransparency, 0x00, 0x12, 0x34, 0x56)) == 0x00123456);
441 static_assert (sal_uInt32(Color(0x12, 0x34, 0x56)) == 0x00123456);
442
443 // Color types
444
445 constexpr ::Color COL_BLACK ( 0x00, 0x00, 0x00 );
446 constexpr ::Color COL_BLUE ( 0x00, 0x00, 0x80 );
447 constexpr ::Color COL_GREEN ( 0x00, 0x80, 0x00 );
448 constexpr ::Color COL_CYAN ( 0x00, 0x80, 0x80 );
449 constexpr ::Color COL_RED ( 0x80, 0x00, 0x00 );
450 constexpr ::Color COL_MAGENTA ( 0x80, 0x00, 0x80 );
451 constexpr ::Color COL_BROWN ( 0x80, 0x80, 0x00 );
452 constexpr ::Color COL_GRAY ( 0x80, 0x80, 0x80 );
453 constexpr ::Color COL_GRAY3 ( 0xCC, 0xCC, 0xCC );
454 constexpr ::Color COL_GRAY7 ( 0x66, 0x66, 0x66 );
455 constexpr ::Color COL_LIGHTGRAY ( 0xC0, 0xC0, 0xC0 );
456 constexpr ::Color COL_LIGHTBLUE ( 0x00, 0x00, 0xFF );
457 constexpr ::Color COL_LIGHTGREEN ( 0x00, 0xFF, 0x00 );
458 constexpr ::Color COL_LIGHTCYAN ( 0x00, 0xFF, 0xFF );
459 constexpr ::Color COL_LIGHTRED ( 0xFF, 0x00, 0x00 );
460 constexpr ::Color COL_LIGHTMAGENTA ( 0xFF, 0x00, 0xFF );
461 constexpr ::Color COL_LIGHTGRAYBLUE ( 0xE0, 0xE0, 0xFF );
462 constexpr ::Color COL_YELLOW ( 0xFF, 0xFF, 0x00 );
463 constexpr ::Color COL_WHITE ( 0xFF, 0xFF, 0xFF );
464 constexpr ::Color COL_TRANSPARENT ( ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF );
465 constexpr ::Color COL_AUTO ( ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF );
466 constexpr ::Color COL_AUTHOR1_DARK ( 198, 146, 0 );
467 constexpr ::Color COL_AUTHOR1_NORMAL ( 255, 255, 158 );
468 constexpr ::Color COL_AUTHOR1_LIGHT ( 255, 255, 195 );
469 constexpr ::Color COL_AUTHOR2_DARK ( 6, 70, 162 );
470 constexpr ::Color COL_AUTHOR2_NORMAL ( 216, 232, 255 );
471 constexpr ::Color COL_AUTHOR2_LIGHT ( 233, 242, 255 );
472 constexpr ::Color COL_AUTHOR3_DARK ( 87, 157, 28 );
473 constexpr ::Color COL_AUTHOR3_NORMAL ( 218, 248, 193 );
474 constexpr ::Color COL_AUTHOR3_LIGHT ( 226, 250, 207 );
475 constexpr ::Color COL_AUTHOR4_DARK ( 105, 43, 157 );
476 constexpr ::Color COL_AUTHOR4_NORMAL ( 228, 210, 245 );
477 constexpr ::Color COL_AUTHOR4_LIGHT ( 239, 228, 248 );
478 constexpr ::Color COL_AUTHOR5_DARK ( 197, 0, 11 );
479 constexpr ::Color COL_AUTHOR5_NORMAL ( 254, 205, 208 );
480 constexpr ::Color COL_AUTHOR5_LIGHT ( 255, 227, 229 );
481 constexpr ::Color COL_AUTHOR6_DARK ( 0, 128, 128 );
482 constexpr ::Color COL_AUTHOR6_NORMAL ( 210, 246, 246 );
483 constexpr ::Color COL_AUTHOR6_LIGHT ( 230, 250, 250 );
484 constexpr ::Color COL_AUTHOR7_DARK ( 140, 132, 0 );
485 constexpr ::Color COL_AUTHOR7_NORMAL ( 237, 252, 163 );
486 constexpr ::Color COL_AUTHOR7_LIGHT ( 242, 254, 181 );
487 constexpr ::Color COL_AUTHOR8_DARK ( 53, 85, 107 );
488 constexpr ::Color COL_AUTHOR8_NORMAL ( 211, 222, 232 );
489 constexpr ::Color COL_AUTHOR8_LIGHT ( 226, 234, 241 );
490 constexpr ::Color COL_AUTHOR9_DARK ( 209, 118, 0 );
491 constexpr ::Color COL_AUTHOR9_NORMAL ( 255, 226, 185 );
492 constexpr ::Color COL_AUTHOR9_LIGHT ( 255, 231, 199 );
493
494 template<typename charT, typename traits>
operator <<(std::basic_ostream<charT,traits> & rStream,const Color & rColor)495 inline std::basic_ostream<charT, traits>& operator <<(std::basic_ostream<charT, traits>& rStream, const Color& rColor)
496 {
497 std::ios_base::fmtflags nOrigFlags = rStream.flags();
498 rStream << "rgba[" << std::hex << std::setfill ('0')
499 << std::setw(2) << static_cast<int>(rColor.GetRed())
500 << std::setw(2) << static_cast<int>(rColor.GetGreen())
501 << std::setw(2) << static_cast<int>(rColor.GetBlue())
502 << std::setw(2) << static_cast<int>(rColor.GetAlpha()) << "]";
503 rStream.setf(nOrigFlags);
504 return rStream;
505 }
506
507 #endif
508
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
510