[GIS] Different ways of saving color ramps in QGIS (xml, qml, csv), which should I use

color rampqgisqml

I started to use QGIS for personal mapping as a hobby (specially for planning bike routes), and I notice there are more than one way to use color ramps.

  1. There are styles, which can be saved and loaded, and may contain color ramps (that is, they aren't the color ramps themselves); When you save a style, the color ramp is saved as a colorrampshader tag in a .qml file;
  2. There are the color ramps themselves, which consist of a table of value, RGBA color (editable via color picker), and label. This, when saved, generate a plain CSV file with .txt extension. An important difference seem to be the mapping of each color to a defined value (for example, to a given elevation) as opposed to a percentage from bottom to top;
  3. There are xml files that apparently can be imported via Style Manager as described in this blog post.

What I would like to know is: what is the preferred/canonical way to store a color ramp? Where can I find the specifications about colorramp and colorrampshader in their respective xml namespaces?

And where does QGIS store the predefined color ramp files?

Best Answer

In QGIS, the preferred/canonical way to store a color ramp is QGIS layer styles (.qml) files.

If you look inside the qml file using a simple text editor, you'll find there is nothing hard to understand.

ColorRamp example:

<colorramp type="gradient" name="[source]">
  <prop k="color1" v="247,251,255,255"/>
  <prop k="color2" v="8,48,107,255"/>
  <prop k="discrete" v="0"/>
  <prop k="stops" v="0.13;222,235,247,255:0.26;198,219,239,255:0.39;158,202,225,255:0.52;107,174,214,255:0.65;66,146,198,255:0.78;33,113,181,255:0.9;8,81,156,255"/>
</colorramp>

ColorRampShader example:

    <colorrampshader colorRampType="DISCRETE" clip="0">
      <item alpha="255" value="0.5" label="0.1 - 0.5" color="#aaffff"/>
      <item alpha="255" value="1" label="0.5 - 1" color="#00aaff"/>
      <item alpha="255" value="5" label="1 - 5" color="#0055ff"/>
      <item alpha="255" value="10" label="5 - 10" color="#00007f"/>
      <item alpha="255" value="15" label="10+" color="#00003c"/>
    </colorrampshader>

If you are looking for specifications, please, check this answer.