0

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:

colors

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.

Community
  • 1
  • 1
Nimrrod
  • 89
  • 1
  • 1
  • 7
  • is this c# or default html css – lordkain Aug 04 '16 at 07:12
  • c#, sorry I will edit it – Nimrrod Aug 04 '16 at 07:13
  • 1
    Who knows? You're using non-standard HTML. Maybe there's a fallback to web-safe colours when you use such horribly outdated HTML? It would certainly make sense. Have you considered using standard HTML, or just using formatted text in the rich text box (RTF) directly? – Luaan Aug 04 '16 at 07:22
  • It doesn't looks like RGB if changing B (and why B, should be R) by so little makes a difference, What is it? If it's something like [HSL](https://en.wikipedia.org/wiki/Web_colors#HTML_color_names)? Then maybe, because changing saturation will make color visible. – Sinatr Aug 04 '16 at 07:22
  • Before going around and accepting the answer, read again what @Luaan says. This is a horribly fragile and convoluted way to apply markup to a RichTextBox. Also the `while` loop made me die inside a little. – CodeCaster Aug 04 '16 at 07:26

2 Answers2

1

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.

oreubens
  • 329
  • 1
  • 9
0

The font tag isn't supported by HTML5, use a span instead.

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