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