HTML如何解析?
HTML如何解析?
这个问题在这里已经有答案了:
参考为什么HTML认为“chucknorris”是一种颜色?
以下分析是否正确?
- 首先,将所有非十六进制字符替换为\'
0
\'。testing ->
0e00000
- 然后,如果它不能被3整除,则附加\'0\'。
0e00000
->0e0000000
- 然后将其分成3个相等的组。
0e0000000
->0e0 000 000
- 然后获取每个组的前2个字符并将它们连接在一起以获取您的颜色代码。
0e0 000 000
->0e0000
#0e0000
接近黑色。
但是,如果您使用此网站并将字体颜色输入为“testing”,它将显示为红色的色调:https://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 colourArray2 = new ArrayList(); 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; }
是的,你是对的,它使用以下解析算法并遵循以下步骤:
首先,删除任何哈希符号,然后将任何非十六进制字符(0-9a-f)替换为0。
例如:#DIXIT 变为 D0000。
对于长度为1-2的情况,使用0进行右侧填充,使其变为3个字符。
例如:"0F" 变为 "0F0","F" 变为 "F00"。
对于长度为3的情况,将每个数字作为红、绿或蓝的值,并在该值前加0。
例如:"0F0" 变为 RGB(0,F,0),该值变为 RGB(00,0F,00)或者000F00。
任何少于4个数字的值都是在这一点上完成的。
对于长度为4或更长的情况,该字段将向右填充0,直到下一个3的倍数为止。这一步对于较长的字段非常重要。
例如:"0F0F" 变为 "0F0F00"
接下来,该字符串被均匀地分为三个部分,从左到右表示红、绿和蓝。
"0F0F00" 表现如预期,变为 RGB(0F,0F,00)。任何6个字符的字符串都在此时完成。
要验证以上内容,请点击此处。
要测试算法,请检查以下样本,你将得到相同的结果:
将执行以下步骤来解析 Dixit:
- 用0替换非十六进制值,例如 D0000。
- 对于长度为4或更长的情况,将该字段向右填充0,直到下一个3的倍数为止。这一步对于较长的字段非常重要。因此,现在 'D0000' 将变为 RGB 形式的 'D0 00 00'。
它实际上正在将其分成RGB值,而不是十六进制颜色值。因此,您不是创建#0e0000
,而是创建RGB(0e0,000,000)
。由于我们知道000
只是0
,因此我们只需要查看其中的0e0
部分。从这里开始,如果有超过2个数字,则需要删除前导的0
,然后从数字中截取左侧的两个数字,这将给您e0
。当您将其从十六进制转换为十进制时,您最终得到e0 = 224
。这给您的是RGB(224,0,0)
或大多数红色的颜色。
更多示例:
eesting => ee00000 => ee0 000 000 => RGB(ee0, 000, 000) => RGB(ee, 00, 00) => RGB(238, 0, 0) eeeting => eee0000 => eee 000 000 => RGB(eee, 000, 000) => RGB(ee, 00, 00) => RGB(238, 0, 0) eeeeing => eeee000 => eee e00 000 => RGB(eee, e00, 000) => RGB(ee, e0, 00) => RGB(238, 224, 0) eefeefeef => eefeefeef => eef eef eef => RGB(eef, eef, eef) => RGB(ee, ee, ee) => RGB(238, 238, 238) teeteetee => 0ee0ee0ee => 0ee 0ee 0ee => RGB(0ee, 0ee, 0ee) => RGB(ee, ee, ee) => RGB(238, 238, 238) 0f0f0f => 0f0f0f => 0f 0f 0f => RGB(0f, 0f, 0f) => RGB(0f, 0f, 0f) => RGB(15, 15, 15) tftftf => 0f0f0f => 0f 0f 0f => RGB(0f, 0f, 0f) => RGB(0f, 0f, 0f) => RGB(15, 15, 15) ttfttfttf => 00f00f00f => 00f 00f 00f => RGB(00f, 00f, 00f) => RGB(0f, 0f, 0f) => RGB(15, 15, 15)