How to get a hex of a color from a Color variable into a localized string?

Hi there!
I’m trying to highlight a part of the text with a specific color stored in the scirptableObject using smart strings.
The line goes as follows:
You need to <color=#{global.fixableOutlineColor}>clean up the yard and the house, and then start a <color=#{global.fixableOutlineColor}>fire at home.
I’m able to access the color variable. It is values are displayed on screen.


If this would be happening in a script I’d use ColorUtility.ToHtmlStringRGB(), but I’m trying to avoid creating an extra script for a single use in the whole game.
I guess I could also make an int field in the scriptableObject that would simply store the hex code of the color, but I’m hoping there is a better solution then creating extra variables.
So, can I turn it somehow into a hex code right inside the smart string without referring to making C# script?

Thanks!

One way to solve this would be to introduce a color formatter.

https://docs.unity3d.com/Packages/com.unity.localization@1.4/manual/Smart/Creating-a-Custom-Formatter.html

Something like this:

using UnityEngine.Localization;
using UnityEngine.Localization.SmartFormat.Core.Extensions;

[DisplayName("Color Formatter")]
public class ColorFormatter : FormatterBase
{
    public override string[] DefaultNames => new string[] { "color" };

    public override bool TryEvaluateFormat(IFormattingInfo formattingInfo)
    {
        if (formattingInfo.CurrentValue is Color color)
        {
            formattingInfo.Write(ColorUtility.ToHtmlStringRGB(color));
            return true;
        }
        return false;
    }
}

Now add this LocalizationSettings/String Database/Smart Format/Formatters, move it to around the midpoint in the list.

Now the string should look like:

You need <color=#{myColor:color()}>...

if i have localized text with int variables, how can i change color of this variables in runtime, after change their value for example? For example in eng loc text looks like:
Apply {damage} damage
“damage” in brackets is IntVariable

How are you setting the color? The same way? If the color is a persistent variable then changing it should trigger an automatic update.

i think it’s can looks like this
Apply <color=red>{damage} damage
but how to get access to color value and change it

I create formatter

 public class DamageValueFormatter : FormatterBase
{
     public override string[] DefaultNames => new string[] { "damageInfo" };

     public override bool TryEvaluateFormat(IFormattingInfo formattingInfo)
     {
         if (formattingInfo.CurrentValue is DamageChangeValue damageInfo)
         {
             if (damageInfo.Sign < 0)
                 formattingInfo.Write($"<color=red>{damageInfo.Damage}</color");
             else if (damageInfo.Sign > 0)
                 formattingInfo.Write($"<color=green>{damageInfo.Damage}</color");
             else
                 formattingInfo.Write($"<color=white>{damageInfo.Damage}</color");

             return true;  
         }

         return false;
     }
}

[Serializable]
public class DamageChangeValue : IVariable
{
     public int Damage;
     public int Sign;

     public object GetSourceValue(ISelectorInfo selector)
     {
         return new { Damage, Sign };
     }
}

the line
Apply {0:damageInfo():damage} damage
but something goes wrong)) I add it in localization settings and setup in inspector:

<color=red> is not a variable, its part of the string and will always be red.
You need a variable in place of red.
E.G <color=#{myColor}>

So you need a Color PersistentVariable

[Serializable]
public class ColorVariable : IVariable
{
    public Color color;

    public object GetSourceValue(ISelectorInfo _)
    {
        return color;
    }
}

With the formatter I provided here How to get a hex of a color from a Color variable into a localized string? - #2 by karl_jones

Now you can add the Color in the same way as you added damageInfo