NCBI C++ ToolKit
rgba_color.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_UTILS___RGBA_COLOR__HPP
2 #define GUI_UTILS___RGBA_COLOR__HPP
3 
4 /* $Id: rgba_color.hpp 43891 2019-09-16 13:50:00Z evgeniev $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Robert Smith, Mike DiCuccio
30  *
31  * File Description:
32  * CRgbaColor - colors in the RGB colorspace, including transparency
33  */
34 
35 
36 #include <corelib/ncbistd.hpp>
37 #include <gui/gui.hpp>
38 #include <gui/utils/vect4.hpp>
39 #include <gui/utils/vect3.hpp>
40 
41 
42 /** @addtogroup GUI_UTILS
43  *
44  * @{
45  */
46 
47 
49 
50 
51 ///
52 /// class CRgbaColor provides a simple abstraction for managing colors.
53 ///
54 /// Keep the class concrete to make it efficient for use in arrays/buffers
55 /// (do not derive from this class or add virtual functions.)
56 ///
58 {
59 public:
60 
61  /// default ctor
62  CRgbaColor();
63 
64  /// construct around a given float array
65  CRgbaColor(const float* color, size_t size);
66 
67  /// construct an RGBA color around an Fl_Color
68  //CRgbaColor(Fl_Color c);
69 
70  /// construct with explicit (floating point) RGB values
71  CRgbaColor(float r, float g, float b);
72 
73  /// construct with explicit (floating point) RGB+alpha values
74  CRgbaColor(float r, float g, float b, float a);
75 
76  // construct using rgb from another color object and a new alpha
77  CRgbaColor(const CRgbaColor& rgb, float alpha);
78 
79  /// construct with explicit (unsigned char) RGB values. These are
80  /// normalized to 0-1 internally.
81  CRgbaColor(unsigned char r, unsigned char g, unsigned char b);
82 
83  /// construct with explicit (unsigned char) RGB+alpha values. These are
84  /// normalized from 0-1 internally.
85  CRgbaColor(unsigned char r, unsigned char g,
86  unsigned char b, unsigned char a);
87 
88  /// construct with explicit (int) RGB values. These are
89  /// normalized to 0-1 internally.
90  /// The values should be in the rane of 0-255.
91  CRgbaColor(int r, int g, int b);
92 
93  /// construct with explicit (int) RGB+alpha values. These are
94  /// normalized from 0-1 internally.
95  /// The values should be in the rane of 0-255.
96  CRgbaColor(int r, int g, int b, int a);
97 
98  /// construct from a string encoded in the form "r g b"
99  explicit CRgbaColor(const string& s);
100 
101  /// set the color from an Fl_Color
102  //void Set(Fl_Color c);
103 
104  /// set the values from explicit (floating point) RGB values
105  void Set(float r, float g, float b);
106 
107  /// set the values from explicit (floating point) RGB+alpha values
108  void Set(float r, float g, float b, float a);
109 
110  /// set the values from explicit (unsigned char) RGB values. These
111  /// are normalized internally to the range 0-1.
112  void Set(unsigned char r, unsigned char g, unsigned char b);
113 
114  /// set the values from explicit (unsigned char) RGB+alpha values. These
115  /// are normalized internally to the range 0-1.
116  void Set(unsigned char r, unsigned char g,
117  unsigned char b, unsigned char a);
118 
119  /// Set specific channels from floating point values
120  void SetRed (float r);
121  void SetGreen(float r);
122  void SetBlue (float r);
123  void SetAlpha(float r);
124 
125  /// Set specific channels from floating unsigned char values. These
126  /// are normalized internally to the range 0-1.
127  void SetRed (unsigned char r);
128  void SetGreen(unsigned char r);
129  void SetBlue (unsigned char r);
130  void SetAlpha(unsigned char r);
131 
132  /// Get specific channels in floating point values.
133  float GetRed (void) const;
134  float GetGreen(void) const;
135  float GetBlue (void) const;
136  float GetAlpha(void) const;
137 
138  /// Get specific channels in unsigned char values.
139  unsigned char GetRedUC (void) const;
140  unsigned char GetGreenUC(void) const;
141  unsigned char GetBlueUC (void) const;
142  unsigned char GetAlphaUC(void) const;
143  CVect4<unsigned char> GetRgbaUC();
144  CVect3<unsigned char> GetRgbUC();
145 
146  /// Access the color array directly.
147  const float* GetColorArray(void) const;
148 
149  /// print the color to a stream in the form "r g b"
150  void PrintTo(CNcbiOstream& strm) const;
151  void PrintTo(CNcbiOstream& strm, bool printAlpha, bool uchars = true) const;
152 
153  /// Return a string representation of the current color
154  string ToString(bool printAlpha = true, bool uchars = true) const;
155  /// Return a CSS string representation of the current color
156  string ToCssString(bool printAlpha = true) const;
157  /// Return an HTML string representation of the current color
158  string ToHtmlString() const;
159 
160  /// Assign color values encoded in a string.
161  /// This function is responsible for all string-based processing;
162  /// all other string-based interfaces funnel through this. The formats
163  /// accepted include, in order of parsing:
164  ///
165  /// 1. A named color, such as "red", "blue", "green", "salmon", etc.
166  /// There are approximately 750 named colors that are currently
167  /// understood.
168  ///
169  /// 2. HTML-format colors, such as "#f0f0dd", or "f0f0dd". These are
170  /// accepted with or without the leading '#'.
171  ///
172  /// 3. Bracketed or non-bracketed triplets or quadruplets of integers, in
173  /// any of the formats below (mix and match spaces or commas as separators,
174  /// with or without '[]' or '()'):
175  ///
176  /// 123 123 123
177  /// 123, 123, 123
178  /// [123 123 123]
179  /// (123, 123, 123)
180  /// (123 123 123)
181  /// [123, 123, 123]
182  ///
183  void FromString(const string& str);
184 
185  // add a color to this color
186  CRgbaColor& operator+=(const CRgbaColor& c1);
187 
188  // multiply this color by a scalar
189  CRgbaColor& operator*=(float f);
190 
191  // lighten a color by a scaling factor. Scale must range from 0-1.
192  // This is equivalent to interpolating the color with white, with the
193  // exception that the alpha channel is not altered.
194  void Lighten(float scale);
195 
196  // lighten a color by a scaling factor. Scale must range from 0-1.
197  // This is equivalent to interpolating the color with black, with the
198  // exception that the alpha channel is not altered.
199  void Darken(float scale);
200 
201  /// Returns the greyscale equivalent of the current color
202  CRgbaColor GetGreyscale() const;
203 
204  /// return a mapped color string from a named color
205  static const char* ColorStrFromName(const string& desc);
206 
207  /// return a color based on a string. This function will understand two
208  /// forms of color type:
209  /// - color names - things like 'red', 'powder blue', etc.
210  /// - color triplets or quads - RGB(A) values encoded as '128 128 128'
211  /// tokens include ' ,[', so all of the following are valid:
212  /// 128 128 128
213  /// [128 128 128]
214  /// 128, 128, 128
215  /// [128, 128, 128]
216  static CRgbaColor GetColor(const string& color_type);
217 
218  /// Interpolate two colors. The resulting color returned is
219  /// ( (color1 * alpha) + (color2 * (1 - alpha) )
220  static CRgbaColor Interpolate(const CRgbaColor& color1,
221  const CRgbaColor& color2,
222  float alpha);
223 
224  /// returns XOR complementary color, alpha is not affected
225  static CRgbaColor Invert(const CRgbaColor& color1);
226 
227  /// @name Color space conversion functions.
228  /// @{
229 
230  /// convert RGB to HSV.
231  /// @param h Hue. range 0 - 360.
232  /// @param s Saturation. range 0 - 1.0
233  /// @param v Value. range 0 - 1.0
234  static void RgbToHsv(const CRgbaColor& rgb, float& h, float& s, float& v);
235  static CRgbaColor HsvToRgb(float h, float s, float v);
236 
237  /// convert RGB to YUV.
238  /// @param y luminance or brightness. range [0, 1]
239  /// @param u difference between blue component and luminance. range [-.5, .5]
240  /// @param v difference between red component and luminance. range [-.5, .5]
241  static void RgbToYuv(const CRgbaColor& rgb, float& y, float& u, float& v);
242  /// Not all possible input values of y, u and v will make a valid RGB color.
243  /// in that case the RGB values are clamped to [0, 1].
244  /// @return false if clipping occurs.
245  /// see http://developer.apple.com/quicktime/icefloe/dispatch027.html
246  static bool YuvToRgb(float y, float u, float v, CRgbaColor& rgb);
247 
248  /// @}
249 
250  /// Rotate the hue of the color by degrees.
251  static CRgbaColor RotateColor(const CRgbaColor& c, float degrees);
252  /// return the brightness or luminance of the color.
253  /// Same as Y in RgbToYuv
254  /// @sa RgbToYuv
255  static float Brightness(const CRgbaColor& rgb);
256  /// returns the distance in the RGB color cube between the two colors,
257  /// scaled to a range [0, 1].
258  static float ColorDistance(const CRgbaColor& c1, const CRgbaColor& c2);
259 
260  /// Return a color guaranteed to contrast nicely with this color.
261  /// @param onlyBW true - only return black or white, otherwise pick from all colors.
262  /// @returns A color that will show up well against this color.
263  CRgbaColor ContrastingColor(bool onlyBW = true) const;
264 
265 private:
266  void x_Clamp();
267 
268  float m_Rgba[4];
269 };
270 
271 
272 /// global comparison operator
274  bool operator==(const CRgbaColor& c1, const CRgbaColor& c2);
275 
276 /// define a some-what arbitrary sort order based on brightness
277 /// It does have the property of being equivalent to operator==, that is:
278 /// ( ! a<b && ! b<a ) iff a == b
280  bool operator<(const CRgbaColor& c1, const CRgbaColor& c2);
281 
282 /// add two colors
284  CRgbaColor operator+(const CRgbaColor& c1, const CRgbaColor& c2);
285 
286 
287 /// multiply a color by a scalar
289  CRgbaColor operator*(const CRgbaColor& c1, float f);
290 
291 
292 
293 inline
294 void CRgbaColor::SetRed(unsigned char r)
295 {
296  m_Rgba[0] = float(r) / 255.0f;
297 }
298 
299 
300 inline
301 void CRgbaColor::SetGreen(unsigned char g)
302 {
303  m_Rgba[1] = float(g) / 255.0f;
304 }
305 
306 
307 inline
308 void CRgbaColor::SetBlue(unsigned char b)
309 {
310  m_Rgba[2] = float(b) / 255.0f;
311 }
312 
313 
314 inline
315 void CRgbaColor::SetAlpha(unsigned char a)
316 {
317  m_Rgba[3] = float(a) / 255.0f;
318 }
319 
320 inline
321 float CRgbaColor::GetRed(void) const
322 {
323  return m_Rgba[0];
324 }
325 
326 inline
327 float CRgbaColor::GetGreen(void) const
328 {
329  return m_Rgba[1];
330 }
331 
332 inline
333 float CRgbaColor::GetBlue(void) const
334 {
335  return m_Rgba[2];
336 }
337 
338 inline
339 float CRgbaColor::GetAlpha(void) const
340 {
341  return m_Rgba[3];
342 }
343 
344 inline
345 unsigned char CRgbaColor::GetRedUC(void) const
346 {
347  return (unsigned char) (m_Rgba[0] * 255.0f);
348 }
349 
350 inline
351 unsigned char CRgbaColor::GetGreenUC(void) const
352 {
353  return (unsigned char) (m_Rgba[1] * 255.0f);
354 }
355 
356 inline
357 unsigned char CRgbaColor::GetBlueUC(void) const
358 {
359  return (unsigned char) (m_Rgba[2] * 255.0f);
360 }
361 
362 inline
363 unsigned char CRgbaColor::GetAlphaUC(void) const
364 {
365  return (unsigned char) (m_Rgba[3] * 255.0f);
366 }
367 
368 inline
370 {
372 
373  rgba[0] = GetRedUC();
374  rgba[1] = GetGreenUC();
375  rgba[2] = GetBlueUC();
376  rgba[3] = GetAlphaUC();
377 
378  return rgba;
379 }
380 
381 inline
383 {
385 
386  rgb[0] = GetRedUC();
387  rgb[1] = GetGreenUC();
388  rgb[2] = GetBlueUC();
389 
390  return rgb;
391 }
392 
393 inline
394 const float* CRgbaColor::GetColorArray(void) const
395 {
396  return m_Rgba;
397 }
398 
399 
401 
402 
403 /* @} */
404 
405 #endif // GUI_UTILS___RGBA_COLOR__HPP
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
Definition: vect3.hpp:48
Include a standard set of the NCBI C++ Toolkit most basic headers.
CRgbaColor & GetColor(CSeqFeatData::ESubtype subtype)
static const char * str(char *buf, int n)
Definition: stats.c:84
unsigned char GetRedUC(void) const
Get specific channels in unsigned char values.
Definition: rgba_color.hpp:345
float GetBlue(void) const
Definition: rgba_color.hpp:333
float GetGreen(void) const
Definition: rgba_color.hpp:327
unsigned char GetGreenUC(void) const
Definition: rgba_color.hpp:351
unsigned char GetAlphaUC(void) const
Definition: rgba_color.hpp:363
void SetAlpha(float r)
Definition: rgba_color.cpp:287
float GetAlpha(void) const
Definition: rgba_color.hpp:339
void SetGreen(float r)
Definition: rgba_color.cpp:275
bool operator<(const CRgbaColor &c1, const CRgbaColor &c2)
define a some-what arbitrary sort order based on brightness It does have the property of being equiva...
Definition: rgba_color.cpp:111
CRgbaColor operator*(const CRgbaColor &c1, float f)
multiply a color by a scalar
Definition: rgba_color.cpp:92
const float * GetColorArray(void) const
Access the color array directly.
Definition: rgba_color.hpp:394
unsigned char GetBlueUC(void) const
Definition: rgba_color.hpp:357
void SetBlue(float r)
Definition: rgba_color.cpp:281
CRgbaColor operator+(const CRgbaColor &c1, const CRgbaColor &c2)
add two colors
Definition: rgba_color.cpp:72
float m_Rgba[4]
Definition: rgba_color.hpp:268
bool operator==(const CRgbaColor &c1, const CRgbaColor &c2)
global comparison operator
Definition: rgba_color.cpp:102
CVect3< unsigned char > GetRgbUC()
Definition: rgba_color.hpp:382
void SetRed(float r)
Set specific channels from floating point values.
Definition: rgba_color.cpp:269
CVect4< unsigned char > GetRgbaUC()
Definition: rgba_color.hpp:369
float GetRed(void) const
Get specific channels in floating point values.
Definition: rgba_color.hpp:321
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
#define NCBI_GUIUTILS_EXPORT
Definition: gui_export.h:518
n background color
CNcbiMatrix< T > & operator+=(CNcbiMatrix< T > &, const CNcbiMatrix< U > &)
global addition: matrix += matrix
Definition: matrix.hpp:570
CNcbiMatrix< T > & operator*=(CNcbiMatrix< T > &, const CNcbiMatrix< U > &)
global multiplication: matrix *= matrix
Definition: matrix.hpp:889
const struct ncbi::grid::netcache::search::fields::SIZE size
unsigned int a
Definition: ncbi_localip.c:102
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
int g(Seg_Gsm *spe, Seq_Mtf *psm, Thd_Gsm *tdg)
Definition: thrddgri.c:44
string ToString(const wxRect &rc)
Definition: wx_utils.cpp:773
Modified on Sat Apr 27 11:24:08 2024 by modify_doxy.py rev. 669887