VPTissue Reference Manual
Rgb2Hsv.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
21 #include "Rgb2Hsv.h"
22 
23 #include <array>
24 #include <cmath>
25 
26 namespace SimPT_Sim {
27 namespace Color {
28 
29 using namespace std;
30 
31 array<double, 3> Rgb2Hsv(double r, double g, double b)
32 {
33  array<double, 3> ret_val {{0.0, 0.0, 0.0}};
34  const auto mini = min( r, min(g, b) );
35  const auto maxi = max( r, max(g, b) );
36  const auto delta = maxi - mini;
37 
38  if ( maxi != 0.0 ) {
39  const auto v = maxi;
40  const auto s = delta / maxi;
41  auto h = -1.0;
42 
43  if( r == maxi ) {
44  h = ( g - b ) / delta; // between yellow & magenta
45  } else if( g == maxi ) {
46  h = 2.0 + ( b - r ) / delta; // between cyan & yellow
47  } else {
48  h = 4.0 + ( r - g ) / delta; // between magenta & cyan
49  }
50  h *= 60.0; // degrees
51  if( h < 0.0 ) {
52  h += 360.0;
53  }
54  ret_val = {{ h, s, v}};
55  } else {
56  ret_val = {{-1.0, 0.0, 0.0}};
57  }
58 
59  return ret_val;
60 }
61 
62 } // namespace
63 } // namespace
64 
Rgb color def to Hsv color def: r,g,b values are from 0 to 1 and h = [0,360], s = [0...
STL namespace.
Namespace for the core simulator.