Shiokaze Framework
A research-oriented fluid solver for computer graphics
color.h
Go to the documentation of this file.
1 /*
2 ** color.h
3 **
4 ** This is part of Shiokaze, a research-oriented fluid solver for computer graphics.
5 ** Created by Ryoichi Ando <rand@nii.ac.jp> on Feb 7, 2017.
6 **
7 ** Transformed from:
8 ** https://stackoverflow.com/questions/3018313/algorithm-to-convert-rgb-to-hsv-and-hsv-to-rgb-in-range-0-255-for-both
9 **
10 */
11 //
12 #ifndef SHKZ_COLOR_H
13 #define SHKZ_COLOR_H
14 //
15 #include <cmath>
16 #include <ostream>
17 #include <vector>
18 #include <shiokaze/core/common.h>
19 //
21 //
23 class color {
26 public:
31  struct rgb {
36  double r;
41  double g;
46  double b;
47  };
52  struct hsv {
57  double h;
62  double s;
67  double v;
68  };
77  static hsv rgb2hsv(rgb in) {
78  hsv out;
79  double min, max, delta;
80 
81  min = in.r < in.g ? in.r : in.g;
82  min = min < in.b ? min : in.b;
83 
84  max = in.r > in.g ? in.r : in.g;
85  max = max > in.b ? max : in.b;
86 
87  out.v = max; // v
88  delta = max - min;
89  if( max > 0.0 ) { // NOTE: if Max is == 0, this divide would cause a crash
90  out.s = (delta / max); // s
91  } else {
92  // if max is 0, then r = g = b = 0
93  // s = 0, v is undefined
94  out.s = 0.0;
95  out.h = NAN; // its now undefined
96  return out;
97  }
98  if( in.r >= max ) // > is bogus, just keeps compilor happy
99  out.h = ( in.g - in.b ) / delta; // between yellow & magenta
100  else
101  if( in.g >= max )
102  out.h = 2.0 + ( in.b - in.r ) / delta; // between cyan & yellow
103  else
104  out.h = 4.0 + ( in.r - in.g ) / delta; // between magenta & cyan
105 
106  out.h *= 60.0; // degrees
107 
108  if( out.h < 0.0 )
109  out.h += 360.0;
110 
111  return out;
112  }
121  static rgb hsv2rgb(hsv in) {
122  double hh, p, q, t, ff;
123  long i;
124  rgb out;
125 
126  if(in.s <= 0.0) { // < is bogus, just shuts up warnings
127  out.r = in.v;
128  out.g = in.v;
129  out.b = in.v;
130  return out;
131  }
132  hh = in.h;
133  if(hh >= 360.0) hh = 0.0;
134  hh /= 60.0;
135  i = (long)hh;
136  ff = hh - i;
137  p = in.v * (1.0 - in.s);
138  q = in.v * (1.0 - (in.s * ff));
139  t = in.v * (1.0 - (in.s * (1.0 - ff)));
140 
141  switch(i) {
142  case 0:
143  out.r = in.v;
144  out.g = t;
145  out.b = p;
146  break;
147  case 1:
148  out.r = q;
149  out.g = in.v;
150  out.b = p;
151  break;
152  case 2:
153  out.r = p;
154  out.g = in.v;
155  out.b = t;
156  break;
157 
158  case 3:
159  out.r = p;
160  out.g = q;
161  out.b = in.v;
162  break;
163  case 4:
164  out.r = t;
165  out.g = p;
166  out.b = in.v;
167  break;
168  case 5:
169  default:
170  out.r = in.v;
171  out.g = p;
172  out.b = q;
173  break;
174  }
175  return out;
176  }
185  static void heatcolor( double heat, double rgb_result[3] ) {
186  hsv hsv_color;
187  heat = std::min(1.0,std::max(0.0,heat));
188  hsv_color.h = 230.0 * (1.0-heat);
189  hsv_color.s = 0.5;
190  hsv_color.v = 0.8;
191  rgb rgb_color = color::hsv2rgb(hsv_color);
192  rgb_result[0] = rgb_color.r;
193  rgb_result[1] = rgb_color.g;
194  rgb_result[2] = rgb_color.b;
195  }
196 };
197 //
199 #endif
color
Class that converts color spaces.
Definition: color.h:25
color::rgb::g
double g
Green color (0.0-1.0).
Definition: color.h:41
color::rgb2hsv
static hsv rgb2hsv(rgb in)
Convert RGB to HSV.
Definition: color.h:77
color::hsv::h
double h
Angle in degrees (0.0-360.0).
Definition: color.h:57
color::hsv::v
double v
Brightness (0.0-1.0).
Definition: color.h:67
color::rgb
Structre that defines RGB color.
Definition: color.h:31
color::hsv2rgb
static rgb hsv2rgb(hsv in)
Convert HSV to RGB.
Definition: color.h:121
SHKZ_BEGIN_NAMESPACE
#define SHKZ_BEGIN_NAMESPACE
Name space beggining definition for shiokaze.
Definition: common.h:39
color::hsv::s
double s
Saturation (0.0-1.0).
Definition: color.h:62
color::rgb::r
double r
Red color (0.0-1.0).
Definition: color.h:36
color::heatcolor
static void heatcolor(double heat, double rgb_result[3])
Convert heat to RGB color.
Definition: color.h:185
common.h
color::rgb::b
double b
Blue color (0.0-1.0).
Definition: color.h:46
SHKZ_END_NAMESPACE
#define SHKZ_END_NAMESPACE
Name space end definition for shiokaze.
Definition: common.h:44
color::hsv
Structre that defines HSV color.
Definition: color.h:52