Search code examples
javascriptcolorskendo-colorpicker

Representing Color Using Just An Integer - Javascript


Let' say that I'm using kendo ui color picker widget. The widget expects hexadecimal color values. But the colors are represented by just one integer in the database. So I need a function to convert integer color to hexadecimal color and vice versa.

For example, I have the value -16777216 for a color in db. How can this number represent a color and how can I convert it to hexadecimal format?

How can I do that?

Thanks in advance,


Solution

  • Convert a number to a hexadecimal string with:

    var hexString = num.toString(16);
    

    and reverse the process with:

    var num = parseInt(hexString, 16);
    

    Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt