简体   繁体   中英

Hexadecimal color failling

Following this answer I wrote some code test.

I tried to put some text with the color '903C39' (which is something like a red), and show it in Web Browser and after paste it in a Rich Text Box. But if I write:

<font color='#903C39'> Blablabla </font>

Appears the text in gray. But if I degrade a little the color to '903C30', then the color 'red' appears:

颜色

Is this normal?

All the code that I tried is:

string html = @"<font color='#903C30' size='1,5' face='Arial'>      903C30</font> <br> <font color='#903C39' size='1,5' face='Arial'>       903C39 </font>";
                    var webBrowser = new WebBrowser();
        webBrowser.CreateControl(); // only if needed
        webBrowser.DocumentText = html;
        while (webBrowser.DocumentText != html)
            Application.DoEvents();
        webBrowser.Document.ExecCommand("SelectAll", false, null);
        webBrowser.Document.ExecCommand("Copy", false, null);
        richTextBox1.Paste(); 

Just having a webBrowser and RichTextBox with the default names.

This is simply a result of how the webbrowser encodes it's HTML into Rich Text.

When it creates the rich text, a default 16 color colortable is inserted into the RTF stream {\\colortbl\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red0\\green255\\blue255;\\red0\\green255\\blue0;\\red255\\green0\\blue255;\\red255\\green0\\blue0;\\red255\\green255\\blue0;\\red255\\green255\\blue255;\\red0\\green0\\blue128;\\red0\\green128\\blue128;\\red0\\green128\\blue0;\\red128\\green0\\blue128;\\red128\\green0\\blue0;\\red128\\green128\\blue0;\\red128\\green128\\blue128;\\red192\\green192\\blue192;}

Any colors used are mapped to the nearest color in this color map. 903C30 happens to be closer to red while 903C39 is closer to the gray.

If you want exact colors, you'll have to create the RTF stream directly without resorting to Copy/paste from WebBrowser as an intermediary.

HTML5不支持font标签,请改用span

<span style='color:#903C30'> Blablabla </span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM