With(
{
h: Upper(Substitute(AppTheme.PrimaryColor, "#", "" ) ),
hexDigits: "0123456789ABCDEF"
},
With(
{
// Convert each hex pair manually to decimal
R: (Find( Mid(h,1,1), hexDigits ) - 1 ) * 16 + (Find( Mid(h,2,1), hexDigits ) - 1 ),
G: (Find( Mid(h,3,1), hexDigits ) - 1 ) * 16 + (Find( Mid(h,4,1), hexDigits ) - 1 ),
B: (Find( Mid(h,5,1), hexDigits ) - 1 ) * 16 + (Find( Mid(h,6,1), hexDigits ) - 1 )
},
With({brightness: (R * 299 + G * 587 + B * 114) / 1000},
If(brightness >= 186,
{Light: true, PrimaryColor: AppTheme.PrimaryColor, PrimaryText:"#000000"},
{Light: false,PrimaryColor: AppTheme.PrimaryColor, PrimaryText:"#ffffff"}
)
)
))