Line data Source code
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 <tools/toolsdllapi.h>
23 :
24 : class SvStream;
25 : class ResId;
26 :
27 : #include <basegfx/color/bcolor.hxx>
28 :
29 : // Color types
30 : typedef sal_uInt32 ColorData;
31 : #define RGB_COLORDATA( r,g,b ) ((ColorData)(((sal_uInt32)((sal_uInt8)(b))))|(((sal_uInt32)((sal_uInt8)(g)))<<8)|(((sal_uInt32)((sal_uInt8)(r)))<<16))
32 : #define TRGB_COLORDATA( t,r,g,b ) ((ColorData)(((sal_uInt32)((sal_uInt8)(b))))|(((sal_uInt32)((sal_uInt8)(g)))<<8)|(((sal_uInt32)((sal_uInt8)(r)))<<16)|(((sal_uInt32)((sal_uInt8)(t)))<<24))
33 : #define COLORDATA_RED( n ) ((sal_uInt8)((n)>>16))
34 : #define COLORDATA_GREEN( n ) ((sal_uInt8)(((sal_uInt16)(n)) >> 8))
35 : #define COLORDATA_BLUE( n ) ((sal_uInt8)(n))
36 : #define COLORDATA_TRANSPARENCY( n ) ((sal_uInt8)((n)>>24))
37 : #define COLORDATA_RGB( n ) ((ColorData)((n) & 0x00FFFFFF))
38 :
39 : #define COL_BLACK RGB_COLORDATA( 0x00, 0x00, 0x00 )
40 : #define COL_BLUE RGB_COLORDATA( 0x00, 0x00, 0x80 )
41 : #define COL_GREEN RGB_COLORDATA( 0x00, 0x80, 0x00 )
42 : #define COL_CYAN RGB_COLORDATA( 0x00, 0x80, 0x80 )
43 : #define COL_RED RGB_COLORDATA( 0x80, 0x00, 0x00 )
44 : #define COL_MAGENTA RGB_COLORDATA( 0x80, 0x00, 0x80 )
45 : #define COL_BROWN RGB_COLORDATA( 0x80, 0x80, 0x00 )
46 : #define COL_GRAY RGB_COLORDATA( 0x80, 0x80, 0x80 )
47 : #define COL_LIGHTGRAY RGB_COLORDATA( 0xC0, 0xC0, 0xC0 )
48 : #define COL_LIGHTBLUE RGB_COLORDATA( 0x00, 0x00, 0xFF )
49 : #define COL_LIGHTGREEN RGB_COLORDATA( 0x00, 0xFF, 0x00 )
50 : #define COL_LIGHTCYAN RGB_COLORDATA( 0x00, 0xFF, 0xFF )
51 : #define COL_LIGHTRED RGB_COLORDATA( 0xFF, 0x00, 0x00 )
52 : #define COL_LIGHTMAGENTA RGB_COLORDATA( 0xFF, 0x00, 0xFF )
53 : #define COL_LIGHTGRAYBLUE RGB_COLORDATA( 0xE0, 0xE0, 0xFF )
54 : #define COL_YELLOW RGB_COLORDATA( 0xFF, 0xFF, 0x00 )
55 : #define COL_WHITE RGB_COLORDATA( 0xFF, 0xFF, 0xFF )
56 : #define COL_TRANSPARENT TRGB_COLORDATA( 0xFF, 0xFF, 0xFF, 0xFF )
57 : #define COL_AUTO (ColorData)0xFFFFFFFF
58 : #define COL_AUTHOR1_DARK RGB_COLORDATA(198, 146, 0)
59 : #define COL_AUTHOR1_NORMAL RGB_COLORDATA(255, 255, 158)
60 : #define COL_AUTHOR1_LIGHT RGB_COLORDATA(255, 255, 195)
61 : #define COL_AUTHOR2_DARK RGB_COLORDATA(6, 70, 162)
62 : #define COL_AUTHOR2_NORMAL RGB_COLORDATA(216, 232, 255)
63 : #define COL_AUTHOR2_LIGHT RGB_COLORDATA(233, 242, 255)
64 : #define COL_AUTHOR3_DARK RGB_COLORDATA(87, 157, 28)
65 : #define COL_AUTHOR3_NORMAL RGB_COLORDATA(218, 248, 193)
66 : #define COL_AUTHOR3_LIGHT RGB_COLORDATA(226, 250, 207)
67 : #define COL_AUTHOR4_DARK RGB_COLORDATA(105, 43, 157)
68 : #define COL_AUTHOR4_NORMAL RGB_COLORDATA(228, 210, 245)
69 : #define COL_AUTHOR4_LIGHT RGB_COLORDATA(239, 228, 248)
70 : #define COL_AUTHOR5_DARK RGB_COLORDATA(197, 0, 11)
71 : #define COL_AUTHOR5_NORMAL RGB_COLORDATA(254, 205, 208)
72 : #define COL_AUTHOR5_LIGHT RGB_COLORDATA(255, 227, 229)
73 : #define COL_AUTHOR6_DARK RGB_COLORDATA(0, 128, 128)
74 : #define COL_AUTHOR6_NORMAL RGB_COLORDATA(210, 246, 246)
75 : #define COL_AUTHOR6_LIGHT RGB_COLORDATA(230, 250, 250)
76 : #define COL_AUTHOR7_DARK RGB_COLORDATA(140, 132, 0)
77 : #define COL_AUTHOR7_NORMAL RGB_COLORDATA(237, 252, 163)
78 : #define COL_AUTHOR7_LIGHT RGB_COLORDATA(242, 254, 181)
79 : #define COL_AUTHOR8_DARK RGB_COLORDATA(53, 85, 107)
80 : #define COL_AUTHOR8_NORMAL RGB_COLORDATA(211, 222, 232)
81 : #define COL_AUTHOR8_LIGHT RGB_COLORDATA(226, 234, 241)
82 : #define COL_AUTHOR9_DARK RGB_COLORDATA(209, 118, 0)
83 : #define COL_AUTHOR9_NORMAL RGB_COLORDATA(255, 226, 185)
84 : #define COL_AUTHOR9_LIGHT RGB_COLORDATA(255, 231, 199)
85 :
86 : #define COLOR_CHANNEL_MERGE( _def_cDst, _def_cSrc, _def_cSrcTrans ) \
87 : ((sal_uInt8)((((long)(_def_cDst)-(_def_cSrc))*(_def_cSrcTrans)+(((_def_cSrc)<<8L)|(_def_cDst)))>>8L))
88 :
89 : // Color
90 :
91 : class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Color
92 : {
93 : protected:
94 : ColorData mnColor;
95 :
96 : public:
97 227787961 : Color()
98 227787961 : : mnColor(COL_BLACK)
99 227787961 : {}
100 10760051 : Color(ColorData nColor)
101 10760051 : : mnColor(nColor)
102 10760051 : {}
103 2045516 : Color(sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
104 2045516 : : mnColor(RGB_COLORDATA(nRed, nGreen, nBlue))
105 2045516 : {}
106 3641 : Color(sal_uInt8 nTransparency, sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
107 3641 : : mnColor(TRGB_COLORDATA(nTransparency, nRed, nGreen, nBlue))
108 3641 : {}
109 : // This ctor is defined in svtools, not tools!
110 : Color(const ResId& rResId);
111 :
112 : // constructor to create a tools-Color from ::basegfx::BColor
113 781849 : explicit Color(const basegfx::BColor& rBColor)
114 781849 : : mnColor(RGB_COLORDATA(sal_uInt8((rBColor.getRed() * 255.0) + 0.5),
115 : sal_uInt8((rBColor.getGreen() * 255.0) + 0.5),
116 781849 : sal_uInt8((rBColor.getBlue() * 255.0) + 0.5)))
117 781849 : {}
118 :
119 0 : bool operator<(const Color& b) const
120 : {
121 0 : return mnColor < b.GetColor();
122 : }
123 :
124 : void SetRed(sal_uInt8 nRed);
125 4656357 : sal_uInt8 GetRed() const
126 : {
127 4656357 : return COLORDATA_RED(mnColor);
128 : }
129 : void SetGreen(sal_uInt8 nGreen);
130 4656190 : sal_uInt8 GetGreen() const
131 : {
132 4656190 : return COLORDATA_GREEN(mnColor);
133 : }
134 : void SetBlue(sal_uInt8 nBlue);
135 4656157 : sal_uInt8 GetBlue() const
136 : {
137 4656157 : return COLORDATA_BLUE(mnColor);
138 : }
139 : void SetTransparency(sal_uInt8 nTransparency);
140 5539302 : sal_uInt8 GetTransparency() const
141 : {
142 5539302 : return COLORDATA_TRANSPARENCY(mnColor);
143 : }
144 :
145 83214 : void SetColor(ColorData nColor)
146 : {
147 83214 : mnColor = nColor;
148 83214 : }
149 431168 : ColorData GetColor() const
150 : {
151 431168 : return mnColor;
152 : }
153 8091 : ColorData GetRGBColor() const
154 : {
155 8091 : return COLORDATA_RGB(mnColor);
156 : }
157 :
158 : sal_uInt8 GetColorError(const Color& rCompareColor) const;
159 :
160 : sal_uInt8 GetLuminance() const;
161 : void IncreaseLuminance(sal_uInt8 cLumInc);
162 : void DecreaseLuminance(sal_uInt8 cLumDec);
163 :
164 : void DecreaseContrast(sal_uInt8 cContDec);
165 :
166 : void Invert();
167 :
168 : void Merge(const Color& rMergeColor, sal_uInt8 cTransparency);
169 :
170 : bool IsRGBEqual(const Color& rColor) const;
171 :
172 : // comparison with luminance thresholds
173 : bool IsDark() const;
174 : bool IsBright() const;
175 :
176 : // color space conversion tools
177 : // the range for h/s/b is:
178 : // Hue: 0-360 degree
179 : // Saturation: 0-100%
180 : // Brightness: 0-100%
181 : static ColorData HSBtoRGB(sal_uInt16 nHue, sal_uInt16 nSaturation, sal_uInt16 nBrightness);
182 : void RGBtoHSB(sal_uInt16& nHue, sal_uInt16& nSaturation, sal_uInt16& nBrightness) const;
183 :
184 21549801 : bool operator==(const Color& rColor) const
185 : {
186 21549801 : return mnColor == rColor.mnColor;
187 : }
188 15416859 : bool operator!=(const Color& rColor) const
189 : {
190 15416859 : return !(Color::operator==(rColor));
191 : }
192 :
193 : SvStream& Read(SvStream& rIStream, bool bNewFormat = true);
194 : SvStream& Write(SvStream& rOStream, bool bNewFormat = true);
195 :
196 : TOOLS_DLLPUBLIC friend SvStream& ReadColor(SvStream& rIStream, Color& rColor);
197 : TOOLS_DLLPUBLIC friend SvStream& WriteColor(SvStream& rOStream, const Color& rColor);
198 :
199 : // Return color as RGB hex string
200 : // for example "00ff00" for green color
201 : OUString AsRGBHexString() const;
202 :
203 : // get ::basegfx::BColor from this color
204 205291 : basegfx::BColor getBColor() const
205 : {
206 205291 : return basegfx::BColor(GetRed() / 255.0, GetGreen() / 255.0, GetBlue() / 255.0);
207 : }
208 : };
209 :
210 53951 : inline void Color::SetRed( sal_uInt8 nRed )
211 : {
212 53951 : mnColor &= 0xFF00FFFF;
213 53951 : mnColor |= ((sal_uInt32)nRed)<<16;
214 53951 : }
215 :
216 53951 : inline void Color::SetGreen( sal_uInt8 nGreen )
217 : {
218 53951 : mnColor &= 0xFFFF00FF;
219 53951 : mnColor |= ((sal_uInt16)nGreen)<<8;
220 53951 : }
221 :
222 53951 : inline void Color::SetBlue( sal_uInt8 nBlue )
223 : {
224 53951 : mnColor &= 0xFFFFFF00;
225 53951 : mnColor |= nBlue;
226 53951 : }
227 :
228 96985 : inline void Color::SetTransparency( sal_uInt8 nTransparency )
229 : {
230 96985 : mnColor &= 0x00FFFFFF;
231 96985 : mnColor |= ((sal_uInt32)nTransparency)<<24;
232 96985 : }
233 :
234 984 : inline bool Color::IsRGBEqual( const Color& rColor ) const
235 : {
236 984 : return COLORDATA_RGB( mnColor ) == COLORDATA_RGB(rColor.mnColor);
237 : }
238 :
239 273427 : inline sal_uInt8 Color::GetLuminance() const
240 : {
241 546854 : return static_cast<sal_uInt8>((COLORDATA_BLUE(mnColor) * 29UL +
242 546854 : COLORDATA_GREEN(mnColor) * 151UL +
243 546854 : COLORDATA_RED(mnColor) * 76UL) >> 8UL);
244 : }
245 :
246 26875 : inline void Color::Merge( const Color& rMergeColor, sal_uInt8 cTransparency )
247 : {
248 26875 : SetRed(COLOR_CHANNEL_MERGE(COLORDATA_RED(mnColor), COLORDATA_RED(rMergeColor.mnColor), cTransparency));
249 26875 : SetGreen(COLOR_CHANNEL_MERGE(COLORDATA_GREEN(mnColor), COLORDATA_GREEN(rMergeColor.mnColor), cTransparency));
250 26875 : SetBlue(COLOR_CHANNEL_MERGE(COLORDATA_BLUE(mnColor), COLORDATA_BLUE(rMergeColor.mnColor), cTransparency));
251 26875 : }
252 :
253 : #endif
254 :
255 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|