Convert HSB color to RGB and RGB to HSB
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27

Thread: Convert HSB color to RGB and RGB to HSB

  1. #1
    Join Date
    May 2014
    Posts
    149

    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?

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,380

    Re: Convert HSB color to RGB and RGB to HSB

    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    May 2014
    Posts
    149

    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

  4. #4
    Join Date
    May 2014
    Posts
    149

    Re: Convert HSB color to RGB and RGB to HSB

    I found I cannot use AMP because I have Win XP. Any other solution?

  5. #5
    Join Date
    May 2014
    Posts
    149

    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 ;

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,380

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Convert HSB color to RGB and RGB to HSB

    Quote 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

  8. #8
    Join Date
    May 2014
    Posts
    149

    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/

  9. #9
    Join Date
    May 2014
    Posts
    149

    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

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Convert HSB color to RGB and RGB to HSB

    Victor Nijegorodov

  11. #11
    Join Date
    May 2014
    Posts
    149

    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'

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,380

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #13
    Join Date
    May 2014
    Posts
    149

    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.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Convert HSB color to RGB and RGB to HSB

    Quote Originally Posted by crazy boy View Post
    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

  15. #15
    Join Date
    May 2014
    Posts
    149

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center