ACT-CV - Machine Vision for Cognitive Modeling
RGBtoHSV.h
Go to the documentation of this file.
1 // -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2 
3 /*
4 ACT-CV - Machine Vision for Cognitive Modeling
5 Copyright (c) 2008 Marc Halbruegge
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
26 #ifndef __RGBTOHSV_H
27 #define __RGBTOHSV_H
28 
30 struct HSV {
31  float h;
32  unsigned char s;
33  unsigned char v;
34 };
35 
36 // RGB nach HSV
37 #ifndef MIN
38 #define MIN(a,b) (a<b?a:b)
39 #endif
40 
41 #ifndef MAX
42 #define MAX(a,b) (a>b?a:b)
43 #endif
44 
46 void RGBtoHSV(unsigned char r, unsigned char g, unsigned char b,
47  float &h, unsigned char &s, unsigned char &v ){
48  unsigned char min, max;
49  float delta;
50  min = MIN(MIN( r, g), b );
51  max = MAX(MAX( r, g), b );
52  v = max; // v
53  delta = static_cast<float>(max - min);
54  if( max != 0 ) {
55  s = (unsigned char) ((delta /(float) max) * 255); // s
56  } else {
57  s = 0;
58  }
59 
60  if (s==0) { // either max=0 oder max=min
61  h = 0; //rot
62  return;
63  }
64 
65  if(r == max) {
66  h = ( g - b ) / delta; // between yellow & magenta
67  } else if(g == max) {
68  h = 2 + ( b - r ) / delta; // between cyan & yellow
69  } else {
70  h = 4 + ( r - g ) / delta; // between magenta & cyan
71  }
72 
73  h *= 60; // degrees
74  if (h < 0) h += 360;
75 }
76 
78 void RGBtoHSV(unsigned char r, unsigned char g, unsigned char b,
79  struct HSV &hsv) {
80  RGBtoHSV(r,g,b,hsv.h,hsv.s,hsv.v);
81 }
82 
84 void RGBtoHSV(unsigned int rgb,
85  struct HSV &hsv) {
86  RGBtoHSV((rgb&0xff0000)>>16,
87  (rgb&0x00ff00)>>8,
88  (rgb&0x0000ff),
89  hsv.h,hsv.s,hsv.v);
90 }
91 
92 #endif


ACT-CV - Machine Vision for Cognitive Modeling
© 2015 Marc Halbruegge (actcvlibrary@googlemail.com)