BGS use the BGSref column(s) in the attribute table to hold a Yellow (Y), Cyan (C), Magenta (M) value (in that order). The following python code can be used as part of a data defined properties, expression to obtain the RGB value.
# Python Function to be used within QGIS and called as part of data defined properties
#
# Takes BGSREF attribute value, which is in Yellow (Y), Cyan (C), Magenta (M) order
# and converts to Red (R), Green (G), Blue (B) order
#
# OUTPUT of the function is typically an RGB string in the style 255,148,0
# OUTPUT may occasionally be an RGB + alpha channel string 255,255,255,255
#
# EXAMPLE take a BGSREF (YCM) value of 608 and converts it into a RGB value of 255,51,117
# EXAMPLE take a BGSREF (YCM) value of 20 and converts it as 020 into a RGB value of 219,255,255
#
# EXPRESSION should look like: BGSrefToQGIScolorRGB( "BGSREF" )
#
# Technically RGB colours are calculated with colour + Black (K) as
# R = 255 * (1-C) * (1-K)
# G = 255 * (1-M) * (1-K)
# B = 255 * (1-Y) * (1-K)
from qgis.utils import qgsfunction
from qgis.core import QGis
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
#logger.info("Attempting to log some debug information")
aColourAdjust = [0,7,14,21,31,42,54,67,80,100]
@qgsfunction(1, "Python")
def BGSrefToQGIScolorRGB(values, feature, parent):
"""
Converts BGSref colour (attribution) from YCM to RGB values.
Output as integers in the range 0 - 255, in the style:
rrr,ggg,bbb(,aaa)
"""
rgbValue = "255,255,255"
isTransparent = False
#logger.info("+++++ Debug for attribute table row starts here +++++")
#logger.debug("Default rgbvalue is " + rgbValue)
bgsref2Convert = str(values[0])
# Test the length of the input value and pad with zeros if applicable.
if len(bgsref2Convert) == 1:
bgsref2Convert = "00" + bgsref2Convert
elif len(bgsref2Convert) == 2:
bgsref2Convert = "0" + bgsref2Convert
elif len(bgsref2Convert) == 3:
#logger.debug("bgsref2Convert value not changed")
pass
else:
#logger.debug("bgsref2Convert value is taken to be white transparent")
rgbValue = "255,255,255,255"
isTransparent = True
#logger.debug("bgsref2Convert value is: " + bgsref2Convert)
# Now we can calculate the RGB values...
if isTransparent == False:
try:
yellow = aColourAdjust[int(bgsref2Convert[:1])]
except TypeError:
yellow = aColourAdjust[int(str(bgsref2Convert)[:1])]
except Exception as e:
yellow = 0
try:
cyan = aColourAdjust[int(bgsref2Convert[1:2])]
except TypeError:
cyan = aColourAdjust[int(str(bgsref2Convert)[-1:])]
except Exception as e:
cyan = 0
try:
magenta = aColourAdjust[int(bgsref2Convert[-1:])]
except TypeError:
magenta = aColourAdjust[int(str(bgsref2Convert)[-1:])]
except Exception as e:
magenta = 0
# Python syntax: 100.0 is deliberate to get a floating point result, otherwise the result of division will always be zero.
#logger.debug("Y is: " + str(yellow) + " C is: " + str(cyan) + " M is: " + str(magenta))
red = int(255-((cyan/100.0)*255))
green = int(255-((magenta/100.0)*255))
blue = int(255-((yellow/100.0)*255))
# Now just output the expected QGIS format for the colour output...
rgbValue = str(red) + "," + str(green) + "," + str(blue)
#logger.debug("Output rgbvalue is " + rgbValue)
#logger.info("----- Debug for attrubute table row ends here -----")
return rgbValue