Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkColorConversions.h"
18 
19 namespace mitk
20 {
21  namespace ColorConversions
22  {
23  /*
24  Convert HSV color to RGB color (pseudocode from wikipedia)
25 
26  H from 0 to 360
27  S from 0 to 1
28  V from 0 to 1
29 
30  R, G, and B from 0 to 1
31  */
32  void Hsv2Rgb(float h, float s, float v, float &r, float &g, float &b)
33  {
34  if (s == 0.0)
35  {
36  r = g = b = v; // gray
37  return;
38  }
39 
40  int Hi = (int)(h / 60.0) % 6;
41  if (h >= 360)
42  Hi = 6;
43 
44  float f = (float)(h / 60.0 - (float)Hi);
45  float p = (float)(v * (1.0 - s));
46  float q = (float)(v * (1.0 - s * f));
47  float t = (float)(v * (1.0 - s * (1.0 - f)));
48 
49  switch (Hi)
50  {
51  case 0:
52  case 6:
53  r = v;
54  g = t;
55  b = p;
56  break;
57  case 1:
58  r = q;
59  g = v;
60  b = p;
61  break;
62  case 2:
63  r = p;
64  g = v;
65  b = t;
66  break;
67  case 3:
68  r = p;
69  g = q;
70  b = v;
71  break;
72  case 4:
73  r = t;
74  g = p;
75  b = v;
76  break;
77  case 5:
78  r = v;
79  g = p;
80  b = q;
81  break;
82  }
83  }
84  /*
85  Convert RGB color to HSV color
86 
87  R from 0 to 1
88  G from 0 to 1
89  B from 0 to 1
90 
91  H from 0 to 360 and -1 for undefined color (pure white)
92  S from 0 to 1
93  V from 0 to 1
94  */
95  void Rgb2Hsv(float r, float g, float b, float &h, float &s, float &v)
96  {
97  /* r = r/255;
98  b = b/255;
99  g = g/255;*/
100 
101  float mn = r, mx = r;
102  int maxVal = 0;
103 
104  if (g > mx)
105  {
106  mx = g;
107  maxVal = 1;
108  }
109  if (b > mx)
110  {
111  mx = b;
112  maxVal = 2;
113  }
114  if (g < mn)
115  mn = g;
116  if (b < mn)
117  mn = b;
118 
119  float delta = mx - mn;
120 
121  v = mx;
122  if (mx != 0)
123  s = delta / mx;
124  else
125  {
126  s = 0;
127  h = 0;
128  return;
129  }
130  if (s == 0.0f)
131  {
132  h = -1;
133  return;
134  }
135  else
136  {
137  switch (maxVal)
138  {
139  case 0:
140  {
141  h = (g - b) / delta;
142  break;
143  } // yel < h < mag
144  case 1:
145  {
146  h = 2 + (b - r) / delta;
147  break;
148  } // cyan < h < yel
149  case 2:
150  {
151  h = 4 + (r - g) / delta;
152  break;
153  } // mag < h < cyan
154  }
155  }
156 
157  h *= 60;
158  if (h < 0)
159  h += 360;
160  }
161 
162  } // ColorConversion
163 
164 } // 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