Solved

Easy answer!

Posted on 1998-08-03
7
199 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

820 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