Setting an objects color

 

Simply trying to change the color of a line, more red if the value in Field[1,i] is high, more orange if Field[1,i] is low. Right now, when I run the EA all the lines are pure black!

for(i=0;i<50;i++)
{
   ObjectCreate("L"+i,OBJ_HLINE,0,0,0);
   int R,G;
   R=245+Field[1,i];
   if(R>255) R=255;
   G=160-(Field[1,i]*16); 
   if(G<0) G=0;
   ObjectSet("L"+i,OBJPROP_COLOR,C'R,G,0');
}      
 
C'R,G,0'

What kind of notation is this? Does this even compile?

Color must be a 24 bit value:

clr = B * 65536 + G * 256 + R;

or more elegantly and probably even faster:

clr = (B << 16) | (G << 8) | R;

(or the other way around, RGB or BGR, I don't remember exactly)


EDIT: I looked up the documentation and you are probably trying to use this: https://docs.mql4.com/basis/types/color I have not tried it but the way I read this is these are meant to be literal constants. By definition a literal is a constant that is evaluated at compile time and it will put the constant value into the compiled code, there is no room for variables that would be evaluated at runtime, all there will be at runtime is a constant integer number. Therefore I wondered why this even compiles without complaining about the unknown or wrong identifiers R and G. All that could ever make sense for constant expressions at compile time would only be other constants but R and G are variables. Probably it interprets the letters as ascii bytes and your constant color is not exactly black, only almost black by pure coincidence.

 

Thanks 7bit.

So I'm not quite sure how I should re-write the ObjectSet based on what you're saying.

//in this case B will always = 0, so no need to calculate it into number
CLR=R*65536 + G*256; 
ObjectSet("L"+i,OBJPROP_COLOR,CLR);

Is this right??

 
You must make sure R and G are in the 0..255 range.
Reason: