Starting from a categorized layer, through this custom function:
from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
@qgsfunction(args='auto', group='Custom')
def get_catg_color(value, feature, parent):
"""
<h2>Example usage:</h2>
<ul>
<li>get_color("nomeCampoCategoria") -> '228,52,199,255'</li>
<li>get_color(to_string(format_number("nomeCampoCategoria",2))) -> '228,52,199,255'</li>
</ul>
"""
layer = iface.activeLayer()
renderer = layer.renderer()
if layer.renderer().type() == "categorizedSymbol":
campo = renderer.legendClassificationAttribute()
for cat in renderer.categories():
if str(value) == cat.value():
colorato = cat.symbol().symbolLayer(0).properties()['color']
break
return colorato
it is possible to capture the color and write it as an attribute of the layer itself:
to make elements of another layer acquire the same color following the proximity criterion, just use in the color property of the second layer, the expression:
overlay_nearest('Reg01012021_WGS84',"colore")[0]