QGIS – Using Same Color for Label with Same Hour

expressionlabelingqgis

I have a layer with a timestamp column. I want the color of the labels with the same hour to be identical. I used this:

CASE
    WHEN hour("time") = 9 THEN color_rgb(rand(0, 255), rand(0, 255), rand(0, 255))
    WHEN hour("time") = 10 THEN color_rgb(rand(0, 255), rand(0, 255), rand(0, 255))
    WHEN hour("time") = 11 THEN color_rgb(rand(0, 255), rand(0, 255), rand(0, 255))
    WHEN hour("time") = 12 THEN color_rgb(rand(0, 255), rand(0, 255), rand(0, 255))
END

But it generates random colors for all labels as in the image.

enter image description here

I could use the following expression, but I want it to be random. I don't want to specify a different color for each hour manually.

CASE
    WHEN hour("time") = 9 THEN '255,0,0'
    WHEN hour("time") = 10 THEN '0,255,0'
    WHEN hour("time") = 11 THEN '0,0,255'
    WHEN hour("time") = 12 THEN '0,255,255'
END

enter image description here

How can I make the color of the label in each rectangle (in the first image) the same, but random? I hope there is a shorter way. The column includes other hours as well.

Best Answer

  1. Use Categorized symbology
  2. Value: hour("time"), Color ramp: 'Random colors'

enter image description here

  1. In Labels tab, use @symbol_color for the color data-defined override

enter image description here