How do I convert an integer to a javascript color?

convert rgb to hex javascript
convert rgba to hex javascript
convert hex code to color name in javascript
javascript rgb color
javascript color functions
rgb to hex converter
js convert color
rgb to hsl javascript

I have an integer which I need to convert to a color in javascript. I am using an MVC model and am trying to replicate a model in a software for a web interface. I have the color in integer format from the database. It needs to be converted to a color in javascript.

For example: integers like -12525360, -5952982

I have the code like this :

items[x].barStyle = "stroke: Black; fill = Red";

So instead of giving the fill:Red, I need to give it the exact color corresponding to the integer value.

This is the code I have written in C#. I need the same thing in javascript. Here resourcecolor= the integer input.

     var bytes = BitConverter.GetBytes(resourcecolor);
     ti.color = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);

In javascript, you express a ARGB color that is to be used with canvas or css as a string like "rgba(0-255,0-255,0-255,0-1)".

You can convert the integer to that string format with this function:

function toColor(num) {
    num >>>= 0;
    var b = num & 0xFF,
        g = (num & 0xFF00) >>> 8,
        r = (num & 0xFF0000) >>> 16,
        a = ( (num & 0xFF000000) >>> 24 ) / 255 ;
    return "rgba(" + [r, g, b, a].join(",") + ")";


How do I convert an integer to a javascript color?, Converting Color Spaces in JavaScript. Avatar of Because rgb() accepts either all integers or all percentages, both cases are covered. In the  I like that this shows the math to convert from base 10 to base 16 and back, but I wanted to note there is a much simpler way to do the hex-to-decimal conversion in Javascript: using parseInt with a base parameter.


hexColour = yourNumber.toString(16)

You may want to normalize 'yourNumber' too.

Converting Color Spaces in JavaScript, This integer is then converted into an RBG hex value by using the JavaScript toString(16) method and as an extra check making sure the string  I'm trying to use some Java Color int values from a database in Javascript. What's the correct way to convert the Java color int (like -2147473665) to an RGB string (like '#ffff00') using Javascript? Converting them straight to hex makes them all dark blue or black

Reading the comments on the question it seems you are able to manipulate the value on the server before its sent to the client. If you are using .NET I suggest using the ColorTranslator

int intColor = -12525360;
string htmlColor = ColorTranslator.ToHtml(Color.FromArgb(intColor)); //#40E0D0

Or this (if you need the alpha channel):

   int intColor = -12525360;
   Color c = Color.FromArgb(intColor);
   string htmlColor = String.Format(CultureInfo.InvariantCulture, 
                        "rgba({0},{1},{2},{3})", c.R, c.G, c.B, c.A / 255f);

How to: Convert a String to Hex Colour (with JS), Converting Hexadecimal Strings to Integers strings* representing red, green, and blue values, we can easily convert a hex colour to RGB like this: If radix is undefined or 0 (or absent), JavaScript assumes the following:. I have written a function in javascript to convert an integer form of colour from db to hex colour format.But i am unable to convert hex colour string to int form.Also parseInt(color.substr(1), 16) is giving different result.

Disclosure: I'm the author of the library suggested below.

If you want to use a library rather than coding it yourself, the pusher.color Javascript library supports integer to HTML color conversions:

// Will set c to "rgba(105,80,131,1.0)"
var c = pusher.color('packed_argb', -9875325).html()

Or if you wanted a different format:

var colorObject = pusher.color('packed_argb', -9875325);
var htmlString = colorObject.html();          // i.e. "rgba(105,80,131,1.0)"
var colorName  = colorObject.html('keyword'); // closest named color
var hexString  = colorObject.html('hex6');    // "#695083"

Internally the library uses code very similar to Esailija's answer.

On parseInt and Converting Hex to RGB, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  What I found to be the simplest and best solution for me was to directly use the Color class as follows: int red =; int green =; int blue =; int alpha = Color.alpha(intColor); This way I could already deal with the integer values without having to handle strings.

Really simply:

colorString = "#" + colour.toString(16).padStart(6, '0');

But when I actually reference it I also remove the alpha from the integer and set the style colour at the same time, so the whole line is:

document.getElementById('selectColourBtn').style.color = "#" + (colour & 0x00FFFFFF).toString(16).padStart(6, '0');

Color Converter, In JavaScript, a number can be a primitive value (typeof = number) or an object ( typeof = object). The valueOf() method is used internally in JavaScript to convert  I'm trying to convert a hexadecimal color string to a int in javascript. The color int must be the same format as VB6. I think the bytes are not in the normal order. Ex: 255 is red (#ff0000) and 16776960 is Aqua (#00ffff) I have a function to do the inverse: (But someone in the comments told me that it's not correct)

JavaScript Number Methods, Javascript functions for doing fast binary/hex/RGB color conversions using bitwise operations. bitcolor.js. // convert 0..255 R,G,B values to binary string. Convert boolean result into number/integer in JavaScript A JavaScript boolean represents one of two values: true or false . However, if one wants to convert a variable that stores boolean value, into integer “0” or “1” , they can do so using multiple approaches.

Javascript functions for doing fast binary/hex/RGB color conversions , Diving into JavaScript: How to Create a Hex2RGB Color Converter to a number , then use toString(16) to convert it to hexadecimal form and  There are two main ways to convert a string to a number in javascript. One way is to parse it and the other way is to change its type to a Number. All of the tricks in the other answers (e.g. unary plus) involve implicitly coercing the type of the string to a number. You can also do the same thing explicitly with the Number function. Parsing

Diving into JavaScript: How to Create a Hex2RGB Color Converter, Convert a string to an integer in JavaScript · PranchalKatiyar. Check out this Author's contributed articles. If you like GeeksforGeeks and would like  In such cases, we can convert a decimal value to the hexadecimal string by using the number.toString(16) and hexadecimal string to the decimal by using hex_string.parseInt(16). In both cases, 16 is the base to the number system that says that target or source value format is hexadecimal.

  • You'll need to be more specific.
  • how does the integer looks like? Which javascript color format do you want to get?
  • if(integer == 1) color == "red" else if(integer == 2) color == "green"?
  • give an example. what have you tried?
  • Still not enough information. What do the numbers represent exactly? What do you mean by "color" her? You cannot convert something into a "color", only into a representation of a color. So, what is your input and what is your expected output?
  • To put a little more context, in pure Javascript, this would work to convert a number to color. = "#" + value.toString(16);
  • It doesn't work for some cases, for example the int value 0 can't convert to desire '#000000'
  • This is a nice one liner: ('#' + mynum.toString(16).padStart(6, '0'));
  • Thanks, this the proper answer.
  • This is also works, though if there is a rgba format in .NET, you should use that because the #xxxxxx-format cannot encode alpha
  • Btw I'm not sure one can do what I edited the post to be in .NET but the alpha value in the css rgba format is between 0-1 (float), not 0-255. There should probably be some kind of cast there?
  • @Esailija yes, having 255 as a float value (by adding the "f") and using InvariantCulture (to always format the float with a ".") should do it.
  • To ensure that it works for numbers such as 0 it needs to be padded. A simple one-liner would then be: "#" + (number & 0x00FFFFFF).toString(16).padStart(6, '0')
  • Good catch! Updated answer. Thanks @ackh.