ColorTranslator question ...

J

JustMe

Hi:

I've figured out how to translate a color to a win32 color, but I can't
figure out how to translate a win32 color back to a color.

Can anyone help me out with this one?

Thanks.
--Terry
 
J

JustMe

Hi Alex ...

Thanks for your reply. I'm still stumped though (must be the heat).

Anyway, let's say I start with color.yellow. The Win32 value for yellow is
65535.

Now that I have the win32 value of 65535, I need to translate that back
to -256 (which is the color.yellow.toargb value).

This is where I'm stumped. How can I translate the -256 value back to 65535
(or any other color values) in the CF?

Thanks,
Terry
 
A

Alex Feinman [MVP]

ToArgb returns RGB +Alpha. The top 8 bit are alpha value which is 0xff for
the opaque colors. Thus Color.Yellow.ToArgb() returns 0xff000000 // Alpha =
0xff
+ 0xff0000 // full Red
+ 0x00ff00 // full Green
+ 0x000000 // no Blue
= 0xffffff00 = -256

I'm not sure why do you say the Win32 value for yellow is 65535 (0x00ffff).
This would be Cyan (G+B)
 
A

Alex Feinman [MVP]

I stand corrected. The COLORREF value (what the ColorTranslator deems to be
Win32 representation and what is used by various Win32 API functions has the
following layout:
00BBGGRR. E.g. for Blue we get 0x00ff0000

The ARGB value is AARRGGBB. ARGB for Blue is 0xff0000ff

Given this in order to convert Win32 color (COLORREF value) and back, you
need to use the following:

int winColor = -255;
Color clr = Color.FromArgb( winColor & 0xff, (winColor >> 8) & 0xff,
(winColor >> 16) & 0xff);
winColor = clr.R + clr.G << 8 + clr.B << 16;
 
T

Tim Wilson [MVP]

I just saw this thread and though that I would post some code into it. I
don't know if this is exactly what you are looking for, but here are two
methods - one that takes a Color and returns a COLORREF, and the other that
takes a COLORREF and returns the Color.

private uint RGB(Color color)
{
// Format the value of color - 0x00bbggrr
return ((uint) (((uint) (color.R) | ((uint) (color.G) << 8)) | (((uint)
(color.B)) << 16)));
}

private Color UnRGB(int color)
{
return Color.FromArgb(color & 0xFF, (color & 0xFF00) >> 8, (color &
0xFF0000) >> 16);
}

HTH
 
J

JustMe

Hi Tim ...

I don't suppose you'd be able to translate this into vb.net could you? It's
exactly what I'm looking for, but I'm a bit lost when it comes to
hexadecimal stuff.

Thanks
--Terry
 
T

Tim Wilson [MVP]

This should work:

Private Function RGB(ByVal color As Color) As Integer
' Format the value of color - 0x00bbggrr
Return Convert.ToInt32((Convert.ToInt32(color.R)) Or
(Convert.ToInt32(color.G) << 8) Or (Convert.ToInt32(color.B) << 16))
End Function

Private Function UnRGB(ByVal color As Integer) As Color
Return System.Drawing.Color.FromArgb((color And &HFF), ((color And &HFF00)End Function
 
J

JustMe

Thank you Tim! Thank you Alex!

Some days I don't know what I'd do without all you MVP's!!!

Have a great day!
--Terry
 
J

jaymikejames

Hi, I have what i think is a simple color translation question..

I'm converting Java to Javascript, and ran across this code in a for loop:

int r1 = l1 >> 16 & 0xff;
int g1 = l1 >> 8 & 0xff;
int b1 = l1 & 0xff;

int r2 = l2 >> 16 & 0xff;
int g2 = l2 >> 8 & 0xff;
int b2 = l2 & 0xff;

int r = r1 * r2 << 8 & 0xff0000;
int g = g1 * g2 & 0xff00;
int b = b1 * b2 >> 8;

I'm looking to mimic this logic in javascript, however, I'm not sure what'shappening when r1 * r2 << 8 & 0xff0000 is computed and set to r. I think it's bit shifting the r2 value left and then multiplying it by r1 to some how cancel out some values?? Thanks for any help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top