Search code examples
javacolors8-bit

How to get 8 bit Colors?


So ,at this moment, I got a method which generates 3 random integers, from 0 to 255.

I use the 3 integers for colors.Red, Green ,Blue.

So,at this moment, if I want to set the color of something, with the generated colors, I use this:

 Color.argb(255, r, g, b); //255 =max intensity and after ,there are red green blue

What I need to do ,is to convert the 3 integers ,or eventually the intensity value too, to a 8bit integer.

Any sort of documentation or guidance is highly appreciated!

If more information is needed, I will comment or modify the body of the question.


Solution

  • You can encode the colors the following way:

    Bits    0### 1### 2### 3### 4### 5### 6### 7###
            Alpha---- Red------ Green---- Blue-----
    

    Note that you'll lose a whole lot of information about the color (but I think that's the thing you want to get).

    Things you'll need to do in order to encode:

    • Change the range of colors (0-255 to 0-3)
    • Shift the colors properly and add them to get the 8bit value.

    Here's some example code:

    import java.awt.Color;
    
    abstract class EightBit {
      public static int fromColor(Color c) {
        return ((c.getAlpha() >> 6) << 6)
             + ((c.getRed()   >> 6) << 4)
             + ((c.getGreen() >> 6) << 2)
             +  (c.getBlue()  >> 6);
      }
      public static Color toColor(int i) {
        return new Color(((i >> 4) % 4) * 64,
                         ((i >> 2) % 4) * 64,
                          (i       % 4) * 64,
                          (i >> 6)      * 64);
      }
    }
    

    Explanation

    Encoding

    Let's start with an example color: new Color(200, 59, 148, 72). Now we'll convert that into an integer. The binary representation of the color is:

    Alpha 200 -- 11001000
    Red    59 -- 00111011
    Green 148 -- 10010100
    Blue   72 -- 01001000
    

    Now, we shift them to the right by 6 bits (so we get the first 2 bits):

    Alpha 3 -- 11
    Red   0 -- 00
    Green 2 -- 10
    Blue  1 -- 01
    

    Now we put them together:

    Bits  [ 1 ][ 1 ][ 0 ][ 0 ][ 1 ][ 0 ][ 0 ][ 1 ] -- 209
           ALPHA---  RED-----  GREEN---  BLUE----
    

    It's 209. See?

    Decoding

    So we're back at our 8bit number: 209. We want to decode it. First, we need to get the 2-bit colors back by shifting them to the right, and modulo 4:

    Bits  [ 1 ][ 1 ][ 0 ][ 0 ][ 1 ][ 0 ][ 0 ][ 1 ]
          \_shift_by_6_bits_____________[ 1 ][ 1 ] -- 3 (Alpha)
                    \_by_4_bits_________[ 0 ][ 0 ] -- 0 (Red)
                              \_by_2____[ 1 ][ 0 ] -- 2 (Green)
                       shift by 0 bits: [ 0 ][ 1 ] -- 1 (Blue)
    

    Now we multiply them by 64:

    3 * 64 = 192 (Alpha)
    0 * 64 =   0 (Red)
    2 * 64 = 128 (Green)
    1 * 64 =  64 (Blue)
    

    And put them back into a Color object. As you can see, the colors are different: some information about the color was lost in the process. This is called lossy compression.