Assign font text colour from a dictionary entry

Hi can anyone tell me why this code doesn’t work.
I’m assigning font properties from a dictionary. The line causing the problem is the one assigning the text colour.

dayCaption(idx).FontName = applyFont.Value(“applyFont”)
dayCaption(idx).SelectionFontName = applyFont.Value(“applyFont”)
dayCaption(idx).FontSize = applyFont.Value(“applyFontSize”)
dayCaption(idx).Bold = applyFont.Value(“applyBold”)
dayCaption(idx).Italic = applyFont.Value(“applyItalic”)
dayCaption(idx).Underline = applyFont.Value(“applyUnderline”)
dayCaption(idx).TextColor = applyFont.Value(“applyColour”)
dayCaption(idx).Text = rec.Column(“caption”).StringValue

When run that row fails with an Invalid cast exception. The message reads
_VariantColor cannot be cast to ColorGroup

If I replace that line with

var clr as Color = applyFont.Value(“applyColour”)
dayCaption(idx).TextColor = clr

it works. I’m quite happy to go with that but I’d like to understand why the original code doesn’t work.
I’m on Xojo 2025V2 and an Intel Mac running Ventura 13.7.6

Try this:

dayCaption(idx).TextColor = ColorGroup(applyFont.Value(“applyColour”).ObjectValue)

I’m not quite sure why you’re getting that particular error message - seems like the compiler or runtime are getting a bit confused by what you’re got going on - but making the assignment more explicit should help.

Thanks for the reply.
I don’t want a colour group, I want a Color. After scouring the documentation I eventually got it to compile. What I needed was

dayCaption(idx).TextColor = applyFont.Value(“applyColour”).ColorValue

I’ve not idea what that is necessary when it’s not for all the other datatypes.

Thanks anyway.

Oh interesting. What type of object are you assigning the color to? It’s not clear from your code.

Try this

dayCaption(idx).TextColor = color ( applyFont.Value(“applyColour”) )

Probably “applyFont.Value(“applyColour”)” is a variant.
If this is the case, you must transcode the variant in color.

It’s a text area.

That’s what I thought. Instead of reading up on Color objects I started to research Variants and tried the syntax that eventually worked

dayCaption(idx).TextColor = applyFont.Value(“applyColour”).ColorValue

What I don’t understand, this was the only row that caused a problem, all the other font properties converted without any problem.

Right. So DesktopTextArea.TextColor accepts a ColorGroup - that’s why you’re getting the error. The framework is smart enough to convert a Color into a ColorGroup for you in “normal” code, but since you’re converting from a Variant, it is getting confused - it’s like the framework will only do one layer of value conversion for you (_VariantColor → Color) but won’t then promote the Color to ColorGroup.

This is a problem you can run into with Variants and it just means that you have to give the compiler a little nudge in the correct direction, which you’ve done by telling it to use Variant.ColorValue. This avoids the extra value conversion, or perhaps performs it in a way that allows the framework to properly turn it into a ColorGroup in a later stage. You don’t need to do this with the other parameters because they are all Booleans, Strings, Integers, etc. which don’t involve any behind-the-scenes conversions between data types.

1 Like

Ok, that explains the problem.
Thanks for that.