BGR Color to Java Color

Discussion in 'Java' started by Andrew Arace, Sep 13, 2003.

  1. Andrew Arace

    Andrew Arace Guest

    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
     
    Andrew Arace, Sep 13, 2003
    #1
    1. Advertising

  2. 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
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Sep 13, 2003
    #2
    1. Advertising

  3. Andrew Arace

    Phil... Guest

    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" <> wrote in message
    news:...
    > 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
     
    Phil..., Sep 14, 2003
    #3
  4. Andrew Arace

    Tim Tyler Guest

    Andrew Arace <> wrote:

    : 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.
    --
    __________
    |im |yler http://timtyler.org/
     
    Tim Tyler, Sep 14, 2003
    #4
  5. Andrew Arace

    Andrew Arace Guest

    (Andrew Arace) wrote in message news:<>...
    > 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
     
    Andrew Arace, Sep 14, 2003
    #5
  6. (Andrew Arace) wrote in message news:<>...
    > (Andrew Arace) wrote in message news:<>...
    > > 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
     
    Stephen Gilbert, Sep 15, 2003
    #6
  7. Andrew Arace

    Andrew Arace Guest

    (Stephen Gilbert) wrote in message news:<>...
    > (Andrew Arace) wrote in message news:<>...
    > > (Andrew Arace) wrote in message news:<>...
    > > > 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.
     
    Andrew Arace, Sep 15, 2003
    #7
  8. (Andrew Arace) wrote in message news:<>...
    > 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.
     
    Stephen Gilbert, Sep 16, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. moondaddy
    Replies:
    3
    Views:
    38,331
    moondaddy
    Apr 28, 2004
  2. PJ6
    Replies:
    4
    Views:
    4,897
  3. Niels Dybdahl

    Color.white vs. Color.WHITE

    Niels Dybdahl, Oct 6, 2004, in forum: Java
    Replies:
    3
    Views:
    443
    Chris Smith
    Oct 6, 2004
  4. Matt Adams
    Replies:
    0
    Views:
    892
    Matt Adams
    Aug 26, 2003
  5. Kamaljeet Saini
    Replies:
    0
    Views:
    427
    Kamaljeet Saini
    Feb 13, 2009
Loading...

Share This Page