3

I have a geological units shapefile which has colour data in a field, in which the colour data relates to an associated text and will help in clarifying which unit is which down the line. An example is: "BASECOLOUR" !FIELD NAME "40, 15, 5, 0" !FIELD DATA

I assume these are CMYK values?

So, how can I do it? Can I build a rule-based expression which uses the field to define the CMYK values?

  • 3
    If you can extract each value of cyan, magenta, yellow, black to each corresponding attribute field, you can style it by expression. On the Layer Properties | Style tab, click on Data defined override (big epsilon button) next to Colors and Edit to give an expression color_cmyk("cyan", "magenta", "yellow", "black"). – Kazuhito May 25 '17 at 23:39
  • I will try to extract the data into different fields, any advice on that? ALSO i do not have a data defined overide button, however i can click the large epsilon (which causes a feature count)... i can still create a rule using the color_cmyk expression so that should work... – Simon May 26 '17 at 5:04
  • 1
    Extracting and splitting the text into c,m,y,k value would be tricky part. I myself would go with Excel 'Text to Columns' function but there may be better ways. – Kazuhito May 26 '17 at 5:54
  • @Simon, the example you posted is the content of only one field? I mean, !FIELD NAME "40, 15, 5, 0" !FIELD DATA is entirely stored in the "BASECOLOUR" field? I feel I'm wrong. Can you please attach only the content stored in the field for defining the colors? – mgri May 26 '17 at 10:32
  • hi mgri, yes the field name is BASECOLOUR, and the example of the data contained in the field was 40,15,5,0. – Simon Jun 1 '17 at 0:24
2

To parse the CMYK values you can setup a custom function in the Expression Builder and use it to extract the values into individual fields.

  1. When looking at the attribute table click on the expression builder.

enter image description here

  1. Click on the Function Editor tab and add a function to parse the color field. Click the "Load" button to "save/enable" it.

    def getColorComponent(color, num, feature, parent):
        items = color.split(',')
        return int(items[num])
    
  2. Use the new custom function to parse out one of the individual CMYK components and save it in a new field. Color values are zero indexed so in the example the 4th value is being stored in the black field with getColorComponent( "color" , 3).

Hope this helps with part of the problem. The good thing is you've got the colors as attributes.

  • Thanks cokrzys, this worked wonderfully. Of course i did not think at first that color was the name of my colour field so that took several minutes before i clicked. my colours are stored in the field "BASECOLOUR". – Simon Jun 1 '17 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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