• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

In VBA, how to change the font color in function of the background color

In a VBA procedure, I’m working with shapes.

As you can see in the code below, each shape has a colored background

Code:
With objshp

  .Select

  .TextFrame.Characters.Text = Cells(my_row, 3).Value

  .Fill.ForeColor.RGB = RGB(col_red, col_green, col_blue)

End With

And some text is written in the shape with a white font color.

This is handy when the background is dark colored but becomes unreadable in case of light background.

Do some members of the forum have a suggestion how to decide in VBA to write the text in light of dark font color in function of the background?

Thanks in advance for your help.

Harry
 
Hi Harry ,

I am not clear on what your problem is.

If the colors of the cells and the font are both being decided by your code , then surely you can set up the proper contrasting combinations within your code.

Or do you already have the colored shapes in the worksheet , for which you need to identify the color and then decide what the font color should be ? Even if this is the case , we can still set up a table of contrasting colors , and then lookup the shape color to identify what the desired font color should be.

More information would help.

Narayan
 
Hi Narayan,

Thanks for your answer.

The context is very simple.

The procedure reads, in a worksheet, the three RGB values and applies them as forecolor in a shape.

The name of the color (also coming from the worksheet) is written in the shape.

I try to find a logical reasoning which decides of the contrast: dark forecolor of the shape involves light font color and inversely.

In your answer you are speaking about a table of contrasting colors. Building such of a table is perhaps the solution to my problem.

In attachment, two examples: in the dark example you can read the name of the color; in the light one, it is impossible.

Hoping that this explanation clarifies somewhat the problem.

Thanks in advance for your help.

Harrydark.JPG light.JPG
 
Hi ,

It would help if you could upload your workbook.

The next best would be to list :

1. Shape colors
2. The name of the color within the shape
3. The RGB values you mention

The data in the snapshots is not giving this information.

Narayan
 
Hi Narayan,
I suggest you open the uploaded file "test nuancier" and read the first sheet RMF (read me first).
You will understand I cannot give you a specific RGB value as it changes on each selection.
Hope this will help.
Thanks for your patience
Have a nice day

Harry
 

Attachments

  • test nuancier.xlsm
    59.6 KB · Views: 2
Try adding after the line:
Code:
  .Fill.ForeColor.RGB = RGB(col_red, col_green, col_blue)
the line:
Code:
  .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB((col_red + 128) Mod 255, (col_green + 128) Mod 255, (col_blue + 128) Mod 255)
This is far from perfect but may point you in a direction…
 
Hi P45cal,

Thank you for the answer.

I would never consider using a MOD function to choose a color.

This solution meets my wish perfectly.

Congratulations for this original suggestion.

Have a nice day

Harry
 
Back
Top