1

This question already has an answer here:

I have a very basic question concerning QGIS symbology:

How do you invert a color ramp for a hexbin layer? I could do the inversion to get reds to be the higher values in the raster equivalent of the dataset but can't seem to get the hexbin color ramp to invert. I am using a Spectral ColorBrewer ramp so blue is low value and red is high value.

marked as duplicate by underdark Aug 17 '14 at 18:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Smile :-) It was as simple as clicking a button! No problem. I enjoyed exploring how gqis uses ramps and what the cpt-city directory means. – Greg Aug 17 '14 at 19:38
0

So what I found out is that cpt (colour palette) utils has been used to convert a number gradients from all over the place. There is a cptutils-online conversion tool. The cptutils have been used to create the cpt-city site - An archive of colour gradients. This also explains why there is a [qgis_install_dir]/apps/qgis/resources/cpt-city-qgis-min/ directory. These files are from the cpt-city site. The cb directory is where the color-brewer files are located at. These are all .svg files. Looking at the spectial11.svg file you will see that <creator name="cptutils" version="1.46"/> created the file. What you'd have to do is go in an manually reverse all the <stop offset ... /> steps to achieve your inverted color ramp.

That's no fun. I installed the SVG2ColoR qgis plugin. It generates stops with numeric values and in a qgis_style format. Swapping the two colors in the file

<prop k="color1" v="94,79,162,255"/>
<prop k="color2" v="158,1,66,255"/>

did not reverse the ramp in the color manager. But this was the first step in the process. Next, I changed the stops line to three lines.

<prop k="stops" v="
0.0909...
"/>

I have been using the vim editor in this process. On the line starting with 0.0909, I used this command. The ^M has to be entered with controlQcontolM on MS Windows or controlVcontrolM on other platforms.

:s/:/:'::text as map_step^Munion all select '/g   

The changed lines must be prefixed with

select '

The last line of the changed lines must have

:'::text as map_step

appended to line. All this did was create a set of unioned select statements as shown below. The sql statement below was tested on PostgreSQL. All it does is preserve the original order of the lines from the qgis_style file with a reversed ramp. The new steps were put in a new file called Spectral_113.xml -- the third attempt. It has been attached below.

The answer was more about the joy of exploration and understanding. I hope it helps.


The sql to reverse the ramp.

with color_map as (
--
-- Begin place your ramp that you want to reverse here.
--
select '0.0909;158,1,66,255:'::text as map_step
union all select '0.0909;213,62,79,255:'::text as map_step
union all select '0.1818;213,62,79,255:'::text as map_step
union all select '0.1818;244,109,67,255:'::text as map_step
union all select '0.2727;244,109,67,255:'::text as map_step
union all select '0.2727;253,174,97,255:'::text as map_step
union all select '0.3636;253,174,97,255:'::text as map_step
union all select '0.3636;254,224,139,255:'::text as map_step
union all select '0.4545;254,224,139,255:'::text as map_step
union all select '0.4545;255,255,191,255:'::text as map_step
union all select '0.5455;255,255,191,255:'::text as map_step
union all select '0.5455;230,245,152,255:'::text as map_step
union all select '0.6364;230,245,152,255:'::text as map_step
union all select '0.6364;171,221,164,255:'::text as map_step
union all select '0.7273;171,221,164,255:'::text as map_step
union all select '0.7273;102,194,165,255:'::text as map_step
union all select '0.8182;102,194,165,255:'::text as map_step
union all select '0.8182;50,136,189,255:'::text as map_step
union all select '0.9091;50,136,189,255:'::text as map_step
union all select '0.9091;94,79,162,255:'::text as map_step
--
-- End place your ramp that you want to reverse here.
--
), cm_ordered as (
   -- Assign a number to preserve the stop order.
   select row_number() over() as rn,
          map_step
     from color_map cm
), cm_parsed as (
   -- Parse the step order and color at the semicolon.
   -- vim broke out the lines at the colon separator. 
   select rn, map_step as orig_map_step,
          substr( map_step, 1, strpos( map_step, ';' )  ) as step_num,
          substr( map_step, strpos( map_step, ';' ) + 1,
                  length( map_step ) ) as step_color
     from cm_ordered
), orig_ramp_order as (
   -- Create one of two tables for the final join.
   select rn, orig_map_step, step_num, step_color
     from cm_parsed
   order by rn
), new_ramp_order as (
   -- Now we assign a reverse order number.
   -- This also creates the second of two tables for the final join
   select row_number() over( order by rn desc ) as rev_rn, rn as orig_rn,
          orig_map_step as orig_map_stepn, step_num as orig_step_num,
          step_color as orig_step_color
     from cm_parsed
   order by rn desc
)
   -- Now the two tables are joined back together.
   -- The original step number order is used.
   -- The colors in reversed order are concatenated with the original step order.
   select step_num || orig_step_color as new_ramp
     from (
           select oro.*, nro.*
             from orig_ramp_order oro, new_ramp_order nro
            where rn = rev_rn
           order by rn
          )  finish_reverse;

The result set with the colors reversed.

new_ramp
0.0909;94,79,162,255:
0.0909;50,136,189,255:
0.1818;50,136,189,255:
0.1818;102,194,165,255:
0.2727;102,194,165,255:
0.2727;171,221,164,255:
0.3636;171,221,164,255:
0.3636;230,245,152,255:
0.4545;230,245,152,255:
0.4545;255,255,191,255:
0.5455;255,255,191,255:
0.5455;254,224,139,255:
0.6364;254,224,139,255:
0.6364;253,174,97,255:
0.7273;253,174,97,255:
0.7273;244,109,67,255:
0.8182;244,109,67,255:
0.8182;213,62,79,255:
0.9091;213,62,79,255:
0.9091;158,1,66,255:

The new Spectral_113.xml file.

<!DOCTYPE qgis_style>
<qgis_style version="1">
  <symbols/>
  <colorramps>
    <colorramp type="gradient" name="Spectral_113">
      <prop k="color1" v="94,79,162,255"/>
      <prop k="color2" v="158,1,66,255"/>
      <prop k="discrete" v="0"/>
      <prop k="stops" v="0.0909;94,79,162,255:0.0909;50,136,189,255:0.1818;50,136,189,255:0.1818;102,194,165,255:0.2727;102,194,165,255:0.2727;171,221,164,255:0.3636;171,221,164,255:0.3636;230,245,152,255:0.4545;230,245,152,255:0.4545;255,255,191,255:0.5455;255,255,191,255:0.5455;254,224,139,255:0.6364;254,224,139,255:0.6364;253,174,97,255:0.7273;253,174,97,255:0.7273;244,109,67,255:0.8182;244,109,67,255:0.8182;213,62,79,255:0.9091;213,62,79,255:0.9091;158,1,66,255"/>
    </colorramp>
  </colorramps>
</qgis_style>

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