469,304 Members | 2,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

BGR Color to Java Color

Took me a while to come up with this (even though it looks simple) it
will convert a BGR color int (or long) like used in VB to a java Color
(java.awt.Color)

examples of this are:
colornum = 16777215; //pure white
colornum = 255; //pure red
colornum = 65280; //pure green
colornum = 16711680; //pure blue

public Color BGRColorToJavaColor(int BGRColorNumber) {
//color codes as a ing in form "BGR"
return new Color((float)(BGRColorNumber & 0xFF),
(float)((BGRColorNumber >> 8) & 0xFF),
(float)((BGRColorNumber >> 16) & 0xFF));
}

Just putting this here to help people. my google groups search didn't
come up with anything like it, so i'm just trying to change that.

comments to my email please ;)

-Drew
Jul 17 '05 #1
5 14336
I don't understand the reason you convert to float.
You are doing bit operations that assume you have integers,
so why not use the "Color(int r, int g, int b)" constructor?

"Andrew Arace" <an**********@hotmail.com> wrote in message
news:11**************************@posting.google.c om...
Took me a while to come up with this (even though it looks simple) it
will convert a BGR color int (or long) like used in VB to a java Color
(java.awt.Color)

examples of this are:
colornum = 16777215; //pure white
colornum = 255; //pure red
colornum = 65280; //pure green
colornum = 16711680; //pure blue

public Color BGRColorToJavaColor(int BGRColorNumber) {
//color codes as a ing in form "BGR"
return new Color((float)(BGRColorNumber & 0xFF),
(float)((BGRColorNumber >> 8) & 0xFF),
(float)((BGRColorNumber >> 16) & 0xFF));
}

Just putting this here to help people. my google groups search didn't
come up with anything like it, so i'm just trying to change that.

comments to my email please ;)

-Drew

Jul 17 '05 #2
an**********@hotmail.com (Andrew Arace) wrote in message news:<11**************************@posting.google. com>...
Took me a while to come up with this (even though it looks simple) it
will convert a BGR color int (or long) like used in VB to a java Color
(java.awt.Color)

examples of this are:
colornum = 16777215; //pure white
colornum = 255; //pure red
colornum = 65280; //pure green
colornum = 16711680; //pure blue

public Color BGRColorToJavaColor(int BGRColorNumber) {
//color codes as a ing in form "BGR"
return new Color((float)(BGRColorNumber & 0xFF),
(float)((BGRColorNumber >> 8) & 0xFF),
(float)((BGRColorNumber >> 16) & 0xFF));
}

Just putting this here to help people. my google groups search didn't
come up with anything like it, so i'm just trying to change that.

comments to my email please ;)

-Drew

SORRY SORRY, messed that post up: turns out my 'tests' i did were on
black, and it worked fine...need to get rid of that (float) casting

public Color BGRColorToJavaColor(int BGRColorNumber) {
//color codes as a int in form "BGR"
return new Color((BGRColorNumber & 0xFF),
((BGRColorNumber >> 8) & 0xFF),
((BGRColorNumber >> 16) & 0xFF));
}

That's right now. Really sorry about that, I felt like an idiot after
i posted it, and 5 seconds later realized I had posted the wrong code.

-Andrew
Jul 17 '05 #3
an**********@hotmail.com (Andrew Arace) wrote in message news:<11**************************@posting.google. com>...
an**********@hotmail.com (Andrew Arace) wrote in message news:<11**************************@posting.google. com>...
Took me a while to come up with this (even though it looks simple) it
will convert a BGR color int (or long) like used in VB to a java Color
(java.awt.Color)

examples of this are:
colornum = 16777215; //pure white
colornum = 255; //pure red
colornum = 65280; //pure green
colornum = 16711680; //pure blue


Isn't there already a Color constructor that does this?
Color red = new Color(255); // looks red to me
Color blue = new Color(16711680); // looks blue too
Jul 17 '05 #4
sg******@occ.cccd.edu (Stephen Gilbert) wrote in message news:<56*************************@posting.google.c om>...
an**********@hotmail.com (Andrew Arace) wrote in message news:<11**************************@posting.google. com>...
an**********@hotmail.com (Andrew Arace) wrote in message news:<11**************************@posting.google. com>...
Took me a while to come up with this (even though it looks simple) it
will convert a BGR color int (or long) like used in VB to a java Color
(java.awt.Color)

examples of this are:
colornum = 16777215; //pure white
colornum = 255; //pure red
colornum = 65280; //pure green
colornum = 16711680; //pure blue


Isn't there already a Color constructor that does this?
Color red = new Color(255); // looks red to me
Color blue = new Color(16711680); // looks blue too

hmm...i don't think so. i'll check out your examples there, but the
Color(int) constructor says "int rgb" where the color i am passing in,
the way VB represents it, its BGR (reversed endian)
....i hope i didn't spend that time for nothing.
Jul 17 '05 #5
an**********@hotmail.com (Andrew Arace) wrote in message news:<11**************************@posting.google. com>...
hmm...i don't think so. i'll check out your examples there, but the
Color(int) constructor says "int rgb" where the color i am passing in,
the way VB represents it, its BGR (reversed endian)
...i hope i didn't spend that time for nothing.


You're right. I wrote a test program and looked at it, but
forgot which part I'd made red and blue. The good news is
that your time wasn't wasted.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michael | last post: by
reply views Thread by Ravi Tallury | last post: by
1 post views Thread by ptaz | last post: by
reply views Thread by Markus Wollny | last post: by
5 posts views Thread by TZESENG | last post: by
12 posts views Thread by Mark Fink | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.