0

I'm working in an application, that uses a rather strange format for it's colours. It's using a variation of ARGB, by using a float to store all the data. The colours themselves are hardcoded into the classes, and are decoded by this operation:

float alpha = (float)(color >> 24 & 255) / 255.0F;
float red = (float)(color >> 16 & 255) / 255.0F;
float blue = (float)(color >> 8 & 255) / 255.0F;
float green = (float)(color & 255) / 255.0F;

The 32 bits of the float are used to extract four 8-Bit Integers by Bit-Shifting them out, and converting them to a float between 0 and 1. This way they are processed further with a graphics library.

I wrote a class to represent colours, since I really don't want to work with all these magical float's. I need a method to reverse the process, but I'm clueless to how this reversal would work. I started by converting my float's to integers between 0 and 1, but that's about as far as I get. How can I take these values and "glue" them together in the same order as the original values?

  public int toARGB() {
        System.out.println(fromARGB(1615855616)); // gives  Color{red=0.3137255, green=0.0, blue=0.0, alpha=0.3764706}
        System.out.println(fromARGB(-2130706433)); // gives  Color{red=1.0, green=1.0, blue=1.0, alpha=0.5019608}

        int closeAlpha = (int) (alpha*255.0F);
        int closeRed = (int) (red*255.0F);
        int closeGreen = (int) (green*255.0F);
        int closeBlue = (int) (blue*255.0F);
 
        //How do I proceed here?
 
        return null;
    }
monamona
  • 1,195
  • 2
  • 17
  • 43

0 Answers0