Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9315357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:20:39+00:00 2026-06-19T02:20:39+00:00

I am working on a Java EE application, in which I have a table

  • 0

I am working on a Java EE application, in which I have a table of “items” which will contain some products, and a field to set its color.

The Problem: The user chooses a color from a palette that contains 16 or perhaps 128 colors. I am storing the color as a byte (8-bit color), and I need to be able to convert RGB color/integer to its 8-bit equivalent and vice-versa, e.g.:

White:  0xFF(0b 111 111 11) to -1     or (255,255,255)
Red:    0x10(0b 111 000 00) to -65536 or (255, 0, 0  )

What I have tried so far:

void setColor(Color color){
   short sColor =  (color.getRGB() >> 16) & 0xFF) >> 8
                 | (color.getRGB() >> 8) & 0xFF) >> 8
                 | (color.getRGB() >> 0) & 0xFF) >> 8;
   }

Color getColor(short sColor){
   Color rgb = new Color(
                        /*red:*/  (sColor & 0xF) << 16, 
                        /*gree:*/ (sColor & 0xF) << 8, 
                        /*blue*/  (sColor & 0xF) << 0));
}
/* or */

Color getColor(short sColor){
   Color rgb = new Color((sColor << 8) + sColor));
}

When I loop through color values 0 to 255, I get a single hue variations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:20:41+00:00Added an answer on June 19, 2026 at 2:20 am

    So in the 8 bit color:

    111 111 11
    red grn bl
    

    ?

    With 8 different values for red and green:

    0 (0)
    1 (36)
    2 (72)
    3 (109)
    4 (145)
    5 (182)
    6 (218)
    7 (255)
    

    And 4 different values for blue.

    Try this:

    public static Color fromByte(byte b) {
        int red = (int) Math.round(((b & 0xE0) >>> 5) / 7.0 * 255.0);
        int green = (int) Math.round(((b & 0x1C) >>> 2) / 7.0 * 255.0);
        int blue = (int) Math.round((b & 0x03) / 3.0 * 255.0);
        return new Color(red, green, blue);
    }
    
    public static byte fromColor(Color color) {
        int red = color.getRed();
        int green = color.getGreen();
        int blue = color.getBlue();
    
        return (byte) (((int) Math.round(red / 255.0 * 7.0) << 5) |
                    ((int) Math.round(green / 255.0 * 7.0) << 2) |
                    ((int) Math.round(blue / 255.0 * 3.0)));
    }
    

    Here are the possible colors http://jsfiddle.net/e3TsR/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a calculator application in java swing, which is working properly with mouse
I am working on a Java application that will use some Hibernate (annotated by
I am working on a Java application which does some reasoning on basic terms
I am working under java application which uses XSLT transformation. I have a lot
I'm currently working on a Java web application which will take data submitted by
I have already developed a Java application which is working well in Windows XP,
I'm currently working on a Java application which should have the capability to use
I have a crawler Java application which is supposed to connect to some HTTP
I'm working on a Java EE (which I'm fairly new to) web application (JSF,
I'm working on a Swing application (currently running on Java 1.6 update 11) which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.