Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: RGB to Hex

  1. #1
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default RGB to Hex

    I am trying to figure out how to get the hexadecimal value from a RGB.

    This is a snippet of my code:

    //where a is the user inputed string of a color, i.e. "red"
    //and where hex is a linkedHashSet used to store the hexadecimal value of the inputed color
     
    final Field f = Color.class.getField(a);
    hex.add(Integer.toHexString((Color)f.get(null) & 0x00ffffff));

    it says that the operater & is undefined for the argument type(s) Color, int

    any insight?
    thanks


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: RGB to Hex

    Because you're using the bit operator on a Color object.
    invoke Color.getRGB() first.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: RGB to Hex

    how to get the hexadecimal value from a RGB.
    Can you show an example of what you want to do?
    Give a sample input and output

  4. #4
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: RGB to Hex

    Literally I want the user to enter a color like red. Since red is a java recognized color I want the program to convert the string into a color and then find the hexadecimal of that color so I can put it into a Jradiobutton to change the background of a gui with it.

    so...
    screen pops up and user enters "red"
    then screen closes and a new screen pops up with a Jradiobutton with the hexadecimal of the entered color
    and when pressed, the screen turns to that color, in this case "red"

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: RGB to Hex

    To get from a String value of the name of a color to a String of its RGB value in hex, why not use a hash table with the key the name of the color and the value either an Integer with its RGB value or a Color object from which you can get its RGB value. Use the toHexString method to get the hexString.

  6. #6
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: RGB to Hex

    sounds like it might work. how would I implement it?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: RGB to Hex

    You'd write code to load the hashtable.
    Then use the hashtable methods to get the color value using the String the user entered.