[Chartjs]-Generate color from number

4👍

Using a method from https://stackoverflow.com/a/2262117/2737978 you can convert an integer into an rgb value.

$("#slider1").on("input", function() {
  var value = $(this).val();
  var rgbColor = intToRGB(value);

  $(".number").html(value);
  $(".example").css('background-color', rgbColor);

});

var intToRGB = function(value) {
  //credit to https://stackoverflow.com/a/2262117/2737978 for the idea of how to implement
  var blue = Math.floor(value % 256);
  var green = Math.floor(value / 256 % 256);
  var red = Math.floor(value / 256 / 256 % 256);

  return "rgb(" + red + "," + green + "," + blue + ")";
}
#slider1 {
  width: 100%
}

.example {
  width: 50px;
  height: 50px;
  margin 0 auto;
  background-color: #000000;
  transition: background-color 100ms ease-in;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="slider1" type="range" min="0" max="16777215" step="1" value="0" />
<div class="number">0</div>
<div class="example"></div>

You would just need to use the intToRGB function in your code. Perhaps though if your numbers are all between 1-1000 you might not see such a gradient so would need to adjust the values (either the 256 or adjust the value going in by a certain factor)

—UPDATE

and here is a live example using your graph and a large factor to give a nice range of colour. You would probably want to add some validation into this to ensure the data does not go above the max but here is a quick go.

Here the int to RGB function has been updated to take in a max value that you would like to represent the top colour. On this a scale can more easily be created to give more diversity between ints, depending on your max the colours will be different as they land on a different part of the overall scale of what can be represented.

var intToRGB = function(value, alpha, max) {
  var valueAsPercentageOfMax = value / max;
  // actual max is 16777215 but represnts white so we will take a max that is
  // below this to avoid white
  var MAX_RGB_INT = 16600000;
  var valueFromMaxRgbInt = Math.floor(MAX_RGB_INT * valueAsPercentageOfMax);
  
  //credit to https://stackoverflow.com/a/2262117/2737978 for the idea of how to implement
  var blue = Math.floor(valueFromMaxRgbInt % 256);
  var green = Math.floor(valueFromMaxRgbInt / 256 % 256);
  var red = Math.floor(valueFromMaxRgbInt / 256 / 256 % 256);
 
  return "rgba(" + red + "," + green + "," + blue + "," + alpha + ")";
}

var data = [12, 19, 3, 5, 2, 3];

var MAX = 19;

var backgroundColors = data.map(function(item) {
  return intToRGB(item, 0.8, MAX);
});

var borderColors = data.map(function(item) {
  return intToRGB(item, 1, MAX);
});
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: data,
      backgroundColor: backgroundColors,
      borderColor: borderColors,
      borderWidth: 1
    }]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.6/Chart.bundle.js"></script>
<canvas id="myChart" width="400" height="400">
</canvas>

Leave a comment