May be ‘Config from query results’ transformation would help in your case:
https://play.grafana.org/d/Fc6gwV4nz/transforms-config-from-query?orgId=1&viewPanel=6
Add additional field to your query that would contain required value color basing on calculated in the same query dynamic threshold.