xref: /trunk/main/tools/inc/tools/color.hxx (revision b2b569f2)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _TOOLS_COLOR_HXX
24 #define _TOOLS_COLOR_HXX
25 
26 #include "tools/toolsdllapi.h"
27 
28 class SvStream;
29 class ResId;
30 #include <tools/solar.h>
31 
32 #ifndef _BGFX_COLOR_BCOLOR_HXX
33 #include <basegfx/color/bcolor.hxx>
34 #endif
35 
36 // --------------------
37 // - ColorCount-Types -
38 // --------------------
39 
40 #define COLCOUNT_MONOCHROM			((sal_uIntPtr)2)
41 #define COLCOUNT_16 				((sal_uIntPtr)16)
42 #define COLCOUNT_256				((sal_uIntPtr)256)
43 #define COLCOUNT_HICOLOR1			(((sal_uIntPtr)0x00007FFF)+1)
44 #define COLCOUNT_HICOLOR2			(((sal_uIntPtr)0x0000FFFF)+1)
45 #define COLCOUNT_TRUECOLOR			(((sal_uIntPtr)0x00FFFFFF)+1)
46 
47 // ---------------
48 // - Color-Types -
49 // ---------------
50 
51 typedef sal_uInt32 ColorData;
52 #define RGB_COLORDATA( r,g,b )		((ColorData)(((sal_uInt32)((sal_uInt8)(b))))|(((sal_uInt32)((sal_uInt8)(g)))<<8)|(((sal_uInt32)((sal_uInt8)(r)))<<16))
53 #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))
54 #define COLORDATA_RED( n )			((sal_uInt8)((n)>>16))
55 #define COLORDATA_GREEN( n )		((sal_uInt8)(((sal_uInt16)(n)) >> 8))
56 #define COLORDATA_BLUE( n ) 		((sal_uInt8)(n))
57 #define COLORDATA_TRANSPARENCY( n ) ((sal_uInt8)((n)>>24))
58 #define COLORDATA_RGB( n )			((ColorData)((n) & 0x00FFFFFF))
59 
60 #define COL_BLACK					RGB_COLORDATA( 0x00, 0x00, 0x00 )
61 #define COL_BLUE					RGB_COLORDATA( 0x00, 0x00, 0x80 )
62 #define COL_GREEN					RGB_COLORDATA( 0x00, 0x80, 0x00 )
63 #define COL_CYAN					RGB_COLORDATA( 0x00, 0x80, 0x80 )
64 #define COL_RED 					RGB_COLORDATA( 0x80, 0x00, 0x00 )
65 #define COL_MAGENTA 				RGB_COLORDATA( 0x80, 0x00, 0x80 )
66 #define COL_BROWN					RGB_COLORDATA( 0x80, 0x80, 0x00 )
67 #define COL_GRAY					RGB_COLORDATA( 0x80, 0x80, 0x80 )
68 #define COL_LIGHTGRAY				RGB_COLORDATA( 0xC0, 0xC0, 0xC0 )
69 #define COL_LIGHTBLUE				RGB_COLORDATA( 0x00, 0x00, 0xFF )
70 #define COL_LIGHTGREEN				RGB_COLORDATA( 0x00, 0xFF, 0x00 )
71 #define COL_LIGHTCYAN				RGB_COLORDATA( 0x00, 0xFF, 0xFF )
72 #define COL_LIGHTRED				RGB_COLORDATA( 0xFF, 0x00, 0x00 )
73 #define COL_LIGHTMAGENTA			RGB_COLORDATA( 0xFF, 0x00, 0xFF )
74 #define COL_YELLOW					RGB_COLORDATA( 0xFF, 0xFF, 0x00 )
75 #define COL_WHITE					RGB_COLORDATA( 0xFF, 0xFF, 0xFF )
76 #define COL_TRANSPARENT 			TRGB_COLORDATA( 0xFF, 0xFF, 0xFF, 0xFF )
77 #define COL_AUTO                    (ColorData)0xFFFFFFFF
78 #define COL_AUTHOR1_DARK			RGB_COLORDATA(198, 146, 0)
79 #define COL_AUTHOR1_NORMAL			RGB_COLORDATA(255, 255, 158)
80 #define COL_AUTHOR1_LIGHT			RGB_COLORDATA(255, 255, 195)
81 #define COL_AUTHOR2_DARK			RGB_COLORDATA(6,  70, 162)
82 #define COL_AUTHOR2_NORMAL			RGB_COLORDATA(216, 232, 255)
83 #define COL_AUTHOR2_LIGHT			RGB_COLORDATA(233, 242, 255)
84 #define COL_AUTHOR3_DARK			RGB_COLORDATA(87, 157,  28)
85 #define COL_AUTHOR3_NORMAL			RGB_COLORDATA(218, 248, 193)
86 #define COL_AUTHOR3_LIGHT			RGB_COLORDATA(226, 250, 207)
87 #define COL_AUTHOR4_DARK			RGB_COLORDATA(105,  43, 157)
88 #define COL_AUTHOR4_NORMAL			RGB_COLORDATA(228, 210, 245)
89 #define COL_AUTHOR4_LIGHT			RGB_COLORDATA(239, 228, 248)
90 #define COL_AUTHOR5_DARK			RGB_COLORDATA(197,   0,  11)
91 #define COL_AUTHOR5_NORMAL			RGB_COLORDATA(254, 205, 208)
92 #define COL_AUTHOR5_LIGHT			RGB_COLORDATA(255, 227, 229)
93 #define COL_AUTHOR6_DARK			RGB_COLORDATA(0, 128, 128)
94 #define COL_AUTHOR6_NORMAL			RGB_COLORDATA(210, 246, 246)
95 #define COL_AUTHOR6_LIGHT			RGB_COLORDATA(230, 250, 250)
96 #define COL_AUTHOR7_DARK			RGB_COLORDATA(140, 132,  0)
97 #define COL_AUTHOR7_NORMAL			RGB_COLORDATA(237, 252, 163)
98 #define COL_AUTHOR7_LIGHT			RGB_COLORDATA(242, 254, 181)
99 #define COL_AUTHOR8_DARK			RGB_COLORDATA(53,  85, 107)
100 #define COL_AUTHOR8_NORMAL			RGB_COLORDATA(211, 222, 232)
101 #define COL_AUTHOR8_LIGHT			RGB_COLORDATA(226, 234, 241)
102 #define COL_AUTHOR9_DARK			RGB_COLORDATA(209, 118,   0)
103 #define COL_AUTHOR9_NORMAL			RGB_COLORDATA(255, 226, 185)
104 #define COL_AUTHOR9_LIGHT			RGB_COLORDATA(255, 231, 199)
105 
106 #define COLOR_CHANNEL_MERGE( _def_cDst, _def_cSrc, _def_cSrcTrans ) \
107 	((sal_uInt8)((((long)(_def_cDst)-(_def_cSrc))*(_def_cSrcTrans)+(((_def_cSrc)<<8L)|(_def_cDst)))>>8L))
108 
109 // ---------
110 // - Color -
111 // ---------
112 
113 class TOOLS_DLLPUBLIC Color
114 {
115 protected:
116 	ColorData			mnColor;
117 
118 public:
Color()119 						Color() { mnColor = COL_BLACK; }
Color(ColorData nColor)120 						Color( ColorData nColor ) { mnColor = nColor; }
Color(sal_uInt8 nRed,sal_uInt8 nGreen,sal_uInt8 nBlue)121 						Color( sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue )
122 							{ mnColor = RGB_COLORDATA( nRed, nGreen, nBlue ); }
Color(sal_uInt8 nTransparency,sal_uInt8 nRed,sal_uInt8 nGreen,sal_uInt8 nBlue)123 						Color( sal_uInt8 nTransparency, sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue )
124 							{ mnColor = TRGB_COLORDATA( nTransparency, nRed, nGreen, nBlue ); }
125 						Color( const ResId& rResId );
126 						 // This ctor is defined in svtools, not tools!
127 
128 						// constructor to create a tools-Color from ::basegfx::BColor
Color(const::basegfx::BColor & rBColor)129 						explicit Color(const ::basegfx::BColor& rBColor)
130 						{
131 							mnColor = RGB_COLORDATA(
132 								sal_uInt8((rBColor.getRed() * 255.0) + 0.5),
133 								sal_uInt8((rBColor.getGreen() * 255.0) + 0.5),
134 								sal_uInt8((rBColor.getBlue() * 255.0) + 0.5));
135 						}
136 
137 	void				SetRed( sal_uInt8 nRed );
GetRed() const138 	sal_uInt8				GetRed() const { return COLORDATA_RED( mnColor ); }
139 	void				SetGreen( sal_uInt8 nGreen );
GetGreen() const140 	sal_uInt8				GetGreen() const { return COLORDATA_GREEN( mnColor ); }
141 	void				SetBlue( sal_uInt8 nBlue );
GetBlue() const142 	sal_uInt8				GetBlue() const { return COLORDATA_BLUE( mnColor ); }
143 	void				SetTransparency( sal_uInt8 nTransparency );
GetTransparency() const144 	sal_uInt8				GetTransparency() const { return COLORDATA_TRANSPARENCY( mnColor ); }
145 
SetColor(ColorData nColor)146 	void				SetColor( ColorData nColor ) { mnColor = nColor; }
GetColor() const147 	ColorData			GetColor() const { return mnColor; }
GetRGBColor() const148 	ColorData			GetRGBColor() const { return COLORDATA_RGB( mnColor ); }
149 
150 	sal_uInt8				GetColorError( const Color& rCompareColor ) const;
151 
152 	sal_uInt8				GetLuminance() const;
153 	void				IncreaseLuminance( sal_uInt8 cLumInc );
154 	void				DecreaseLuminance( sal_uInt8 cLumDec );
155 
156 	void				IncreaseContrast( sal_uInt8 cContInc );
157 	void				DecreaseContrast( sal_uInt8 cContDec );
158 
159 	void				Invert();
160 
161 	void				Merge( const Color& rMergeColor, sal_uInt8 cTransparency );
162 
163 	sal_Bool				IsRGBEqual( const Color& rColor ) const;
164 
165     // comparison with luminance thresholds
166     sal_Bool                IsDark()    const;
167     sal_Bool                IsBright()  const;
168 
169     // color space conversion tools
170     // the range for h/s/b is:
171     // Hue: 0-360 degree
172     // Saturation: 0-100 %
173     // Brightness: 0-100 %
174     static ColorData    HSBtoRGB( sal_uInt16 nHue, sal_uInt16 nSat, sal_uInt16 nBri );
175     void                RGBtoHSB( sal_uInt16& nHue, sal_uInt16& nSat, sal_uInt16& nBri ) const;
176 
177     // the range for cymk is 0 to 1.0
178     static ColorData    CMYKtoRGB( double fCyan, double fMagenta, double fYellow, double fKey );
179     void                RGBtoCMYK( double& fCyan, double& fMagenta, double& fYellow, double& fKey );
180 
operator ==(const Color & rColor) const181     sal_Bool            operator==( const Color& rColor ) const
182                         { return (mnColor == rColor.mnColor); }
operator !=(const Color & rColor) const183     sal_Bool            operator!=( const Color& rColor ) const
184                         { return !(Color::operator==( rColor )); }
185 
186 	SvStream&			Read( SvStream& rIStm, sal_Bool bNewFormat = sal_True );
187 	SvStream&			Write( SvStream& rOStm, sal_Bool bNewFormat = sal_True );
188 
189 	TOOLS_DLLPUBLIC friend SvStream&	operator>>( SvStream& rIStream, Color& rColor );
190 	TOOLS_DLLPUBLIC friend SvStream&	operator<<( SvStream& rOStream, const Color& rColor );
191 
192 	// get ::basegfx::BColor from this color
getBColor() const193 	::basegfx::BColor getBColor() const { return ::basegfx::BColor(GetRed() / 255.0, GetGreen() / 255.0, GetBlue() / 255.0); }
194 };
195 
SetRed(sal_uInt8 nRed)196 inline void Color::SetRed( sal_uInt8 nRed )
197 {
198 	mnColor &= 0xFF00FFFF;
199 	mnColor |= ((sal_uInt32)nRed)<<16;
200 }
201 
SetGreen(sal_uInt8 nGreen)202 inline void Color::SetGreen( sal_uInt8 nGreen )
203 {
204 	mnColor &= 0xFFFF00FF;
205 	mnColor |= ((sal_uInt16)nGreen)<<8;
206 }
207 
SetBlue(sal_uInt8 nBlue)208 inline void Color::SetBlue( sal_uInt8 nBlue )
209 {
210 	mnColor &= 0xFFFFFF00;
211 	mnColor |= nBlue;
212 }
213 
SetTransparency(sal_uInt8 nTransparency)214 inline void Color::SetTransparency( sal_uInt8 nTransparency )
215 {
216 	mnColor &= 0x00FFFFFF;
217 	mnColor |= ((sal_uInt32)nTransparency)<<24;
218 }
219 
IsRGBEqual(const Color & rColor) const220 inline sal_Bool Color::IsRGBEqual( const Color& rColor ) const
221 {
222 	return (COLORDATA_RGB( mnColor ) == COLORDATA_RGB( rColor.mnColor ));
223 }
224 
GetLuminance() const225 inline sal_uInt8 Color::GetLuminance() const
226 {
227 	return( (sal_uInt8) ( ( COLORDATA_BLUE( mnColor ) * 28UL +
228 						COLORDATA_GREEN( mnColor ) * 151UL +
229 						COLORDATA_RED( mnColor ) * 77UL ) >> 8UL ) );
230 }
231 
Merge(const Color & rMergeColor,sal_uInt8 cTransparency)232 inline void Color::Merge( const Color& rMergeColor, sal_uInt8 cTransparency )
233 {
234 	SetRed( COLOR_CHANNEL_MERGE( COLORDATA_RED( mnColor ), COLORDATA_RED( rMergeColor.mnColor ), cTransparency ) );
235 	SetGreen( COLOR_CHANNEL_MERGE( COLORDATA_GREEN( mnColor ), COLORDATA_GREEN( rMergeColor.mnColor ), cTransparency ) );
236 	SetBlue( COLOR_CHANNEL_MERGE( COLORDATA_BLUE( mnColor ), COLORDATA_BLUE( rMergeColor.mnColor ), cTransparency ) );
237 }
238 
239 #endif // _TOOLS_COLOR_HXX
240