Search code examples
javascriptd3.jscolorbrewer

Subset of colorbrewer set in D3


I'd like to use only the 5 darkest colors from the 'Blues' color set from colorbrewer.js. How do I do that? I can't find any documentation covering it. Here's my code:

var colorScale = d3.scale.quantize()
    .domain([0, maxInteractions*2])
    .range(colorbrewer.Blues[9]);

Solution

  • colorbrewer.Blues[9] is just an array of colors. You can use good old array.slice() to get the last five elements.

    colorbrewer.Blues[9]
    //["#f7fbff", "#deebf7", "#c6dbef", "#9ecae1", "#6baed6", "#4292c6", "#2171b5", "#08519c", "#08306b"]
    
    colorbrewer.Blues[9].slice(4)
    //["#6baed6", "#4292c6", "#2171b5", "#08519c", "#08306b"]