title: Given a finite palette, how to sort from Hot to Cold, i.e. Diverging
body: <p><strong>Update</strong> (Original question below)</p>
  <p>Issue is partially solved.</p>
  <p>Still trying to figure out how to extend to other color combinations</p>
  <p>If I use this code</p>
  <pre><code>hLIM &lt;- rgb2hsv(col2rgb('#8000ff'))['h', ]
  sLIM &lt;- rgb2hsv(col2rgb('#8000ff'))['s', ]
  vLIM &lt;- rgb2hsv(col2rgb('#8000ff'))['v', ]
  rankorder &lt;- order((hLIM-tHSVcol[,1] + (hLIM &lt; tHSVcol[,1])),
                     (sLIM-tHSVcol[,2] + (sLIM &lt; tHSVcol[,2])),
                     (vLIM-tHSVcol[,3] + (vLIM &lt; tHSVcol[,3])))
  
  orderType &lt;- &quot;HSV Ordering&quot;
  </code></pre>
  <p>I am able to sort this</p>
  <p><a href="https://i.stack.imgur.com/tjM7j.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tjM7j.jpg" alt="enter image description here" /></a></p>
  <p><strong>Unsorted Color Palette:</strong></p>
  <p><code>&quot;#313695&quot;,&quot;#fdae61&quot;,&quot;#a50026&quot;,&quot;#ffffbf&quot;,&quot;#f46d43&quot;,&quot;#fee090&quot;,&quot;#e0f3f8&quot;,&quot;#74add1&quot;,&quot;#d73027&quot;,&quot;#4575b4&quot;,&quot;#abd9e9&quot;</code></p>
  <p>Into this</p>
  <p><a href="https://i.stack.imgur.com/lcmSJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lcmSJ.jpg" alt="enter image description here" /></a></p>
  <p><strong>Sorted Color Palette:</strong></p>
  <p><code>&quot;#a50026&quot;,&quot;#d73027&quot;,&quot;#f46d43&quot;,&quot;#fdae61&quot;,&quot;#fee090&quot;,&quot;#ffffbf&quot;,&quot;#e0f3f8&quot;,&quot;#abd9e9&quot;,&quot;#74add1&quot;, &quot;#4575b4&quot;,&quot;#313695&quot;</code></p>
  <p>However, I have difficulty extending it to other color combinations.</p>
  <p>For Instance when I try to sort this</p>
  <p><a href="https://i.stack.imgur.com/gGfsu.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gGfsu.jpg" alt="enter image description here" /></a></p>
  <p><strong>Unsorted</strong></p>
  <pre><code>&quot;#22325f&quot; &quot;#88ce64&quot; &quot;#fbd234&quot; &quot;#b8091f&quot; &quot;#682f4e&quot; &quot;#fdea6e&quot; &quot;#4aec6a&quot; &quot;#fb7894&quot; &quot;#f13111&quot; &quot;#2584a0&quot; &quot;#460809&quot; &quot;#00699e&quot; &quot;#391b72&quot; &quot;#03471d&quot; &quot;#ba0841&quot;
  </code></pre>
  <p>I get this (Interesting part is, I'm unsure how the perfectly sorted palette would look like ---- certainly no dark bands interspersed!)</p>
  <p><a href="https://i.stack.imgur.com/tJ8k8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tJ8k8.jpg" alt="enter image description here" /></a></p>
  <p>Imperfectly Sorted</p>
  <pre><code>&quot;#391b72&quot; &quot;#22325f&quot; &quot;#00699e&quot; &quot;#2584a0&quot; &quot;#03471d&quot; &quot;#4aec6a&quot; &quot;#88ce64&quot; &quot;#fdea6e&quot; &quot;#fbd234&quot; &quot;#f13111&quot; &quot;#460809&quot; &quot;#b8091f&quot; &quot;#fb7894&quot; &quot;#ba0841&quot; &quot;#682f4e&quot;
  </code></pre>
  <hr />
  <hr />
  <hr />
  <h1>Original Question</h1>
  <p>I have a finite palette, how to sort from Hot(reds) to Cold(blues).</p>
  <p>How do I turn this</p>
  <p><a href="https://i.stack.imgur.com/tjM7j.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tjM7j.jpg" alt="enter image description here" /></a></p>
  <p><strong>Unsorted Color Palette:</strong></p>
  <p><code>&quot;#313695&quot;,&quot;#fdae61&quot;,&quot;#a50026&quot;,&quot;#ffffbf&quot;,&quot;#f46d43&quot;,&quot;#fee090&quot;,&quot;#e0f3f8&quot;,&quot;#74add1&quot;,&quot;#d73027&quot;,&quot;#4575b4&quot;,&quot;#abd9e9&quot;</code></p>
  <p>Into this</p>
  <p><a href="https://i.stack.imgur.com/lcmSJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lcmSJ.jpg" alt="enter image description here" /></a></p>
  <p><strong>Sorted Color Palette:</strong></p>
  <p><code>&quot;#a50026&quot;,&quot;#d73027&quot;,&quot;#f46d43&quot;,&quot;#fdae61&quot;,&quot;#fee090&quot;,&quot;#ffffbf&quot;,&quot;#e0f3f8&quot;,&quot;#abd9e9&quot;,&quot;#74add1&quot;, &quot;#4575b4&quot;,&quot;#313695&quot;</code></p>
  <p>R Code is at the end of the message. I tried sorting by combinations of <code>RGB</code> and <code>HSV</code>, (based on what is commented, or uncommented) but to no avail.</p>
  <p>I am unable to find a pattern in the following values (where colors are sorted divergently)</p>
  <p>Any guidance is greatly appreciated.</p>
  <p><strong>RGB Sorted Color</strong></p>
  <pre><code>&gt; tRGBcol
        red green blue
   [1,] 165     0   38
   [2,] 215    48   39
   [3,] 244   109   67
   [4,] 253   174   97
   [5,] 254   224  144
   [6,] 255   255  191
   [7,] 224   243  248
   [8,] 171   217  233
   [9,] 116   173  209
  [10,]  69   117  180
  [11,]  49    54  149
  </code></pre>
  <p><strong>HSV Sorted Color</strong></p>
  <pre><code>&gt; tHSVcol
                  h          s         v
   [1,] 0.961616162 1.00000000 0.6470588
   [2,] 0.008522727 0.81860465 0.8431373
   [3,] 0.039548023 0.72540984 0.9568627
   [4,] 0.082264957 0.61660079 0.9921569
   [5,] 0.121212121 0.43307087 0.9960784
   [6,] 0.166666667 0.25098039 1.0000000
   [7,] 0.534722222 0.09677419 0.9725490
   [8,] 0.543010753 0.26609442 0.9137255
   [9,] 0.564516129 0.44497608 0.8196078
  [10,] 0.594594595 0.61666667 0.7058824
  [11,] 0.658333333 0.67114094 0.5843137
  </code></pre>
  <p>Manually split hex colors</p>
  <pre><code>a5  00  26
  d7  30  27
  f4  6d  43
  fd  ae  61
  fe  e0  90
  ff  ff  bf
  e0  f3  f8
  ab  d9  e9
  74  ad  d1
  45  75  b4
  31  36  95
  </code></pre>
  <p><strong>Code</strong></p>
  <pre><code>RenderPal &lt;- function(x,name){
    
    if ((missing(x)) || (missing(name))){
      stop(&quot;Internal error, please troubleshoot&quot;)
    }
    n &lt;- length(x)
    old &lt;- graphics::par(mar = c(0.5, 0.5, 0.5, 0.5))
    on.exit(graphics::par(old))
    
    graphics::image(1:n, 1, as.matrix(1:n), col = x,
                    ylab = &quot;&quot;, xaxt = &quot;n&quot;, yaxt = &quot;n&quot;, bty = &quot;n&quot;)
    graphics::rect(0, 0.9, n + 1, 1.1, col = grDevices::rgb(1, 1, 1, 0.8), border = NA)
    graphics::text((n + 1) / 2, 1, labels = name, cex = 2, family = &quot;serif&quot;)
  }
  
  i &lt;- NULL
  oldPal &lt;- NULL
  rankorder &lt;- c(1,2,3,4,5,6,7,8,9,10,11)
  orderedPal&lt;- NULL
  RGBcol &lt;- NULL
  HSVcol &lt;- NULL
  tHSVcol &lt;- NULL
  orderType &lt;- &quot;Unsorted&quot;
  
  
  
  oldPal &lt;- c(&quot;#313695&quot;,&quot;#fdae61&quot;,&quot;#a50026&quot;,&quot;#ffffbf&quot;,&quot;#f46d43&quot;,&quot;#fee090&quot;,&quot;#e0f3f8&quot;,&quot;#74add1&quot;,&quot;#d73027&quot;,&quot;#4575b4&quot;,&quot;#abd9e9&quot;)
  
  # Print hex values
  oldPal
  
  # Convert Hex to RGB
  RGBcol &lt;- col2rgb(oldPal)
  
  # Print RGB values
  RGBcol
  
  # Transpose matrix
  tRGBcol &lt;- t(RGBcol)
  
  # Print matrix
  tRGBcol
  
  # Uncomment following to order by Red, Green, then Blue
  # rankorder &lt;- order(tRGBcol[,1],tRGBcol[,2],tRGBcol[,3])
  # orderType &lt;- &quot;Red Ordering&quot;
  
  # Uncomment following to order by Blue, Green, then Red
  # rankorder &lt;- order(tRGBcol[,3],tRGBcol[,2],tRGBcol[,1])
  # orderType &lt;- &quot;Blue Ordering&quot;
  
  # Uncomment following to order by Green, Blue then Red
  # rankorder &lt;- order(tRGBcol[,2],tRGBcol[,3],tRGBcol[,1])
  # orderType &lt;- &quot;Green Ordering&quot;
  
  # Uncomment following to order by Red + Blue
  # rANDb &lt;- apply(tRGBcol[,c(1,3)],1,sum)
  # rankorder &lt;- order(rANDb)
  # orderType &lt;- &quot;Red + Blue Ordering&quot;
  
  # Uncomment following to order by Red + Green + Blue
  # rANDgANDb &lt;- apply(tRGBcol[,c(1,2,3)],1,sum)
  # rankorder &lt;- order(rANDgANDb)
  # orderType &lt;- &quot;Red + Green + Blue Ordering&quot;
  
  # Convert RGB to HSV
  HSVcol &lt;- rgb2hsv(RGBcol)
  
  # Print matrix
  HSVcol
  
  # Transpose matrix
  tHSVcol &lt;- t(HSVcol)
  
  # Print matrix
  tHSVcol
  
  
  # Uncomment following to order by Hue, then Saturation, then Value
  # rankorder &lt;- order(tHSVcol[,1],tHSVcol[,2],tHSVcol[,3])
  # orderType &lt;- &quot;Hue Ordering&quot;
  
  # Uncomment following to order by hANDv = Hue + Value
  # hANDv &lt;- apply(tHSVcol[,c(1,3)],1,sum)
  # rankorder &lt;- order(hANDv)
  # orderType &lt;- &quot;Hue + Value Ordering&quot;
  
  # Uncomment following to order by hPRODv = Hue * Value
  # hPRODv &lt;- apply(tHSVcol[,c(1,3)],1,prod)
  # rankorder &lt;- order(hPRODv)
  # orderType &lt;- &quot;Hue * Value Ordering&quot;
  
  # Combine matrices tRGBcol and tHSVcol
  tCombo &lt;- cbind(tRGBcol,tHSVcol)
  
  rankorder
  
  for (i in 1:length(rankorder)){
    orderedPal[i] &lt;- oldPal[rankorder[i]]
  }
  
  # Print old, unordered palette
  oldPal
  
  # Print new, ordered palette
  orderedPal
  
  RenderPal(oldPal, &quot;Unordered Palette&quot;)
  RenderPal(orderedPal, orderType)
  </code></pre>
  <p>Edit: Result when I sort by Hue</p>
  <p><a href="https://i.stack.imgur.com/2Hm0g.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2Hm0g.jpg" alt="enter image description here" /></a></p>