WPCubed Product Support

Check and change char color with cpattr.

Can't figure out why this doesn't work.
It has no effect on the color of the text. I just want to detect all white text in the document and change it to black, leaving all other colors how they exist.
Thanks for any help.

if wprichtext1.PaperColor = clblack then
    begin
      wprichtext1.PaperColor := clwhite;
      wprichtext1.SelectAll;
      progressbar.Maximum := wprichtext1.SelLength;
      progressbar.Update;
      WPRichText1.CPPosition := 0;
      with WPRichtext1.cpattr do
      begin
        beginupdate;
        WPRichtext1.CPPosition := 0;
        repeat
          if WPRichText1.CPAttr.Color = clwhite then wprichtext1.CPAttr.Color := clblack ;
          progressbar.Position := wprichtext1.CPPosition;
          progressbar.Update;
        until wprichtext1.CPMoveNext = FALSE;
        endupdate;
      end;
      wprichtext1.HideSelection; wprichtext1.Refresh;
      wprichtext1.CPPosition := lastpos;
      wprichtext1.Refresh;
    end else
#1 ♥ 0
5.7k Views 2 users read 3 min
support

This is important to know:

CPAttr was a real pointer in WPTools 4 many years ago. At those days it gave access to a record which had an element for several character attributes, such as font, color etc.

Color never was a 4 byte TColor value but an index into a color palette. Use ColorToNr to convert a TColor into such an index value.

Now the character attributes are stored as a 3 byte index into a cache which holds the character attributes.

CPAttr is supposed to make old code working, it is not a pointer anymore.

From I code I see that you want to show the text inverted.

You can do this like this:

if WPRichText1.PaperColor = clBlack then
   begin
      WPRichText1.TextColors[0] := clBlack;
      WPRichText1.Memo.TextColor := clBlack;
      WPRichText1.PaperColor := clWhite;

   end else
   begin
       WPRichText1.TextColors[0] := clWhite;
       WPRichText1.Memo.TextColor := clWhite;
       WPRichText1.PaperColor := clBlack;
   end;
#2 ♥ 0

Thanks, I know I can invert the color of text in the entire document (or change to any color) by just assigning the color,
However, I want to preserve existing colors. That is why I need to go through the entire document attr by character in some manor.
If on word is RED, I want to keep it red, but still change all white words to black.
-Damon

#3 ♥ 0
support

Hi,

Did you try? If a color is read it should remain red since my code only changes the default.

Julian

#4 ♥ 0

Julian, I try the code above, it does keep other text color formatting, but does not change white text at all.
Maybe it is because my default on wprichtext1 the settings I start with are black paper color and white font color?
-damon

#5 ♥ 0
support

Hi,

I understand. With the suggested color switching it is not necessary to use a white font.

to change that as well, You can change WPRichText1.TextColors[13].

The number 13 is the fixed index for white - it should be the same as ColorToNr(clWhite).

But I would rather not start with a white font and change the default font color instead.

Julian

#6 ♥ 0

Thanks Julian, I take your advice and not start with white font and have it working correctly now.

#7 ♥ 0
This category is archived. No new posts are possible.