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 <basegfx/color/bcolor.hxx>
30 constexpr sal_uInt32
extractRGB(sal_uInt32 nColorNumber
)
32 return nColorNumber
& 0x00FFFFFF;
39 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Color
41 // data intentionally public; read the commit log!
67 constexpr Color(sal_uInt32 nColor
)
71 constexpr Color(sal_uInt8 nTransparency
, sal_uInt8 nRed
, sal_uInt8 nGreen
, sal_uInt8 nBlue
)
72 : mValue(sal_uInt32(nBlue
) | (sal_uInt32(nGreen
) << 8) | (sal_uInt32(nRed
) << 16) | (sal_uInt32(nTransparency
) << 24))
75 constexpr Color(sal_uInt8 nRed
, sal_uInt8 nGreen
, sal_uInt8 nBlue
)
76 : Color(0, nRed
, nGreen
, nBlue
)
79 // constructor to create a tools-Color from ::basegfx::BColor
80 explicit Color(const basegfx::BColor
& rBColor
)
82 sal_uInt8(std::lround(rBColor
.getRed() * 255.0)),
83 sal_uInt8(std::lround(rBColor
.getGreen() * 255.0)),
84 sal_uInt8(std::lround(rBColor
.getBlue() * 255.0)))
87 /** Primarily used when passing Color objects to UNO API */
88 constexpr explicit operator sal_uInt32() const
93 constexpr explicit operator sal_Int32() const
95 return sal_Int32(mValue
);
98 bool operator<(const Color
& aCompareColor
) const
100 return mValue
< aCompareColor
.mValue
;
103 void SetRed(sal_uInt8 nRed
);
104 sal_uInt8
GetRed() const
108 void SetGreen(sal_uInt8 nGreen
);
109 sal_uInt8
GetGreen() const
113 void SetBlue(sal_uInt8 nBlue
);
114 sal_uInt8
GetBlue() const
118 void SetTransparency(sal_uInt8 nTransparency
);
119 sal_uInt8
GetTransparency() const
124 Color
GetRGBColor() const
126 return color::extractRGB(mValue
);
129 sal_uInt16
GetColorError(const Color
& rCompareColor
) const;
131 sal_uInt8
GetLuminance() const;
132 void IncreaseLuminance(sal_uInt8 cLumInc
);
133 void DecreaseLuminance(sal_uInt8 cLumDec
);
135 void DecreaseContrast(sal_uInt8 cContDec
);
138 * Apply tint or shade to a color.
140 * The input value is the percentage (in 100th of percent) of how much the
141 * color changes towards the black (shade) or white (tint). If the value
142 * is positive, the color is tinted, if the value is negative, the color is
145 void ApplyTintOrShade(sal_Int16 n100thPercent
);
149 void Merge(const Color
& rMergeColor
, sal_uInt8 cTransparency
);
151 bool IsRGBEqual(const Color
& rColor
) const;
153 // comparison with luminance thresholds
155 bool IsBright() const;
157 // color space conversion tools
158 // the range for h/s/b is:
160 // Saturation: 0-100%
161 // Brightness: 0-100%
162 static Color
HSBtoRGB(sal_uInt16 nHue
, sal_uInt16 nSaturation
, sal_uInt16 nBrightness
);
163 void RGBtoHSB(sal_uInt16
& nHue
, sal_uInt16
& nSaturation
, sal_uInt16
& nBrightness
) const;
165 bool operator==(const Color
& rColor
) const
167 return mValue
== rColor
.mValue
;
169 bool operator!=(const Color
& rColor
) const
171 return !(Color::operator==(rColor
));
174 // Return color as RGB hex string
175 // for example "00ff00" for green color
176 OUString
AsRGBHexString() const;
178 // get ::basegfx::BColor from this color
179 basegfx::BColor
getBColor() const
181 return basegfx::BColor(GetRed() / 255.0, GetGreen() / 255.0, GetBlue() / 255.0);
185 inline void Color::SetRed( sal_uInt8 nRed
)
190 inline void Color::SetGreen( sal_uInt8 nGreen
)
195 inline void Color::SetBlue( sal_uInt8 nBlue
)
200 inline void Color::SetTransparency( sal_uInt8 nTransparency
)
205 inline bool Color::IsRGBEqual( const Color
& rColor
) const
207 return color::extractRGB(mValue
) == color::extractRGB(rColor
.mValue
);
210 inline sal_uInt8
Color::GetLuminance() const
212 return sal_uInt8((B
* 29UL + G
* 151UL + R
* 76UL) >> 8);
215 constexpr sal_uInt8
ColorChannelMerge(sal_uInt8 nDst
, sal_uInt8 nSrc
, sal_uInt8 nSrcTrans
)
217 return sal_uInt8(((sal_Int32(nDst
) - nSrc
) * nSrcTrans
+ ((nSrc
<< 8) | nDst
)) >> 8);
220 inline void Color::Invert()
227 inline sal_uInt16
Color::GetColorError( const Color
& rColor
) const
229 return static_cast<sal_uInt16
>(
230 abs(static_cast<int>(GetBlue()) - rColor
.GetBlue()) +
231 abs(static_cast<int>(GetGreen()) - rColor
.GetGreen()) +
232 abs(static_cast<int>(GetRed()) - rColor
.GetRed()));
235 inline void Color::Merge( const Color
& rMergeColor
, sal_uInt8 cTransparency
)
237 R
= ColorChannelMerge(R
, rMergeColor
.R
, cTransparency
);
238 G
= ColorChannelMerge(G
, rMergeColor
.G
, cTransparency
);
239 B
= ColorChannelMerge(B
, rMergeColor
.B
, cTransparency
);
242 // to reduce the noise when moving these into and out of Any
243 inline bool operator >>=( const css::uno::Any
& rAny
, Color
& value
)
246 if (!(rAny
>>= nTmp
))
252 inline void operator <<=( css::uno::Any
& rAny
, Color value
)
254 rAny
<<= sal_Int32(value
);
256 namespace com
{ namespace sun
{ namespace star
{ namespace uno
{
258 inline Any
makeAny( Color
const & value
)
260 return Any(sal_Int32(value
));
264 // Test compile time conversion of Color to sal_uInt32
266 static_assert (sal_uInt32(Color(0x00, 0x12, 0x34, 0x56)) == 0x00123456);
267 static_assert (sal_uInt32(Color(0x12, 0x34, 0x56)) == 0x00123456);
271 constexpr ::Color
COL_BLACK ( 0x00, 0x00, 0x00 );
272 constexpr ::Color
COL_BLUE ( 0x00, 0x00, 0x80 );
273 constexpr ::Color
COL_GREEN ( 0x00, 0x80, 0x00 );
274 constexpr ::Color
COL_CYAN ( 0x00, 0x80, 0x80 );
275 constexpr ::Color
COL_RED ( 0x80, 0x00, 0x00 );
276 constexpr ::Color
COL_MAGENTA ( 0x80, 0x00, 0x80 );
277 constexpr ::Color
COL_BROWN ( 0x80, 0x80, 0x00 );
278 constexpr ::Color
COL_GRAY ( 0x80, 0x80, 0x80 );
279 constexpr ::Color
COL_GRAY3 ( 0xCC, 0xCC, 0xCC );
280 constexpr ::Color
COL_GRAY7 ( 0x66, 0x66, 0x66 );
281 constexpr ::Color
COL_LIGHTGRAY ( 0xC0, 0xC0, 0xC0 );
282 constexpr ::Color
COL_LIGHTBLUE ( 0x00, 0x00, 0xFF );
283 constexpr ::Color
COL_LIGHTGREEN ( 0x00, 0xFF, 0x00 );
284 constexpr ::Color
COL_LIGHTCYAN ( 0x00, 0xFF, 0xFF );
285 constexpr ::Color
COL_LIGHTRED ( 0xFF, 0x00, 0x00 );
286 constexpr ::Color
COL_LIGHTMAGENTA ( 0xFF, 0x00, 0xFF );
287 constexpr ::Color
COL_LIGHTGRAYBLUE ( 0xE0, 0xE0, 0xFF );
288 constexpr ::Color
COL_YELLOW ( 0xFF, 0xFF, 0x00 );
289 constexpr ::Color
COL_WHITE ( 0xFF, 0xFF, 0xFF );
290 constexpr ::Color
COL_TRANSPARENT ( 0xFF, 0xFF, 0xFF, 0xFF );
291 constexpr ::Color
COL_AUTO ( 0xFF, 0xFF, 0xFF, 0xFF );
292 constexpr ::Color
COL_AUTHOR1_DARK ( 198, 146, 0 );
293 constexpr ::Color
COL_AUTHOR1_NORMAL ( 255, 255, 158 );
294 constexpr ::Color
COL_AUTHOR1_LIGHT ( 255, 255, 195 );
295 constexpr ::Color
COL_AUTHOR2_DARK ( 6, 70, 162 );
296 constexpr ::Color
COL_AUTHOR2_NORMAL ( 216, 232, 255 );
297 constexpr ::Color
COL_AUTHOR2_LIGHT ( 233, 242, 255 );
298 constexpr ::Color
COL_AUTHOR3_DARK ( 87, 157, 28 );
299 constexpr ::Color
COL_AUTHOR3_NORMAL ( 218, 248, 193 );
300 constexpr ::Color
COL_AUTHOR3_LIGHT ( 226, 250, 207 );
301 constexpr ::Color
COL_AUTHOR4_DARK ( 105, 43, 157 );
302 constexpr ::Color
COL_AUTHOR4_NORMAL ( 228, 210, 245 );
303 constexpr ::Color
COL_AUTHOR4_LIGHT ( 239, 228, 248 );
304 constexpr ::Color
COL_AUTHOR5_DARK ( 197, 0, 11 );
305 constexpr ::Color
COL_AUTHOR5_NORMAL ( 254, 205, 208 );
306 constexpr ::Color
COL_AUTHOR5_LIGHT ( 255, 227, 229 );
307 constexpr ::Color
COL_AUTHOR6_DARK ( 0, 128, 128 );
308 constexpr ::Color
COL_AUTHOR6_NORMAL ( 210, 246, 246 );
309 constexpr ::Color
COL_AUTHOR6_LIGHT ( 230, 250, 250 );
310 constexpr ::Color
COL_AUTHOR7_DARK ( 140, 132, 0 );
311 constexpr ::Color
COL_AUTHOR7_NORMAL ( 237, 252, 163 );
312 constexpr ::Color
COL_AUTHOR7_LIGHT ( 242, 254, 181 );
313 constexpr ::Color
COL_AUTHOR8_DARK ( 53, 85, 107 );
314 constexpr ::Color
COL_AUTHOR8_NORMAL ( 211, 222, 232 );
315 constexpr ::Color
COL_AUTHOR8_LIGHT ( 226, 234, 241 );
316 constexpr ::Color
COL_AUTHOR9_DARK ( 209, 118, 0 );
317 constexpr ::Color
COL_AUTHOR9_NORMAL ( 255, 226, 185 );
318 constexpr ::Color
COL_AUTHOR9_LIGHT ( 255, 231, 199 );
320 template<typename charT
, typename traits
>
321 inline std::basic_ostream
<charT
, traits
>& operator <<(std::basic_ostream
<charT
, traits
>& rStream
, const Color
& rColor
)
323 return rStream
<< "c[" << std::hex
<< std::setfill ('0')
324 << std::setw(2) << static_cast<int>(rColor
.GetRed())
325 << std::setw(2) << static_cast<int>(rColor
.GetGreen())
326 << std::setw(2) << static_cast<int>(rColor
.GetBlue())
327 << std::setw(2) << static_cast<int>(rColor
.GetTransparency()) << "]";
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */