HTML如何解析<字体颜色=“测试”>?
关于为什么HTML认为“chucknorris”是一种颜色?
以下分析是否正确?
-
首先,所有非十六进制字符都替换为“”。
0
测试->
0e00000
-
然后,如果它不能被 3 整除,则将 '0' 附加到它。
0e00000
->0e0000000
-
然后分成3个相等的组。
0e0000000
->0e0 000 000
-
然后获取每个组的前2个字符,并将它们连接在一起以获取颜色代码。
0e0 000 000
->0e0000
#0e0000
接近黑色。
但是,如果您使用此站点并输入字体颜色作为“测试”,它将显示为红色阴影:http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_font_color
我错过了什么吗?
在答案之后附加:
我正在编写一个Android应用程序,需要我将字体颜色= “”解析为颜色代码。我把我在这里拼凑起来的算法放在一起,以备将来参考:
public String getColourCode(String nonStandardColour) {
String rtnVal = "#000000";
// first replace all non-hex characters
String converted = nonStandardColour.toLowerCase().replaceAll("[g-z]", "0");
System.out.println(nonStandardColour + " is now " + converted);
System.out.println("Length: " + converted.length());
if (converted.length() <= 3) {
// append "0"s if length != 3
while (converted.length() !=3) {
converted = converted + "0";
}
System.out.println("Converted colour is now " + converted);
// Length is 3, so split into 3 characters and prepend 0 to each
String[] colourArray = new String[3];
colourArray[0] = "0" + convertedOpNickColour.substring(0, 1);
colourArray[1] = "0" + convertedOpNickColour.substring(1, 2);
colourArray[2] = "0" + convertedOpNickColour.substring(2, 3);
rtnVal = "#" + Integer.toHexString(Color.rgb(
Integer.parseInt(colourArray[0], 16),
Integer.parseInt(colourArray[1], 16),
Integer.parseInt(colourArray[2], 16)));
}
else { // converted.length() is >= 4
System.out.println("Appending 0s until divisible by 3");
while(converted.length() % 3 != 0) {
converted = converted + "0";
}
System.out.println("Converted colour is now " + converted);
// divide into 3 equal groups
List<String> colourArray2 = new ArrayList<String>();
int index = 0;
while (index<converted.length()) {
colourArray2.add(converted.substring(
index, Math.min(index(converted.length()/3),converted.length())));
index+=(converted.length()/3);
}
System.out.printf("The 3 groups are:");
System.out.printf(colourArray2.get(0));
System.out.printf(colourArray2.get(1));
System.out.printf(colourArray2.get(2));
// if the groups are e.g. 0f0 0f0 0f0
if (rgbColour.get(0).length() >=3 ) {
rtnVal = Integer.toHexString(Color.rgb(
Integer.parseInt(colourArray2.get(0).substring(0,2), 16),
Integer.parseInt(colourArray2.get(1).substring(0,2), 16),
Integer.parseInt(colourArray2.get(2).substring(0,2), 16)));
// remove alpha
System.out.println("rtnVal is #" + rtnVal.substring(2));
return "#" + rtnVal.substring(2);
}
// groups are e.g. 0f 0f 0f
else {
rtnVal = Integer.toHexString(Color.rgb(
Integer.parseInt(colourArray2.get(0), 16),
Integer.parseInt(colourArray2.get(1), 16),
Integer.parseInt(colourArray2.get(2), 16)));
System.out.println("rtnVal is #" + rtnVal.substring(2));
return "#" + rtnVal.substring(2);
}
}
return rtnVal;
}