Solved

Easy answer!

Posted on 1998-08-03
7
196 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
How do I open an Access Data base with a password set using C++ 2 109
Path to  STL Map header file 1 41
How can i compile this github project?? 2 74
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now