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.
You can encode the colors the following way:
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:
0-255
to0-3
)Here's some example code:
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:Now, we shift them to the right by 6 bits (so we get the first 2 bits):
Now we put them together:
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:Now we multiply them by 64:
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.