Creating new QGIS color ramp from r,g,b text file?

There's two options:

Option 1: Write a little PyQGIS script which converts the csv to a QgsVectorGradientColorRampV2 (see https://qgis.org/api/2.18/classQgsVectorGradientColorRampV2.html ). Specifically, you'll need to set the first color (via setColor1), the last color (Via setColor2), and all the stops in between (via setStops). This will look something like:

ramp = QgsVectorGradientColorRampV2()
ramp.setColor1( QColor( r,g,b ) ) # substitute in the first color here
ramp.setColor2( QColor( r,g,b ) ) # substitute in the last color
stops = []
for i in range(1,254):
    offset = 1.0/i
    color = QColor(r,g,b) # substitute color from line i of the csv
    stops.append(QgsGradientStop(offset, color ))
ramp.setStops(stops)

QgsStyleV2.defaultStyle().addColorRamp('Cividis', ramp)

or

Option 2: Export an existing ramp to XML, and then mash the csv color definitions into the same format using your favorite text editor/Python.


As you have described, formatted xml file (each color is represented as stop; R, G, B, A) would be required. To accelerate the task, we can (1) create a copy of existing color ramp by export and (2) edit the colors in the <colorramp> tag.

From the linked RGB text file, we can work on csv and/or text file. Below is one of MS-Excel example (column A starts at 1/257 and ends at 256/257, column E is alpha band).

enter image description here

We will concatenate fields and then merge this new color with an existing xml file.

Final cividis.xml:

<!DOCTYPE qgis_style>
<qgis_style version="1">
  <symbols/>
  <colorramps>
    <colorramp type="gradient" name="Cividis" favorite="1">
      <prop k="color1" v="0,32,76,255"/>
      <prop k="color2" v="255,233,69,255"/>
      <prop k="discrete" v="0"/>
      <prop k="rampType" v="gradient"/>
      <prop k="stops" v="0.0038911;0,32,76,255:0.0077821;0,32,78,255:0.0116732;0,33,80,255:0.0155642;0,34,81,255:0.0194553;0,35,83,255:0.0233463;0,35,85,255:0.0272374;0,36,86,255:0.0311284;0,37,88,255:0.0350195;0,38,90,255:0.0389105;0,38,91,255:0.0428016;0,39,93,255:0.0466926;0,40,95,255:0.0505837;0,40,97,255:0.0544747;0,41,99,255:0.0583658;0,42,100,255:0.0622568;0,42,102,255:0.0661479;0,43,104,255:0.0700389;0,44,106,255:0.0739300;0,45,108,255:0.0778210;0,45,109,255:0.0817121;0,46,110,255:0.0856031;0,46,111,255:0.0894942;0,47,111,255:0.0933852;0,47,111,255:0.0972763;0,48,111,255:0.1011673;0,49,111,255:0.1050584;0,49,111,255:0.1089494;0,50,110,255:0.1128405;0,51,110,255:0.1167315;0,52,110,255:0.1206226;0,52,110,255:0.1245136;1,53,110,255:0.1284047;6,54,110,255:0.1322957;10,55,109,255:0.1361868;14,55,109,255:0.1400778;18,56,109,255:0.1439689;21,57,109,255:0.1478599;23,57,109,255:0.1517510;26,58,108,255:0.1556420;28,59,108,255:0.1595331;30,60,108,255:0.1634241;32,60,108,255:0.1673152;34,61,108,255:0.1712062;36,62,108,255:0.1750973;38,62,108,255:0.1789883;39,63,108,255:0.1828794;41,64,107,255:0.1867704;43,65,107,255:0.1906615;44,65,107,255:0.1945525;46,66,107,255:0.1984436;47,67,107,255:0.2023346;49,68,107,255:0.2062257;50,68,107,255:0.2101167;51,69,107,255:0.2140078;53,70,107,255:0.2178988;54,70,107,255:0.2217899;55,71,107,255:0.2256809;56,72,107,255:0.2295720;58,73,107,255:0.2334630;59,73,107,255:0.2373541;60,74,107,255:0.2412451;61,75,107,255:0.2451362;62,75,107,255:0.2490272;64,76,107,255:0.2529183;65,77,107,255:0.2568093;66,78,107,255:0.2607004;67,78,107,255:0.2645914;68,79,107,255:0.2684825;69,80,107,255:0.2723735;70,80,107,255:0.2762646;71,81,107,255:0.2801556;72,82,107,255:0.2840467;73,83,107,255:0.2879377;74,83,107,255:0.2918288;75,84,107,255:0.2957198;76,85,107,255:0.2996109;77,85,107,255:0.3035019;78,86,107,255:0.3073930;79,87,108,255:0.3112840;80,88,108,255:0.3151751;81,88,108,255:0.3190661;82,89,108,255:0.3229572;83,90,108,255:0.3268482;84,90,108,255:0.3307393;85,91,108,255:0.3346304;86,92,108,255:0.3385214;87,93,109,255:0.3424125;88,93,109,255:0.3463035;89,94,109,255:0.3501946;90,95,109,255:0.3540856;91,95,109,255:0.3579767;92,96,109,255:0.3618677;93,97,110,255:0.3657588;94,98,110,255:0.3696498;95,98,110,255:0.3735409;95,99,110,255:0.3774319;96,100,110,255:0.3813230;97,101,111,255:0.3852140;98,101,111,255:0.3891051;99,102,111,255:0.3929961;100,103,111,255:0.3968872;101,103,111,255:0.4007782;102,104,112,255:0.4046693;103,105,112,255:0.4085603;104,106,112,255:0.4124514;104,106,112,255:0.4163424;105,107,113,255:0.4202335;106,108,113,255:0.4241245;107,109,113,255:0.4280156;108,109,114,255:0.4319066;109,110,114,255:0.4357977;110,111,114,255:0.4396887;111,111,114,255:0.4435798;111,112,115,255:0.4474708;112,113,115,255:0.4513619;113,114,115,255:0.4552529;114,114,116,255:0.4591440;115,115,116,255:0.4630350;116,116,117,255:0.4669261;117,117,117,255:0.4708171;117,117,117,255:0.4747082;118,118,118,255:0.4785992;119,119,118,255:0.4824903;120,120,118,255:0.4863813;121,120,119,255:0.4902724;122,121,119,255:0.4941634;123,122,119,255:0.4980545;123,123,120,255:0.5019455;124,123,120,255:0.5058366;125,124,120,255:0.5097276;126,125,120,255:0.5136187;127,126,120,255:0.5175097;128,126,120,255:0.5214008;129,127,120,255:0.5252918;130,128,120,255:0.5291829;131,129,120,255:0.5330739;132,129,120,255:0.5369650;133,130,120,255:0.5408560;134,131,120,255:0.5447471;135,132,120,255:0.5486381;136,133,120,255:0.5525292;137,133,120,255:0.5564202;138,134,120,255:0.5603113;139,135,120,255:0.5642023;140,136,120,255:0.5680934;141,136,120,255:0.5719844;142,137,120,255:0.5758755;143,138,120,255:0.5797665;144,139,120,255:0.5836576;145,140,120,255:0.5875486;146,140,120,255:0.5914397;147,141,120,255:0.5953307;148,142,120,255:0.5992218;149,143,120,255:0.6031128;150,143,119,255:0.6070039;151,144,119,255:0.6108949;152,145,119,255:0.6147860;153,146,119,255:0.6186770;154,147,119,255:0.6225681;155,147,119,255:0.6264591;156,148,119,255:0.6303502;157,149,119,255:0.6342412;158,150,118,255:0.6381323;159,151,118,255:0.6420233;160,152,118,255:0.6459144;161,152,118,255:0.6498054;162,153,118,255:0.6536965;163,154,117,255:0.6575875;164,155,117,255:0.6614786;165,156,117,255:0.6653696;166,156,117,255:0.6692607;167,157,117,255:0.6731518;168,158,116,255:0.6770428;169,159,116,255:0.6809339;170,160,116,255:0.6848249;171,161,116,255:0.6887160;172,161,115,255:0.6926070;173,162,115,255:0.6964981;174,163,115,255:0.7003891;175,164,115,255:0.7042802;176,165,114,255:0.7081712;177,166,114,255:0.7120623;178,166,114,255:0.7159533;180,167,113,255:0.7198444;181,168,113,255:0.7237354;182,169,113,255:0.7276265;183,170,112,255:0.7315175;184,171,112,255:0.7354086;185,171,112,255:0.7392996;186,172,111,255:0.7431907;187,173,111,255:0.7470817;188,174,110,255:0.7509728;189,175,110,255:0.7548638;190,176,110,255:0.7587549;191,177,109,255:0.7626459;192,177,109,255:0.7665370;193,178,108,255:0.7704280;194,179,108,255:0.7743191;195,180,108,255:0.7782101;197,181,107,255:0.7821012;198,182,107,255:0.7859922;199,183,106,255:0.7898833;200,184,106,255:0.7937743;201,184,105,255:0.7976654;202,185,105,255:0.8015564;203,186,104,255:0.8054475;204,187,104,255:0.8093385;205,188,103,255:0.8132296;206,189,103,255:0.8171206;208,190,102,255:0.8210117;209,191,102,255:0.8249027;210,192,101,255:0.8287938;211,192,101,255:0.8326848;212,193,100,255:0.8365759;213,194,99,255:0.8404669;214,195,99,255:0.8443580;215,196,98,255:0.8482490;216,197,97,255:0.8521401;217,198,97,255:0.8560311;219,199,96,255:0.8599222;220,200,96,255:0.8638132;221,201,95,255:0.8677043;222,202,94,255:0.8715953;223,203,93,255:0.8754864;224,203,93,255:0.8793774;225,204,92,255:0.8832685;227,205,91,255:0.8871595;228,206,91,255:0.8910506;229,207,90,255:0.8949416;230,208,89,255:0.8988327;231,209,88,255:0.9027237;232,210,87,255:0.9066148;233,211,86,255:0.9105058;235,212,86,255:0.9143969;236,213,85,255:0.9182879;237,214,84,255:0.9221790;238,215,83,255:0.9260700;239,216,82,255:0.9299611;240,217,81,255:0.9338521;241,218,80,255:0.9377432;243,219,79,255:0.9416342;244,220,78,255:0.9455253;245,221,77,255:0.9494163;246,222,76,255:0.9533074;247,223,75,255:0.9571984;249,224,73,255:0.9610895;250,224,72,255:0.9649805;251,225,71,255:0.9688716;252,226,70,255:0.9727626;253,227,69,255:0.9766537;255,228,67,255:0.9805447;255,229,66,255:0.9844358;255,230,66,255:0.9883268;255,231,67,255:0.9922179;255,232,68,255:0.9961089;255,233,69,255"/>
    </colorramp>
  </colorramps>
</qgis_style>

Save the above text as an xml file (cividis.xml) and import it into the Style Manager.


This is probably Option 2 mentioned by ndawson.