OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimHsvVector.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // Author: Garrett Potts (gpotts@remotesensing.org)
6 // Description:
7 //
8 //*************************************************************************
9 // $Id: ossimHsvVector.cpp 9963 2006-11-28 21:11:01Z gpotts $
10 #include <algorithm>
11 //using namespace std;
12 
15 #include <math.h>
16 #include <ossim/base/ossimCommon.h>
17 
20 {
21  float r,g,b;
22 
23  r = rgb.getR()/255.0;
24  g = rgb.getG()/255.0;
25  b = rgb.getB()/255.0;
26 
27  // this code was taken from http://www.acm.org/jgt/papers/SmithLyons96/hsv_rgb.html
28  // the input RGB is required to be on [0, 1].
29  // Their S and V are returned on [0, 1] and H is but we will try
30  // to make it return on 0..255 for S and V and see what that looks
31  // like and keep the H on 0..6.
32  // returned on [0, 6]. Exception: H is returned UNDEFINED if S==0.
33 
34  float x, v, f, i;
35 
36  x = min(min(r, g), b);
37  v = max(max(r, g), b);
38  if(v==x)
39  {
40  theBuf[0] = OSSIM_HSV_UNDEFINED, // we really need to mark as undefined
41  theBuf[1] = 0;
42  theBuf[2] = v;
43  }
44  else
45  {
46  f = (r == x) ? g - b : ((g == x) ? b - r : r - g);
47  i = (r == x) ? 3 : ((g == x) ? 5 : 1);
48 
49  theBuf[0] = clamp(i - f /(v - x), 0, 6);
50  theBuf[0]/=6.0;
51  theBuf[1] = clamp((v - x)/v, 0, 1);
52  theBuf[2] = clamp(v, 0, 1);
53  }
54 }
55 
57 {
58  float r,g,b;
59 
60  r = rgb.getR()/255.0;
61  g = rgb.getG()/255.0;
62  b = rgb.getB()/255.0;
63 
64  // this code was taken from http://www.acm.org/jgt/papers/SmithLyons96/hsv_rgb.html
65  // the input RGB is required to be on [0, 1].
66  // Their S and V are returned on [0, 1] and H is but we will try
67  // to make it return on 0..255 for S and V and see what that looks
68  // like and keep the H on 0..6.
69  // returned on [0, 6]. Exception: H is returned UNDEFINED if S==0.
70 
71  float x, v, f, i;
72 
73  x = min(min(r, g), b);
74  v = max(max(r, g), b);
75  if(v==x)
76  {
77  theBuf[0] = OSSIM_HSV_UNDEFINED, // we really need to mark as undefined
78  theBuf[1] = 0;
79  theBuf[2] = v;
80  }
81  else
82  {
83  f = (r == x) ? g - b : ((g == x) ? b - r : r - g);
84  i = (r == x) ? 3 : ((g == x) ? 5 : 1);
85 
86  theBuf[0] = clamp(i - f /(v - x), 0, 6);
87  theBuf[0]/=6.0;
88  theBuf[1] = clamp((v - x)/v, 0, 1);
89  theBuf[2] = clamp(v, 0, 1);
90  }
91 
92  return *this;
93 }
ossim_uint32 x
unsigned char getR() const
float clamp(float colorValue, float min=0, float max=255) const
static const float OSSIM_HSV_UNDEFINED
ossimHsvVector(float h=0, float s=0, float i=0)
const ossimHsvVector & operator=(const ossimRgbVector &rgb)
unsigned char getB() const
unsigned char getG() const
#define max(a, b)
Definition: auxiliary.h:76
#define min(a, b)
Definition: auxiliary.h:75