?
Solved

Easy answer!

Posted on 1998-08-03
7
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 6

Accepted Solution

by:
thresher_shark earned 80 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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