QGIS - How to differently colorize labels from two separate fields using color expression script?

I have 2 fields, I concatenate them [concat(to_string(field1), ' ', to_string(field2))] to show their labels side-by-side.

Is it possible to colorize from "text color expression" (using some scripts) first label in "color1" and second label in "color2"?

Clarification

I want the final result to be like below ("field1" data to be shown in red, "field2" data to be shown in green)

You may be able to use Symbology, rather than Label.

(1) Choose Symbology tab and add two layers of Centroid fill with the Font marker type. Give negative offset (e.g. -10.0mm) to red, and positive offset (e.g. 10.0mm) to green.

(2) Highlight Font marker. Scroll down a little bit further, and click on the Data defined override (a small pull-down menu). Select each of your field for the corresponding color.

(3) You will probably have to adjust font size and offset value, by visually checking what you see on the map.

• Yeah, not bad option. It is similar to "rule-based labeling" that I did. Thank you for your effort. I just want to find out if that "color expression" is capable for that or not. I failed but maybe someone (more advanced in scripting) knows the way. – Vitruvius Jan 24 at 11:27
• AFAIK the label color is applied to the entire label, there's no way to apply different colors to different parts of a label. It's possible for label items in the print layout, but not for map labels. For example, here's a print layout label with red and green text: <p style="color:red">red text</p><p style="color:green">green text</p> – csk Jan 24 at 18:19

EDIT: I misread your question, my apologies. I thought you wanted to select a color using an expression which depended on the value of two fields. Sorry about that.

Yes you can.

In the label options for the layer open the color selectio box. You don't need to set any colours here just find the colours you want and copy their hex codes into notepad:

In the Text tab for labelling add an expression for the color value by clicking the box shown below and choosing Edit:

Then add an expression like the following:

CASE
WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98'
WHEN "Field2" = 'value3' AND "Field2" = 'value4' THEN '#625974'
END

• WHEN "Field1" = 'value1' AND "Field2" = 'value2' THEN '#00ab98' that makes both fields (where their labels appear together) to be #00ab98. But I want different color for each label in all cases. – Vitruvius Jan 24 at 9:28