Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ColorTranslator question ...

48 views
Skip to first unread message

JustMe

unread,
Aug 31, 2003, 6:27:54 PM8/31/03
to
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


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.512 / Virus Database: 309 - Release Date: 8/22/2003


Alex Feinman [MVP]

unread,
Aug 31, 2003, 7:33:38 PM8/31/03
to
The RGB color can be converted to Color using Color.FromArgb(rgbColor) or
Color.FromArgb(r, g, b)
The system color index can be converted using Color.FromSysIcv(nColor) where
nColor is one of the constants defined in SDK as a parameter for GetSysColor
function, such as COLOR_ACTIVECAPTION or COLOR_TEXT etc.

"JustMe" <temi...@sasktel.net> wrote in message
news:vl4tr06...@corp.supernews.com...

JustMe

unread,
Aug 31, 2003, 8:10:29 PM8/31/03
to
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
"Alex Feinman [MVP]" <publi...@alexfeinman.com> wrote in message
news:eY0FPhBc...@TK2MSFTNGP12.phx.gbl...

Alex Feinman [MVP]

unread,
Aug 31, 2003, 8:38:43 PM8/31/03
to
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)

"JustMe" <temi...@sasktel.net> wrote in message

news:vl53ri...@corp.supernews.com...

JustMe

unread,
Aug 31, 2003, 8:58:20 PM8/31/03
to
Hi Alex ...

When I do colortranslator.towin32(color.yellow) I get 65535.

Still puzzled.

Thanks again,
-Terry

"Alex Feinman [MVP]" <publi...@alexfeinman.com> wrote in message

news:u6ummFC...@tk2msftngp13.phx.gbl...

Alex Feinman [MVP]

unread,
Aug 31, 2003, 11:14:54 PM8/31/03
to
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;

"JustMe" <temi...@sasktel.net> wrote in message

news:vl56l33...@corp.supernews.com...

Tim Wilson [MVP]

unread,
Sep 1, 2003, 9:28:07 AM9/1/03
to
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
--
Tim Wilson
Windows Embedded MVP

"JustMe" <temi...@sasktel.net> wrote in message

news:vl56l33...@corp.supernews.com...

JustMe

unread,
Sep 1, 2003, 11:14:25 AM9/1/03
to
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

"Tim Wilson [MVP]" <Tim_W...@Rogers.com> wrote in message
news:ekovMzIc...@TK2MSFTNGP10.phx.gbl...

Version: 6.0.512 / Virus Database: 309 - Release Date: 8/23/2003


Tim Wilson [MVP]

unread,
Sep 1, 2003, 11:57:33 AM9/1/03
to
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)
>> 8), ((color And &HFF0000) >> 16))
End Function

--
Tim Wilson
Windows Embedded MVP

"JustMe" <temi...@sasktel.net> wrote in message

news:vl6oqc6...@corp.supernews.com...

JustMe

unread,
Sep 1, 2003, 1:23:46 PM9/1/03
to
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

"Tim Wilson [MVP]" <Tim_W...@Rogers.com> wrote in message

news:%23Q5GtGK...@TK2MSFTNGP10.phx.gbl...

jaymik...@gmail.com

unread,
Feb 13, 2014, 4:52:06 PM2/13/14
to
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's happening 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
0 new messages