我下面有Java代码演示,这给了我一些问题。
这是示例:
public class MyTest
{
public static void main(String as[])
{
String ColorHex="#4EB3A2";
int RedColor = Integer.parseInt(ColorHex.substring(1,3), 16);
int GreenColor = Integer.parseInt(ColorHex.substring(3,5), 16);
int BlueColor = Integer.parseInt(ColorHex.substring(5,7), 16);
int finalColorValue = 65536 * RedColor + 256*GreenColor + BlueColor;
int ColorDecimal=finalColorValue;
int red = ColorDecimal % 256;
ColorDecimal = ( ColorDecimal - red ) / 256;
int green = ColorDecimal % 256;
ColorDecimal = ( ColorDecimal - green ) / 256;
int blue = ColorDecimal % 256;
ColorDecimal = ( ColorDecimal - blue ) / 256;
String hex = String.format("#%02x%02x%02x", red, green, blue);
System.out.println("hex"+hex);
}
}
这里hex
应该是,#4EB3A2
但它正在返回#a2b34e
。我在这里做错了什么?
以下解决了您的问题:
String ColorHex="#4EB3A2";
int RedColor = Integer.parseInt(ColorHex.substring(1,3), 16);
int GreenColor = Integer.parseInt(ColorHex.substring(3,5), 16);
int BlueColor = Integer.parseInt(ColorHex.substring(5,7), 16);
int finalColorValue = 65536 * RedColor + 256*GreenColor + BlueColor;
int ColorDecimal=finalColorValue;
// Blue extracted first.
int blue = ColorDecimal % 256;
ColorDecimal = (ColorDecimal - blue ) / 256;
int green = ColorDecimal % 256;
ColorDecimal = (ColorDecimal - green ) / 256;
int red = ColorDecimal % 256;
ColorDecimal = (ColorDecimal - red ) / 256;
String hex = String.format("#%02x%02x%02x", red, green, blue);
System.out.println("hex" + hex);
说明:
蓝色占据ColorDecimal的最低字节,因此应首先从中提取它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句