You can encode colors as follows:
Bits 0 2 4 6
Alpha---- Red------ Green---- Blue-----
, ( , , ).
:
:
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);
}
}
: new Color(200, 59, 148, 72). . :
Alpha 200 -- 11001000
Red 59 -- 00111011
Green 148 -- 10010100
Blue 72 -- 01001000
6 ( 2 ):
Alpha 3 -- 11
Red 0 -- 00
Green 2 -- 10
Blue 1 -- 01
:
Bits [ 1 ][ 1 ][ 0 ][ 0 ][ 1 ][ 0 ][ 0 ][ 1 ] -- 209
ALPHA--- RED----- GREEN--- BLUE----
209. ?
, 8- : 209. . -, 2- , 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)
64:
3 * 64 = 192 (Alpha)
0 * 64 = 0 (Red)
2 * 64 = 128 (Green)
1 * 64 = 64 (Blue)
Color. , : . .