BGR Color to Java Color

A

Andrew Arace

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
 
M

Marco Schmidt

comp.lang.java removed, it's not a valid group

Andrew Arace:

[...]
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.

Decoding those int values is described in several places, my version
at
<http://www.geocities.com/marcoschmidt.geo/java-image-faq.html#rgba>.
It also shows how to get from single 0..255 sample values to RGB(A).

However, I don't understand why you use the float, float, float
constructor of Color when there is an int, int, int constructor which
seems to be more straight-forward.

Regards,
Marco
 
P

Phil...

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?
 
T

Tim Tyler

: 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.

Note:

``Color(float r, float g, float b)

Creates an opaque sRGB color with the specified red, green, and
blue values in the range (0.0 - 1.0).''

- http://java.sun.com/j2se/1.4.1/docs/api/java/awt/Color.html

You appear to be feeding in values between 0 and 255.

That is unlikely to produce the desired effect.
 
A

Andrew Arace

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
 
S

Stephen Gilbert

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
 
A

Andrew Arace

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.
 
S

Stephen Gilbert

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.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top