By "color" we usually mean 24-bit RGB color: 1 byte (8 bits) for red, green, blue. That is, every channel has value from 0-255, or 0x00 to 0xff in hexadecimal display.
White color is all channels at full: #FFFFFF, black is all channels turned off: #000000. Obviously, lighter color means higher values in channels, darker color means lower values in channels.
How exactly you choose your algorithm is up to you, simple one would be:
//pseudo-code
if (red + green + blue <= (0xff * 3) / 2) //half-down, half-up
fontcolor = white;
else
fontcolor = black;
Edit: asker asks for more complete example, so he/she can have better start, so here it is:
public static void main(String[] args) throws IOException {
String value =
// new Scanner(System.in).nextLine(); //from input
"#112233"; //from constant
int red = Integer.parseInt(value.substring(1, 1 + 2), 16);
int green = Integer.parseInt(value.substring(3, 3 + 2), 16);
int blue = Integer.parseInt(value.substring(5, 5 + 2), 16);
System.out.println("red = " + Integer.toHexString(red)
+ ", green = " + Integer.toHexString(green)
+ ", blue = " + Integer.toHexString(blue));
if (red + green + blue <= 0xff * 3 / 2)
System.out.println("using white color #ffffff");
else
System.out.println("using black color #000000");
String colorBackToString = "#" + Integer.toHexString(red) +
Integer.toHexString(green) +
Integer.toHexString(blue);
System.out.println("color was " + colorBackToString);
}
It produces output:
red = 11, green = 22, blue = 33
using white color #ffffff
color was #112233
And shows technique of splitting color in format #aabbcc into rgb channels, joining them later (if needed), etc.