iMSTK
Interactive Medical Simulation Toolkit
imstkColor.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include "imstkMath.h"
10 
11 #include <iostream>
12 
14 #ifdef WIN32
15 #pragma warning(disable : 4201)
16 #endif
17 namespace imstk
18 {
24 struct Color
25 {
26  union
27  {
28  double rgba[4];
29  struct
30  {
31  double r;
32  double g;
33  double b;
34  double a;
35  };
36  };
37 
38  Color();
39  Color(const double red,
40  const double green,
41  const double blue,
42  const double alpha = 1.0);
43  Color(const double* rgba_color);
44  Color(const Color& color, const double alpha);
45 
49  Color& operator=(const Color& p_color);
50 
51  operator Eigen::Matrix<unsigned char, 3, 1>()
52  {
53  return Eigen::Matrix<unsigned char, 3, 1>(
54  static_cast<unsigned char>(r * 255.0),
55  static_cast<unsigned char>(g * 255.0),
56  static_cast<unsigned char>(b * 255.0));
57  }
58  operator Eigen::Matrix<unsigned char, 4, 1>()
59  {
60  return Eigen::Matrix<unsigned char, 4, 1>(
61  static_cast<unsigned char>(r * 255.0),
62  static_cast<unsigned char>(g * 255.0),
63  static_cast<unsigned char>(b * 255.0),
64  static_cast<unsigned char>(a * 255.0));
65  }
66 
70  friend std ::ostream& operator<<(std::ostream& os, const Color& c);
71 
75  double operator()(const int p_i) const;
76 
80  void setValue(const double p_red,
81  const double p_green,
82  const double p_blue,
83  const double p_alpha = 1.0);
84 
88  void getValue(double color[4]);
89 
93  const double* getValue() const;
94 
98  std::string rgbHex();
99 
100  static Color darken(const Color color, const double factor);
101  static Color lighten(const Color color, const double factor);
102 
103  static Color clamp(const Color color, const Color min, const Color max);
104 
108  static Color lerpRgba(const Color& start, const Color& end, const double t);
109  static Color lerpRgb(const Color& start, const Color& end, const double t);
110 
112  static Color White;
113  static Color Black;
114  static Color DarkGray;
115  static Color LightGray;
116  static Color Blue;
117  static Color Green;
118  static Color Red;
119  static Color Pink;
120  static Color Orange;
121  static Color Yellow;
122  static Color Teal;
123  static Color Marigold;
124  static Color Bone;
125  static Color YellowBone;
126  static Color Blood;
127  static Color LightSkin;
128  static Color DarkSkin;
129 };
130 #ifdef WIN32
131 #pragma warning(default : 4201)
132 #endif
133 
137 Color operator*(const Color& color_lhs, const Color& color_rhs);
138 Color operator*(const Color& color_lhs, const double intensity_rhs);
139 Color operator*=(const Color& color_lhs, const Color& color_rhs);
140 Color operator*=(const Color& color_lhs, const double intensity_rhs);
141 
145 Color operator+(const Color& color_lhs, const Color& color_rhs);
146 Color operator+(const Color& intensity_lhs, const double intensity_rhs);
147 Color operator+=(const Color& color_lhs, const Color& color_rhs);
148 Color operator+=(const Color& intensity_lhs, const double intensity_rhs);
149 
153 Color operator-(const Color& color_lhs, const Color& color_rhs);
154 Color operator-(const Color& color_rhs, const double intensity_lhs);
155 Color operator-=(const Color& color_lhs, const Color& color_rhs);
156 Color operator-=(const Color& color_rhs, const double intensity_lhs);
157 
161 bool operator==(const Color& color_lhs, const Color& color_rhs);
162 bool operator!=(const Color& color_lhs, const Color& color_rhs);
163 } // namespace imstk
Color operator-(const Color &color_lhs, const Color &color_rhs)
Subtraction operators.
Definition: imstkColor.cpp:266
Compound Geometry.
double operator()(const int p_i) const
returns the color value given with the index
Definition: imstkColor.cpp:75
friend std ::ostream & operator<<(std::ostream &os, const Color &c)
Bitwise operator.
Definition: imstkColor.cpp:86
void setValue(const double p_red, const double p_green, const double p_blue, const double p_alpha=1.0)
set RGB color
Definition: imstkColor.cpp:96
static Color lerpRgba(const Color &start, const Color &end, const double t)
interpolate between two colors by ratio t
Definition: imstkColor.cpp:153
std::string rgbHex()
Definition: imstkColor.cpp:165
Color in RGB space.
Definition: imstkColor.h:24
static Color White
Various commonly used colors.
Definition: imstkColor.h:112
const double * getValue() const
get RGB color
Definition: imstkColor.cpp:125
bool operator==(const Color &color_lhs, const Color &color_rhs)
Comparison operator.
Definition: imstkColor.cpp:310
Color & operator=(const Color &p_color)
Equality operator.
Definition: imstkColor.cpp:65
Color operator*(const Color &color_lhs, const Color &color_rhs)
Multiplication operators.
Definition: imstkColor.cpp:178
Color operator+(const Color &color_lhs, const Color &color_rhs)
Addition operators.
Definition: imstkColor.cpp:222