2

I have this value: -16744448; And this value is this color: the color associated with -16744448

And now, I need to know how to change any value in hex like '#01ff00', '#7fff82', '#c1ffc0', '#16F6F5','#81FFFC','#BFFFFF' to int32 (is necessary)

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(",") + ")";
}

I used this formula, but I don't know how to apply reverse engineering.

EDIT:

I have this value: -16744448 in the database, with the formula i have this value for the color: '#008000', i need to convert this color again in -16744448.

When i used the formula mentioned in the comments i got: 32768

This value is from VB.net, this function: Color.FromArgb

6
  • 1
    Does this answer your question? Convert hexadecimal color to integer in javascript Commented Sep 24, 2020 at 20:27
  • There is no such thing as "int32" in JavaScript. You can convert the string to a number. Commented Sep 24, 2020 at 20:30
  • Yeah, i know, but in the database i got only 'int32' numbers from Visual Basic. And know i need to transform these colors. They get with color.toARGB Commented Sep 24, 2020 at 22:21
  • @RyanWilson that answer doesn't work... He used a rgb color. Commented Sep 24, 2020 at 22:23
  • I don't know about the a - it doesn't seem to be in your number. "32768" is correct, just subtract 16^6.
    – iAmOren
    Commented Sep 24, 2020 at 22:50

2 Answers 2

1

We're removing the "#", parsing from hexadecimal to decimal, complementing by 16^6.
Back:
Uncomplementing, to hexadecimal string fill with leading zeros, trimming to last 6, adding "#" at the beginning.

var values=["#008000",'#01ff00', '#7fff82', '#c1ffc0', '#16F6F5','#81FFFC','#BFFFFF'];

values.forEach(v=>{
  var r=parseInt(v.slice(-6),16)-Math.pow(16,6);
  var rr="#"+("0".repeat(6)+(Math.pow(16,6)+r).toString(16)).slice(-6);
  console.log(v,r,rr);
});
.as-console-wrapper { max-height: 100% !important; top: 0; }

2
  • Thanks! that was exacty i want Commented Sep 25, 2020 at 13:34
  • Glad to help! Thanks for accepting my answer - could you up-vote it as well, please?
    – iAmOren
    Commented Sep 25, 2020 at 14:02
0

You can parse a hexadecimal code value with parseInt. Just set the radix to 16. This even takes care of three-digit hex code like #F00.

I started writing a CSS color value parser a while back, and used my hex logic below.

const colors = [ '#01ff00', '#7fff82', '#c1ffc0', '#16F6F5','#81FFFC','#BFFFFF' ]

console.log(colors.map(color => toColor(hexToInt(color))));

function hexToInt(input) {
  return parseInt(input.replace(/^#([\da-f])([\da-f])([\da-f])$/i,
    '#$1$1$2$2$3$3').substring(1), 16);
}; 

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.