TAdvRichEditorBase.GetContentAsHTML: Font-Color Problem in Emails because of font-color-attributes in generated html

Hello,
I've got another issue that I probably already have solved, but this one requires a far more thorough explanation:

I have built an EMail-Client in an application that is based on the demo "AdvRichEditor->MailClient", meaning that it uses an TAdvRichEditor + FormatToolBar to generate the HTML-Body for an email.

While testing I encountered an interesting phenomenon: Depending on the application -VCL-style and the style of the email-client (e.g. Thunderbird), the text of the received email would be invisible, either because of white text on white background or dark text on dark background.
In my case my application had a dark style with white text and the email-client (Thunderbird) a white background, which resulted in white font color on white background.

When you look into the html that is generated by the TAdvRichEditor, the problem is obvious: Every text-element is surrounded by a font-color-attribute, which forces the email-client to use THIS font color instead of the default font color of its own style, which would be correct.

Therefore I looked into the function TAdvRichEditorBase.GetContentAsHTML, where I found two issues:

  1. The variable "didclr" is always false, which means that the following If-clause is always true (except in the case of an URL of course):
if (not didclr or (clr <> GetDefaultFontColor)) and (el.URL = '') then
        begin
          didclr := false;
          fontattr := AppendAttr(fontattr, 'color', ColorToHtml(clr));
          isfntclr := true;
        end;

This is obviously responsible for every text-element having a font-color-attribute.
Removing "didclr" or the "nots" before it solves this issue.

  1. Even with the aforementioned correction there is still an issue when you are using are VCL-Style in which the default font color isn't "clBlack", because quite early in TAdvRichEditorBase.GetContentAsHTML there is the following line:
SetFontColor(clBlack);

Regardless of the style or manual changes to the TAdvRichEditor.Font.Color, this line hardcodes it to "clBlack".
Now let us assume that my VCL-style is a dark theme with the TAdvRichEditor having a dark background & the default font color being "clwhite" and take a look at the very same code snippet that has received the aforementioned correction:

if ({not didclr or }(clr <> GetDefaultFontColor)) and (el.URL = '') then
        begin
          didclr := false;
          fontattr := AppendAttr(fontattr, 'color', ColorToHtml(clr));
          isfntclr := true;
        end;

Once even a single text element has been colored in a different color as for example "clRed", every following text element will receive a font-color-attribute since "GetDefaultFontColor" is de facto hardcoded to "clBlack" whereas the correct default font color in this case would be "clWhite".
In this example if the text in the TAdvRichEditor were "Test1 Test2 Test3" and the color of "Test2" were to be changed to clRed, then in the resulting html "Test1" would not have a font-color-attribute, "Test2" would have one setting its color to red and "Test3" would incorrectly have also one setting its color to white.

Removing "SetFontColor(clBlack);", whose purpose I don't understand, solves this issue and the generated HTML, when given to an email, will be displayed correctly, regardless of VCL-style and email-client.

I strongly assume those two changes to be correct, at least in the context of generating an html for an email, but if I should have overlooked something I am open for correction.

Thanks for your feedback. We investigated this deeper. You're correct that the check with didclr does not have an effect. We removed it.
With respect to SetFontColor(), this is because by default the exported HTML is for a HTML page with white background color and to make the default text at least readable on this HTML page regardless of the VCL style applied in the application.
I retested the case you mentioned and for test1test2test3 with test2 set to red color text, I get the resulting HTML:

<HTML><BODY style='font-family:"Segoe UI";font-style:normal;font-weight:normal;font-size:10pt'>test1<FONT color="#FF0000">test2</FONT>test3</BODY></HTML>

The second part of the described problem depends on having a VCL style whose standard text color is not clBlack, but for test purposes it should probably suffice to manually change the color sheme of the AdvRichEditor.
In my case it was a dark theme, in which the AdvRichEditor has a dark backgroud and white text color.

The problem occurs here:

clr := (el as TTextElement).TextColor;

 if (clr <> GetDefaultFontColor) and (el.URL = '') then
 begin
 didclr := false;
 fontattr := AppendAttr(fontattr, 'color', ColorToHtml(clr));
 isfntclr := true;
 end;

"SetFontColor(clBlack)" causes "GetDefaultFontColor" to always return "clBlack". However, due to the VCL style, the default font color of the AdvRichEditor really is "clWhite".
That means every unformated TextElement, whose text color by default is "clWhite", will trigger "clr <> GetDefaultFontColor", whicj shouldn't be the case.

We applied a change and tested this with a dark style VCL style (with white font) and also under these circumstances it behaves correct.
These improvements will be included in the next release.