Solved

Easy answer!

Posted on 1998-08-03
7
198 Views
Last Modified: 2010-08-05
I'm working on a DLL in VC 5 for use by VB 5.  The dll uses the hDC content to change the graphics on a form.  What I need to do is calculate the individual RGB values of a point.  I'm reading the point using GETPIXEL, and setting it using SETPIXEL.  Anyone know how I could calculate the RGB values of a point quickly and easily?
0
Comment
Question by:codewizard
  • 5
  • 2
7 Comments
 
LVL 6

Accepted Solution

by:
thresher_shark earned 20 total points
ID: 1169294
Try the following:
void CalcRGB (char &r, char &g, char &b, char rgb)
{ r = (char) rgb;
  b = (char) >> 8;
  g = (char) >> 16;
}
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1169295
Sheesh, I just realized that I didn't write the function
correctly.  Use the following, and disregard my original answer:

void CalcRGB (BYTE &r, BYTE &g, BYTE &b, COLORREF rgb)
{ r = (BYTE) rgb;
  g = (BYTE) (rgb >> 8);
  b = (BYTE) (rgb >> 16);
}

Don't you love it when you're in a hurry?

You supply the function three variables.  The first three
variables are the ones you want the individual red, green,
and blue intensities to be stored.  The last variable is
the value that you get from the GetPixel function.  The
function prototype looks like the following:

void CalcRGB (BYTE &, BYTE &, BYTE &, COLORREF);

If you have any questions, feel free to ask.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169296
To be on the safe side you might want to use the macros defined by windows for this.  They are
GetRValue(), GetGValue() and GetBValue().  Should yield the same results, but it is usually safest to use the windows provided stuff in case they change things.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 6

Expert Comment

by:thresher_shark
ID: 1169297
You know, when I saw the question, I thought there should be a Windows provided solution, but at the time I couldn't think of it so I improvised :-)

codewizard, if you want to calculate all the variables at once and stick with the Windows functions that nietod mentioned use the following instead:

void CalcRGB (BYTE &r, BYTE &g, BYTE &b, COLORREF rgb)
{ r = GetRValue (rgb);
  g = GetGValue (rgb);
  b = GetBValue (rgb);
}
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1169298
You know, when I saw the question, I thought there should be a Windows provided solution, but at the time I couldn't think of it so I improvised :-)

codewizard, if you want to calculate all the variables at once and stick with the Windows functions that nietod mentioned use the following instead:

void CalcRGB (BYTE &r, BYTE &g, BYTE &b, COLORREF rgb)
{ r = GetRValue (rgb);
  g = GetGValue (rgb);
  b = GetBValue (rgb);
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169299
Then you might not be aware that there is a reverse macro RGB() that creates a COLORREF from three color values.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1169300
Actually I was aware of that one.  Thanks for checking though.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question