Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkColorConversions.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkColorConversions.h"
14 
15 namespace mitk
16 {
17  namespace ColorConversions
18  {
19  /*
20  Convert HSV color to RGB color (pseudocode from wikipedia)
21 
22  H from 0 to 360
23  S from 0 to 1
24  V from 0 to 1
25 
26  R, G, and B from 0 to 1
27  */
28  void Hsv2Rgb(float h, float s, float v, float &r, float &g, float &b)
29  {
30  if (s == 0.0)
31  {
32  r = g = b = v; // gray
33  return;
34  }
35 
36  int Hi = (int)(h / 60.0) % 6;
37  if (h >= 360)
38  Hi = 6;
39 
40  auto f = (float)(h / 60.0 - (float)Hi);
41  auto p = (float)(v * (1.0 - s));
42  auto q = (float)(v * (1.0 - s * f));
43  auto t = (float)(v * (1.0 - s * (1.0 - f)));
44 
45  switch (Hi)
46  {
47  case 0:
48  case 6:
49  r = v;
50  g = t;
51  b = p;
52  break;
53  case 1:
54  r = q;
55  g = v;
56  b = p;
57  break;
58  case 2:
59  r = p;
60  g = v;
61  b = t;
62  break;
63  case 3:
64  r = p;
65  g = q;
66  b = v;
67  break;
68  case 4:
69  r = t;
70  g = p;
71  b = v;
72  break;
73  case 5:
74  r = v;
75  g = p;
76  b = q;
77  break;
78  }
79  }
80  /*
81  Convert RGB color to HSV color
82 
83  R from 0 to 1
84  G from 0 to 1
85  B from 0 to 1
86 
87  H from 0 to 360 and -1 for undefined color (pure white)
88  S from 0 to 1
89  V from 0 to 1
90  */
91  void Rgb2Hsv(float r, float g, float b, float &h, float &s, float &v)
92  {
93  /* r = r/255;
94  b = b/255;
95  g = g/255;*/
96 
97  float mn = r, mx = r;
98  int maxVal = 0;
99 
100  if (g > mx)
101  {
102  mx = g;
103  maxVal = 1;
104  }
105  if (b > mx)
106  {
107  mx = b;
108  maxVal = 2;
109  }
110  if (g < mn)
111  mn = g;
112  if (b < mn)
113  mn = b;
114 
115  float delta = mx - mn;
116 
117  v = mx;
118  if (mx != 0)
119  s = delta / mx;
120  else
121  {
122  s = 0;
123  h = 0;
124  return;
125  }
126  if (s == 0.0f)
127  {
128  h = -1;
129  return;
130  }
131  else
132  {
133  switch (maxVal)
134  {
135  case 0:
136  {
137  h = (g - b) / delta;
138  break;
139  } // yel < h < mag
140  case 1:
141  {
142  h = 2 + (b - r) / delta;
143  break;
144  } // cyan < h < yel
145  case 2:
146  {
147  h = 4 + (r - g) / delta;
148  break;
149  } // mag < h < cyan
150  }
151  }
152 
153  h *= 60;
154  if (h < 0)
155  h += 360;
156  }
157 
158  } // ColorConversion
159 
160 } // mitk
DataCollection - Class to facilitate loading/accessing structured data.
void Rgb2Hsv(float r, float g, float b, float &h, float &s, float &v)
convert a RGB color to HSV color, rgb parameters from 0 to 1
void Hsv2Rgb(float h, float s, float v, float &r, float &g, float &b)
convert a HSV color to RGB color, H from 0 to 360, all other parameters 0 to 1