-
June 11th, 2014, 05:47 AM
#1
Convert HSB color to RGB and RGB to HSB
Hi, I am trying to make this code working and to remake it to work also for RGB to HSB, can you help? This code is for C and I need to make it working in Visual Studio C++ .
Code:
// mask1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
typedef struct RGB_t { unsigned char red, green, blue; } RGB;
typedef struct HSB_t { float hue, saturation, brightness; } HSB;
/*
* Returns the hue, saturation, and brightness of the color.
*/
void RgbToHsb(struct RGB_t rgb, struct HSB_t* outHsb)
{
// TODO check arguments
float r = rgb.red / 255.0f;
float g = rgb.green / 255.0f;
float b = rgb.blue / 255.0f;
float max = fmaxf(fmaxf(r, g), b);
float min = fminf(fminf(r, g), b);
float delta = max - min;
if (delta != 0)
{
float hue;
if (r == max)
{
hue = (g - b) / delta;
}
else
{
if (g == max)
{
hue = 2 + (b - r) / delta;
}
else
{
hue = 4 + (r - g) / delta;
}
}
hue *= 60;
if (hue < 0) hue += 360;
outHsb->hue = hue;
}
else
{
outHsb->hue = 0;
}
outHsb->saturation = max == 0 ? 0 : (max - min) / max;
outHsb->brightness = max;
}
int main()
{
struct RGB_t rgb = { 132, 34, 255 };
struct HSB_t hsb;
RgbToHsb(rgb, &hsb);
printf("RGB(%u,%u,%u) -> HSB(%f,%f,%f)\n", rgb.red, rgb.green, rgb.blue,
hsb.hue, hsb.saturation, hsb.brightness);
// prints: RGB(132,34,255) -> HSB(266.606354,0.866667,1.000000)
return 0;
}
Error I got is error C3861: 'fmaxf': identifier not found
What file or namespace should I include. Do I need to add some library into linker?
-
June 11th, 2014, 05:58 AM
#2
Re: Convert HSB color to RGB and RGB to HSB
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 11th, 2014, 06:05 AM
#3
Re: Convert HSB color to RGB and RGB to HSB
I tried to include the header amp_math.h but I got error
Cannot open include file: 'amp_math.h': No such file or directory
-
June 11th, 2014, 06:08 AM
#4
Re: Convert HSB color to RGB and RGB to HSB
I found I cannot use AMP because I have Win XP. Any other solution?
-
June 11th, 2014, 06:57 AM
#5
Re: Convert HSB color to RGB and RGB to HSB
I am working on own solution but I have error:
Code:
// mask1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
struct RGB {
int r;
int g;
int b;
} rgb;
struct HSV {
float h;
float s;
float v;
} hsv;
/*
* Returns the hue, saturation, and brightness of the color.
*/
VOID rgb2hsv(struct RGB &rgb, struct HSV &hsv)
{
int R,G,B; float H,S,V, dR,dG,dB; // H,S,V and delta components
// struct hsv { float h; float s; float v; };
R = ( rgb.r / 255 ); //RGB from 0 to 255
G = ( rgb.g / 255 );
B = ( rgb.b / 255 );
int varMin = min( rgb.r, rgb.g, rgb.b ); //Min. value of RGB
int varMax = max( rgb.r, rgb.g, rgb.b ); //Max. value of RGB
int dMax = varMax - varMin; //Delta RGB value
int V = varMax;
if ( dMax == 0 ) //This is a gray, no chroma...
{
hsv.h = 0; //HSV results from 0 to 1
hsv.s = 0;
}
else //Chromatic data...
{
hsv.s = dMax / varMax ;
dR = ( ( ( varMax - R ) / 6 ) + ( dMax / 2 ) ) / dMax ;
dG = ( ( ( varMax - G ) / 6 ) + ( dMax / 2 ) ) / dMax ;
dB = ( ( ( varMax - B ) / 6 ) + ( dMax / 2 ) ) / dMax ;
if ( R == varMax ) H = dB - dG ;
else if ( G == varMax ) H = ( 1 / 3 ) + dR - dB ;
else if ( B == varMax ) H = ( 2 / 3 ) + dG - dR ;
if ( H < 0 ) H += 1 ;
if ( H > 1 ) H -= 1 ;
}
}
int main()
{
struct RGB_t rgb = { 132, 34, 255 };
struct HSV_t hsb;
rgb2hsv(&rgb, &hsb);
printf("RGB(%u,%u,%u) -> HSB(%f,%f,%f)\n", rgb.r, rgb.g, rgb.b,
hsb.h, hsb.s, hsb.b);
// prints: RGB(132,34,255) -> HSB(266.606354,0.866667,1.000000)
return 0;
}
C2146: syntax error : missing ';' before identifier 'rgb2hsv'
k1.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
refers to line 23: VOID rgb2hsv ....
I cannot find out where I am missing ;
-
June 11th, 2014, 07:07 AM
#6
Re: Convert HSB color to RGB and RGB to HSB
Where are structs RGB_t and HSV_t defined?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 11th, 2014, 07:16 AM
#7
Re: Convert HSB color to RGB and RGB to HSB
Originally Posted by crazy boy
C2146: syntax error : missing ';' before identifier 'rgb2hsv'
k1.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
refers to line 23: VOID rgb2hsv ....
Try to replace VOID with void.
And note that there is a lot of other bugs in your code.
Victor Nijegorodov
-
June 11th, 2014, 07:42 AM
#8
Re: Convert HSB color to RGB and RGB to HSB
I fixed those errors. Thanks. But is there function to find min and max values in C++? Or should I use this template?
http://www.cplusplus.com/reference/algorithm/max/
-
June 11th, 2014, 08:01 AM
#9
Re: Convert HSB color to RGB and RGB to HSB
How correctly use std::min_element and std::max_element to find the minimal value and maximal value?
Code:
// mask1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <algorithm>
struct RGB { int r; int g; int b; } rgb;
struct HSV { float h; float s; float v; } hsv;
bool minFn(int i, int j) { return i<j; }
bool maxFn(int i, int j) { return i>j; }
// Returns the hue, saturation, and brightness of the color
void rgb2hsv(struct RGB &rgb, struct HSV &hsv)
{
int R,G,B; float H,S,V, dR,dG,dB; // H,S,V and delta components
// struct hsv { float h; float s; float v; };
R = ( rgb.r / 255 ); //RGB from 0 to 255
G = ( rgb.g / 255 );
B = ( rgb.b / 255 );
int varMin = *std::min_element( rgb, 255, minFn ); //Min. value of RGB
int varMax = *std::max_element( rgb, 255, maxFn ); //Max. value of RGB
int dMax = varMax - varMin; //Delta RGB value
int V = varMax;
if ( dMax == 0 ) //This is a gray, no chroma...
{
hsv.h = 0; //HSV results from 0 to 1
hsv.s = 0;
}
else //Chromatic data...
{
hsv.s = dMax / varMax ;
dR = ( ( ( varMax - R ) / 6 ) + ( dMax / 2 ) ) / dMax ;
dG = ( ( ( varMax - G ) / 6 ) + ( dMax / 2 ) ) / dMax ;
dB = ( ( ( varMax - B ) / 6 ) + ( dMax / 2 ) ) / dMax ;
if ( R == varMax ) H = dB - dG ;
else if ( G == varMax ) H = ( 1 / 3 ) + dR - dB ;
else if ( B == varMax ) H = ( 2 / 3 ) + dG - dR ;
if ( H < 0 ) H += 1 ;
if ( H > 1 ) H -= 1 ;
}
}
int main()
{
struct RGB rgb = { 132, 34, 255 };
struct HSV hsb;
rgb2hsv(&rgb, &hsb);
printf("RGB(%u,%u,%u) -> HSB(%f,%f,%f)\n", rgb.r, rgb.g, rgb.b,
hsb.h, hsb.s, hsb.b);
// prints: RGB(132,34,255) -> HSB(266.606354,0.866667,1.000000)
return 0;
}
error C2782: '_FwdIt std::min_element(_FwdIt,_FwdIt,_Pr)' : template parameter '_FwdIt' is ambiguous
-
June 11th, 2014, 08:09 AM
#10
Re: Convert HSB color to RGB and RGB to HSB
Victor Nijegorodov
-
June 11th, 2014, 08:36 AM
#11
Re: Convert HSB color to RGB and RGB to HSB
I quite don't know. It still wants 3 arguments
Code:
// mask1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <algorithm>
struct RGB { int r; int g; int b; } rgb;
struct HSV { float h; float s; float v; } hsv;
template<class ForwardIt>
ForwardIt min_element(ForwardIt first, ForwardIt last)
{
if (first == last) return last;
ForwardIt smallest = first;
++first;
for (; first != last; ++first) {
if (*first < *smallest) {
smallest = first;
}
}
return smallest;
}
// Returns the hue, saturation, and brightness of the color
void rgb2hsv(struct RGB &rgb, struct HSV &hsv)
{
int R,G,B; float H,S,V, dR,dG,dB; // H,S,V and delta components
// struct hsv { float h; float s; float v; };
R = ( rgb.r / 255 ); //RGB from 0 to 255
G = ( rgb.g / 255 );
B = ( rgb.b / 255 );
int varMin = *std::min_element( std::begin(rgb), std::end(rgb) ); //Min. value of RGB
int varMax = *std::max_element( std::begin(rgb), std::end(rgb) ); //Max. value of RGB
int dMax = varMax - varMin; //Delta RGB value
int V = varMax;
if ( dMax == 0 ) //This is a gray, no chroma...
{
hsv.h = 0; //HSV results from 0 to 1
hsv.s = 0;
}
else //Chromatic data...
{
hsv.s = dMax / varMax ;
dR = ( ( ( varMax - R ) / 6 ) + ( dMax / 2 ) ) / dMax ;
dG = ( ( ( varMax - G ) / 6 ) + ( dMax / 2 ) ) / dMax ;
dB = ( ( ( varMax - B ) / 6 ) + ( dMax / 2 ) ) / dMax ;
if ( R == varMax ) H = dB - dG ;
else if ( G == varMax ) H = ( 1 / 3 ) + dR - dB ;
else if ( B == varMax ) H = ( 2 / 3 ) + dG - dR ;
if ( H < 0 ) H += 1 ;
if ( H > 1 ) H -= 1 ;
}
}
int main()
{
struct RGB rgb = { 132, 34, 255 };
struct HSV hsb;
rgb2hsv(&rgb, &hsb);
printf("RGB(%u,%u,%u) -> HSB(%f,%f,%f)\n", rgb.r, rgb.g, rgb.b,
hsb.h, hsb.s, hsb.b);
// prints: RGB(132,34,255) -> HSB(266.606354,0.866667,1.000000)
return 0;
}
'_Ty *std::begin(_Ty (&)[_Size])' : could not deduce template argument for '_Ty (&)[_Size]' from 'RGB'
-
June 11th, 2014, 09:00 AM
#12
Re: Convert HSB color to RGB and RGB to HSB
I'm not sure what you are trying to accomplish by using min_element the way you are but it isn't going to do what you want as rgb has only 1 element of type RGB. Therefore the first and last forward iterator are just &rgb. But as there is only one element, min_element will return straight away as first equals last.
The comment says minimum value of RGB - do you mean the minimum value of r, g, b elements of RGB?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 11th, 2014, 09:06 AM
#13
Re: Convert HSB color to RGB and RGB to HSB
I give up templates. Never mind, here I have simplified solution. You see from the function getMinMax what I am trying to archive.
bool getMinMax(RGB &rgb) ... first error
void rgb2hsv(struct RGB *rgb, struct HSV *hsv) ... second error
Code:
// mask1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
struct RGB { int r; int g; int b; } rgb;
struct HSV { float h; float s; float v; } hsv;
struct STAT { int min; int max; } stat;
bool getMinMax(RGB &rgb){
if (rgb.r < rgb.g )
{
if (rgb.r < rgb.b )
{
stat.min = rgb.r;
if ( rgb.g > rgb.b )
stat.max = rgb.g;
else
stat.max = rgb.b;
}
else
{
stat.min = rgb.b;
if ( rgb.g > rgb.b )
stat.max = rgb.g;
else
stat.max = rgb.b;
}
}
else
if ( rgb.g < rgb.b )
{
stat.min = rgb.g;
stat.max = rgb.b;
}
else
{
stat.min = rgb.b;
stat.max = rgb.g;
}
}
// Returns the hue, saturation, and brightness of the color
void rgb2hsv(struct RGB *rgb, struct HSV *hsv)
{
int R,G,B; float H,S,V, dR,dG,dB; // H,S,V and delta components
// struct hsv { float h; float s; float v; };
R = ( rgb.r / 255 ); //RGB from 0 to 255
G = ( rgb.g / 255 );
B = ( rgb.b / 255 );
getMinMax(rgb); // get min and max values of RGB
int dMax = stat.max - stat.min; //Delta RGB value
V = stat.max;
if ( dMax == 0 ) //This is a gray, no chroma...
{
hsv.h = 0; //HSV results from 0 to 1
hsv.s = 0;
}
else //Chromatic data...
{
hsv.s = dMax / stat.max ;
dR = (float) ( ( ( stat.max - R ) / 6 ) + ( dMax / 2 ) ) / dMax ;
dG = (float) ( ( ( stat.max - G ) / 6 ) + ( dMax / 2 ) ) / dMax ;
dB = (float) ( ( ( stat.max - B ) / 6 ) + ( dMax / 2 ) ) / dMax ;
if ( R == stat.max ) H = dB - dG ;
else if ( G == stat.max ) H = ( 1 / 3 ) + dR - dB ;
else if ( B == stat.max ) H = ( 2 / 3 ) + dG - dR ;
if ( H < 0 ) H += 1 ;
if ( H > 1 ) H -= 1 ;
}
}
int main()
{
struct RGB rgb = { 132, 34, 255 };
struct HSV hsb;
rgb2hsv(&rgb, &hsb);
printf("RGB(%u,%u,%u) -> HSB(%f,%f,%f)\n", rgb.r, rgb.g, rgb.b,
hsb.h, hsb.s, hsb.b);
// prints: RGB(132,34,255) -> HSB(266.606354,0.866667,1.000000)
return 0;
}
Now I got this errors:
mask1.cpp
k1.cpp(52): error C2228: left of '.r' must have class/struct/union
type is 'RGB *'
did you intend to use '->' instead?
k1.cpp(53): error C2228: left of '.g' must have class/struct/union
type is 'RGB *'
did you intend to use '->' instead?
k1.cpp(54): error C2228: left of '.b' must have class/struct/union
type is 'RGB *'
did you intend to use '->' instead?
k1.cpp(55): error C2664: 'getMinMax' : cannot convert parameter 1 from 'RGB *' to 'RGB &'
k1.cpp(57): warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
k1.cpp(61): error C2228: left of '.h' must have class/struct/union
type is 'HSV *'
did you intend to use '->' instead?
k1.cpp(62): error C2228: left of '.s' must have class/struct/union
type is 'HSV *'
did you intend to use '->' instead?
k1.cpp(66): error C2228: left of '.s' must have class/struct/union
type is 'HSV *'
did you intend to use '->' instead?
k1.cpp(87): error C2039: 'b' : is not a member of 'HSV'
k1.cpp(8) : see declaration of 'HSV'
I believe I have wrong defined the pointers. How to correct?
Last edited by crazy boy; June 11th, 2014 at 09:31 AM.
-
June 11th, 2014, 09:19 AM
#14
Re: Convert HSB color to RGB and RGB to HSB
Originally Posted by crazy boy
Now I got this errors:
Code:
mask1.cpp
k1.cpp(52): error C2228: left of '.r' must have class/struct/union
type is 'RGB *'
did you intend to use '->' instead?
Well, the compiler shows you very clear error description and even suggests to correct your code!
So why do you ignore this suggestion?
Or if you do not want to use pointers - change your functions passing the parameters by reference or value.
Victor Nijegorodov
-
June 11th, 2014, 09:24 AM
#15
Re: Convert HSB color to RGB and RGB to HSB
I was used to use . to access members of objects. Structs have always -> to access their members?
Edit:
Great. Now I understand it. So
When I don't declare pointer * so I use dot . to access members but when I declare * operator so I must to use ->.
This message on the other hand says that I did not defined pointer * but I try to access member like a pointer:
type 'RGB' does not have an overloaded member 'operator ->'
The message
left of '.s' must have class/struct/union
type is 'HSV *'
did you intend to use '->' instead?
Says that I use dot instead arrow.
Last edited by crazy boy; June 11th, 2014 at 09:42 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|