How to make a color a bit darker?

 

Hi coders,

I use the color red for a rectangle. If a predefined event happens I would like to make this red a bit darker. Mathematically spoken I would like to subtract C'50,50,50' from the normal color.

Is that somehow possible without setting a new color? Sometimes I don't know about the color of a rectangle and so I can't use absolute color values. I only want to make the current color a bit darker, whatever color it originally is.

Thanks! 

 
mar:

Hi coders,

I use the color red for a rectangle. If a predefined event happens I would like to make this red a bit darker. Mathematically spoken I would like to subtract C'50,50,50' from the normal color.

Is that somehow possible without setting a new color? Sometimes I don't know about the color of a rectangle and so I can't use absolute color values. I only want to make the current color a bit darker, whatever color it originally is.

Thanks! 

I made this, not tested:

color darken(color mycolor,int c_offset)
  {
  int R=(mycolor%65536)%256;
  int G=((mycolor-R)%65536)/256;
  int B=(mycolor-(256*G)-R)/65536;
  
   if(R-c_offset>0) R-=c_offset;
   else R=0;
   if(G-c_offset>0) G-=c_offset;
   else G=0;
   if(B-c_offset>0) B-=c_offset;
   else B=0;

   return color(B*65536+G*256+R);
  }
 
Works perfect! Thank you very much! 
 
mar:
Works perfect! Thank you very much! 
You are welcome!
Reason: