Tetrapod Project
hsvrgb.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014, Jan Winkler <winkler@cs.uni-bremen.de>
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of Universität Bremen nor the names of its
13 // contributors may be used to endorse or promote products derived from
14 // this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
27 
28 /* Author: Jan Winkler */
29 
31 
32 
49 void RGBtoHSV(double& fR, double& fG, double fB, double& fH, double& fS, double& fV) {
50  double fCMax = std::max(std::max(fR, fG), fB);
51  double fCMin = std::min(std::min(fR, fG), fB);
52  double fDelta = fCMax - fCMin;
53 
54  if(fDelta > 0) {
55  if(fCMax == fR) {
56  fH = 60 * (std::fmod(((fG - fB) / fDelta), 6));
57  } else if(fCMax == fG) {
58  fH = 60 * (((fB - fR) / fDelta) + 2);
59  } else if(fCMax == fB) {
60  fH = 60 * (((fR - fG) / fDelta) + 4);
61  }
62 
63  if(fCMax > 0) {
64  fS = fDelta / fCMax;
65  } else {
66  fS = 0;
67  }
68 
69  fV = fCMax;
70  } else {
71  fH = 0;
72  fS = 0;
73  fV = fCMax;
74  }
75 
76  if(fH < 0) {
77  fH = 360 + fH;
78  }
79 }
80 
96 void HSVtoRGB(double& fR, double& fG, double& fB, double& fH, double& fS, double& fV) {
97  double fC = fV * fS; // Chroma
98  double fHPrime = std::fmod(fH / 60.0, 6);
99  double fX = fC * (1 - std::fabs(std::fmod(fHPrime, 2) - 1));
100  double fM = fV - fC;
101 
102  if(0 <= fHPrime && fHPrime < 1) {
103  fR = fC;
104  fG = fX;
105  fB = 0;
106  } else if(1 <= fHPrime && fHPrime < 2) {
107  fR = fX;
108  fG = fC;
109  fB = 0;
110  } else if(2 <= fHPrime && fHPrime < 3) {
111  fR = 0;
112  fG = fC;
113  fB = fX;
114  } else if(3 <= fHPrime && fHPrime < 4) {
115  fR = 0;
116  fG = fX;
117  fB = fC;
118  } else if(4 <= fHPrime && fHPrime < 5) {
119  fR = fX;
120  fG = 0;
121  fB = fC;
122  } else if(5 <= fHPrime && fHPrime < 6) {
123  fR = fC;
124  fG = 0;
125  fB = fX;
126  } else {
127  fR = 0;
128  fG = 0;
129  fB = 0;
130  }
131 
132  fR += fM;
133  fG += fM;
134  fB += fM;
135 }
void RGBtoHSV(double &fR, double &fG, double fB, double &fH, double &fS, double &fV)
Convert RGB to HSV color space.
Definition: hsvrgb.cpp:49
void HSVtoRGB(double &fR, double &fG, double &fB, double &fH, double &fS, double &fV)
Convert HSV to RGB color space.
Definition: hsvrgb.cpp:96