java - HTML如何解析<字体颜色="testing">?

标签 java html fonts colors

<分区>

引用Why does HTML think “chucknorris” is a color?

下列分析是否正确?

  1. 首先,所有非十六进制字符都替换为“0”。

    测试 -> 0e00000

  2. 如果它不能被 3 整除,则在其后附加“0”。

    0e00000 -> 0e0000000

  3. 然后分成 3 个相等的组。

    0e0000000 -> 0e0 000 000

  4. 然后获取每组的前 2 个字符并将它们连接在一起以获得您的颜色代码。

    0e0 000 000 -> 0e0000

#0e0000 接近黑色。

但是如果你使用这个网站并输入字体颜色为“testing”,它会显示为红色阴影:http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_font_color

有什么我想念的吗?

附加在答案之后:

我正在编写一个 Android 应用程序,它需要我将 font color = ""解析为颜色代码。我把我拼凑的算法放在这里以供将来引用:

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;
}

最佳答案

它实际做的是将其拆分为 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)

关于java - HTML如何解析<字体颜色="testing">?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20918112/

相关文章:

ios - OpenType字体无法在iOS上正确呈现

html - 谷歌浏览器字体优化

java - 读取 blob 字段时出现 "java.lang.OutOfMemoryError: Java heap space"

java.lang.ClassNotFoundException : com. game.mrnom.MrNomGame 加载器 dalvik.system.PathClassLoader [/data/app/com.game.mrnom-1.apk]

html - 边距和边框的问题

html - 如何让进度条从右向左过渡,而不是从左向右过渡?

python - 在 Tkinter 中指定字体?

java - 使用 apache commons 邮件重新发送 MultiPartEmail

java - 如何在 Java 中比较字符串?

html - 如何使 div 中的图像响应?