Skip to content

Instantly share code, notes, and snippets.

@jscarto
Created December 8, 2016 21:43
Show Gist options
  • Select an option

  • Save jscarto/ff9a22eac8f17cceec4ec3964bb65d9b to your computer and use it in GitHub Desktop.

Select an option

Save jscarto/ff9a22eac8f17cceec4ec3964bb65d9b to your computer and use it in GitHub Desktop.
# Script modified from original by Anita Graser: https://twitter.com/underdarkGIS/status/716389969850404864
# To use: Enter a paletteName, paste array of hex codes into colors, and run!
# Re-running with the same name will update the previous color ramp
############################################################################
from PyQt4.QtGui import QColor
paletteName = '' # give the palette a name
colors = [] # replace with hex values
color1 = QColor(colors[0])
color2 = QColor(colors[-1])
stops = []
ramp = QgsVectorGradientColorRampV2(color1,color2,1,stops)
tags = []
if len(colors) > 2:
step = 1.0 / (len(colors))
for i, color in enumerate(colors[1:]):
stops.append(QgsGradientStop(step * (i+1), QColor(color)))
ramp.setStops(stops)
QgsStyleV2.defaultStyle().removeColorRamp(paletteName)
QgsStyleV2.defaultStyle().addColorRamp(paletteName, ramp, 0)
QgsStyleV2.defaultStyle().saveColorRamp(paletteName, ramp, 0, tags)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment