A slight mistake to fix

For developers using the Construct 2 Javascript SDK

Post » Sat Oct 15, 2016 6:22 pm

It appears that if we set a ept_color property for a behaviour,it returns a decimal number instead of the format rgb(r,g,b) that gets returned in the case of a plugin.
The decimal should then be converted to hexadecimal, but the hex color is inverted in the form of bgr instead of rgb.

Its a big hassle, so it would be great if this gets fixed.
Banned User
B
17
S
7
G
23
Posts: 388
Reputation: 13,994

Post » Sat Oct 15, 2016 7:23 pm

You sure it's not rgba?
Image ImageImage
B
169
S
50
G
169
Posts: 8,284
Reputation: 108,212

Post » Sat Oct 15, 2016 7:43 pm

newt wrote:You sure it's not rgba?

No it should be returning : rgb(r,g,b)
Instead it returns a decimal, in my case its 6698973, which should be converted to hex, it becomes 0x6633ff.
But its in the form of bgr, which means that 0x66 is b, 0x33 is g and 0xff is red.

Code: Select all
new cr.Property(ept_color,    "Fog color",      0x222222,      "The color of the fog."),


Here is a workaround:
Code: Select all
var fogColor = hexToRgb(this.properties[2]);


Code: Select all
function hexToRgb(hex) {
      hex = hex.toString(16);
      var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
      return result ? {
         r: parseInt(result[1], 16),
         g: parseInt(result[2], 16),
         b: parseInt(result[3], 16)
      } : null;
   }
Banned User
B
17
S
7
G
23
Posts: 388
Reputation: 13,994

Post » Sat Oct 15, 2016 8:02 pm

OK. I know alpha uses a value of 1 to 0, so I thought that might be what you were seeing.
Image ImageImage
B
169
S
50
G
169
Posts: 8,284
Reputation: 108,212

Post » Sat Oct 15, 2016 10:04 pm

It probably isn't a mistake since it's a format used throughout C2. Here's how the rgb() function in converts it to a single number.
Number = red & (green<<8) & (blue<<16);

There isn't a standard color format but if I may, here's a shorter way to convert it to the format you want:
function hex2rgb(hex)
{
return {
r: hex&0xff,
g: (hex>>8)&0xff,
b: (hex>>16)&0xff
};
}
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 2 guests