I know how to extract the geometry data, but I am struggling with the following derived attribute data in QGIS:

enter image description here

Searched a lot for extracting derived data, but only geometry data comes up. Result: I want to fill a new column with the color code.

@symbol_color works fine within the symbology, but it is not usable for the field calculator.

Original data: enter image description here

Label using @symbol_color enter image description here enter image description here

If I can label all the features with the color code, there must be a possibility to save the color code in the attribute table.

  • Using the variable @symbol_color works for labeling, but not in the field calculator.
    – Babel
    Commented Oct 25, 2021 at 15:42
  • Thx, tried everything I found, definitely does not work in the field calculator. Maybe there is any possibility to access it using pyqgis?
    – AndreasK
    Commented Oct 26, 2021 at 19:45
  • You should maybe modify the question and add a pyqgis tag to attract the attention of those who know more about this.
    – Babel
    Commented Oct 26, 2021 at 19:47
  • Which QGIS version do you use? Commented Oct 27, 2021 at 9:33

1 Answer 1


There is probably a more direct method, but you can drill down through the layer's renderer to get the categories of a categorised symbology, and subsequently the colours used in their symbol layers.

The categories I used are strings of random integers between 100 and 500.

For each category, you can get the attribute value used to symbolise it with its .value() method.

## get the layer
lyr = iface.activeLayer()

## get the field index of the attribute used to classify the symbology
category_field = lyr.fields().indexFromName('category')

## get the layer's renderer
rnd = lyr.renderer()

## get the categories from the categorized renderer
cats = rnd.categories()

## get the dataProvider of the layer to add a new field in which to store the values
prov = lyr.dataProvider()

## name of field
rgb_field_name = 'RGB'

## if field does not exist, create it
if rgb_field_name not in lyr.fields().names():
    prov.addAttributes([QgsField(rgb_field_name, QVariant.String)])

## get the layer's features as an iterator
feats = lyr.getFeatures()

## open an edit session

## iterate through features
for f in feats:
    ## iterate through the categories for each feature
    for c in cats:
        ## get the attribute value used for the current category
        cat_value = c.value()
        ## check if the current feature's category attribute matches the category value;
        ## if so, get the fill colour from the feature's symbol
        if f[category_field] == cat_value:
            ## get symbol
            s = c.symbol()

            ## get symbol layers
            sym_lyrs = s.symbolLayers()

            ## get the first symbol layer
            sym = sym_lyrs[0]

            ## get fill colour of symbol layer
            col = sym.fillColor()

            ## get the rgb values of the color
            rgb = col.getRgb()
            ## rgb is a tuple, so convert it to a string and assign it to the new field
            f[rgb_field_name] = str(rgb)
            ## update the feature's attributes

## save the edits and close the edit session

I am not sure how to get the hex color code, but there is probably a way of translating the RGB to hex if you particularly need hex.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.