Getting currentfeature's color in Expression Builder of QGIS?

In QGIS >= 2.14 you can use the symbol_color variable. Eg, set a data defined override for your label color to the expression @symbol_color


Can it be done. Of couse with QGIS the answer is normally yes.

Lets take our vector layer with some normal labels and symbols:

enter image description here

Now add a data defined color for the buffer:

enter image description here

Click on Edit and select the function editor.

enter image description here

Click new file and give it a name (called mine colorfuncs)

Paste the following code

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def symbol_color(layername, feature, parent):
    layer = QgsMapLayerRegistry.instance().mapLayersByName(layername)[0]
    r = layer.rendererV2()
    symbol = r.symbolForFeature(feature)
    values = symbol.color().getRgb()
    return "{}, {}, {}, {}".format(*values)

Should look like this:

enter image description here

Hit Run Script

Jump back over to the expressions tab and use the new function:

symbol_color('Pits')

We have to give it the name because expressions don't know about the layer only the feature.

Hit OK and Apply. BAM!

enter image description here

hmm almost. We just have to tweak the transparecny of the buffer:

enter image description here

BAM!

enter image description here