PCL
Color.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/Color.h - Released 2024-06-18T15:48:54Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_Color_h
53 #define __PCL_Color_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/Math.h>
60 #include <pcl/String.h>
61 
62 namespace pcl
63 {
64 
65 // ----------------------------------------------------------------------------
66 
92 using RGBA = uint32;
93 
99 inline uint8 Alpha( RGBA rgba )
100 {
101  return uint8( rgba >> 24 );
102 }
103 
109 inline uint8 Red( RGBA rgba )
110 {
111  return uint8( rgba >> 16 );
112 }
113 
119 inline uint8 Green( RGBA rgba )
120 {
121  return uint8( rgba >> 8 );
122 }
123 
129 inline uint8 Blue( RGBA rgba )
130 {
131  return uint8( rgba );
132 }
133 
140 inline void ClearAlpha( RGBA& rgba )
141 {
142  rgba &= 0x00fffffful;
143 }
144 
150 inline void ClearRed( RGBA& rgba )
151 {
152  rgba &= 0xff00fffful;
153 }
154 
160 inline void ClearGreen( RGBA& rgba )
161 {
162  rgba &= 0xffff00fful;
163 }
164 
170 inline void ClearBlue( RGBA& rgba )
171 {
172  rgba &= 0xffffff00ul;
173 }
174 
180 inline void SetAlpha( RGBA& rgba, uint8 a )
181 {
182  ClearAlpha( rgba ); rgba |= uint32( a ) << 24;
183 }
184 
190 inline void SetRed( RGBA& rgba, uint8 r )
191 {
192  ClearRed( rgba ); rgba |= uint32( r ) << 16;
193 }
194 
200 inline void SetGreen( RGBA& rgba, uint8 g )
201 {
202  ClearGreen( rgba ); rgba |= uint32( g ) << 8;
203 }
204 
210 inline void SetBlue( RGBA& rgba, uint8 b )
211 {
212  ClearBlue( rgba ); rgba |= uint32( b );
213 }
214 
228 inline RGBA RGBAColor( int r, int g, int b, int a )
229 {
230  return (uint32( a ) << 24) | (uint32( r ) << 16) | (uint32( g ) << 8) | uint32( b );
231 }
232 
246 inline RGBA RGBAColor( int r, int g, int b )
247 {
248  return 0xff000000ul | (uint32( r ) << 16) | (uint32( g ) << 8) | uint32( b );
249 }
250 
264 inline RGBA RGBAColor( double r, double g, double b, double a )
265 {
266  return RGBAColor( RoundInt( 255*r ), RoundInt( 255*g ), RoundInt( 255*b ), RoundInt( 255*a ) );
267 }
268 
282 inline RGBA RGBAColor( float r, float g, float b, float a )
283 {
284  return RGBAColor( double( r ), double( g ), double( b ), double( a ) );
285 }
286 
300 inline RGBA RGBAColor( double r, double g, double b )
301 {
302  return RGBAColor( RoundInt( 255*r ), RoundInt( 255*g ), RoundInt( 255*b ) );
303 }
304 
318 inline RGBA RGBAColor( float r, float g, float b )
319 {
320  return RGBAColor( double( r ), double( g ), double( b ) );
321 }
322 
343 RGBA PCL_FUNC RGBAColor( const IsoString& colorNameOrHex );
344 
345 inline RGBA RGBAColor( const IsoString::ustring_base& colorNameOrHex )
346 {
347  return RGBAColor( IsoString( colorNameOrHex ) );
348 }
349 
355 inline RGBA StringToRGBAColor( const IsoString& colorNameOrHex )
356 {
357  return RGBAColor( colorNameOrHex );
358 }
359 
360 inline RGBA StringToRGBAColor( const IsoString::ustring_base& colorNameOrHex )
361 {
362  return StringToRGBAColor( IsoString( colorNameOrHex ) );
363 }
364 
374 {
375  IsoString s;
376  s.Format( "#%02X%02X%02X", Red( c ), Green( c ), Blue( c ) );
377  return s;
378 }
379 
388 {
389  IsoString s;
390  s.Format( "#%02X%02X%02X%02X", Red( c ), Green( c ), Blue( c ), Alpha( c ) );
391  return s;
392 }
393 
404 
405 // ----------------------------------------------------------------------------
406 
407 } // pcl
408 
409 #endif // __PCL_Color_h
410 
411 // ----------------------------------------------------------------------------
412 // EOF pcl/Color.h - Released 2024-06-18T15:48:54Z
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5425
GenericString< char16_type, CharTraits, PCL_STRING_ALLOCATOR > ustring_base
Definition: String.h:5488
IsoString & Format(const_c_string fmt,...)
Definition: String.h:6570
int RoundInt(T x) noexcept
Definition: Math.h:1503
unsigned char uint8
Definition: Defs.h:642
unsigned int uint32
Definition: Defs.h:666
uint8 Red(RGBA rgba)
Definition: Color.h:109
IsoString RGBColorToHexString(RGBA c)
Definition: Color.h:373
uint8 Alpha(RGBA rgba)
Definition: Color.h:99
void SetBlue(RGBA &rgba, uint8 b)
Definition: Color.h:210
IsoString PCL_FUNC CSSColorName(RGBA)
void SetAlpha(RGBA &rgba, uint8 a)
Definition: Color.h:180
void SetGreen(RGBA &rgba, uint8 g)
Definition: Color.h:200
IsoString RGBAColorToHexString(RGBA c)
Definition: Color.h:387
void SetRed(RGBA &rgba, uint8 r)
Definition: Color.h:190
uint32 RGBA
Definition: Color.h:92
void ClearBlue(RGBA &rgba)
Definition: Color.h:170
RGBA RGBAColor(int r, int g, int b, int a)
Definition: Color.h:228
void ClearGreen(RGBA &rgba)
Definition: Color.h:160
void ClearAlpha(RGBA &rgba)
Definition: Color.h:140
void ClearRed(RGBA &rgba)
Definition: Color.h:150
uint8 Blue(RGBA rgba)
Definition: Color.h:129
RGBA StringToRGBAColor(const IsoString &colorNameOrHex)
Definition: Color.h:355
uint8 Green(RGBA rgba)
Definition: Color.h:119
PCL root namespace.
Definition: AbstractImage.h:77