Noitidart
Noitidart

Reputation: 37328

COLORREF to RGB

I'm trying to use JavaScript to convert a COLORREF:

COLORREF:

When specifying an explicit RGB color, the COLORREF value has the following hexadecimal form:

0x00bbggrr

The low-order byte contains a value for the relative intensity of red; the second byte contains a value for green; and the third byte contains a value for blue. The high-order byte must be zero. The maximum value for a single byte is 0xFF.

To create a COLORREF color value, use the RGB macro. To extract the individual values for the red, green, and blue components of a color value, use the GetRValue, GetGValue, and GetBValue macros, respectively.

I know about UInt32Array but I don't know how to use it.

How can I convert from COLORREF to RGB?

It should be the opposite of this function I found:

 cssColorToCOLORREF: function(csscolor) {
   let rgb = csscolor.substr(1);
   let rr = rgb.substr(0, 2);
   let gg = rgb.substr(2, 2);
   let bb = rgb.substr(4, 2);
   return parseInt("0x"+bb+gg+rr);
 },

Upvotes: 0

Views: 3177

Answers (1)

Jongware
Jongware

Reputation: 22478

COLORREF is typedef'ed as a DWORD, which is Microsoft's name for a 32-bit unsigned integer, and so its value can be split into R,G,B,A components using regular bit manipulations.

As an array:

input = 4294967295;
output = [
     input       & 0xff,
    (input >> 8) & 0xff,
    (input >>16) & 0xff,
    (input >>24) & 0xff
];

resulting in an output order as [red, green, blue, alpha]; or as an object:

output = {
    r: input       & 0xff,
    g:(input >> 8) & 0xff,
    b:(input >>16) & 0xff,
    a:(input >>24) & 0xff
};

resulting in output.r, output.g, output.b and output.a.

Upvotes: 3

Related Questions