//+------------------------------------------------------------------+
//|                                                        Color.mq5 |
//|                                               Copyright TheXpert |
//|                                           theforexpert@gmail.com |
//+------------------------------------------------------------------+
#property copyright "TheXpert"
#property link      "theforexpert@gmail.com"
#property version   "1.00"
//--- library
#property library
//--- maximum and minimum amount of colors
#define MIN_COLORS_COUNT 2
#define MAX_COLORS_COUNT 63
//+------------------------------------------------------------------+
//| Red color component                                              |
//+------------------------------------------------------------------+
uchar R(color clr)
  {
//--- return the value of the red color component
   return(uchar)(clr & 0x0000FF);
  }
//+------------------------------------------------------------------+
//| Green color component                                            |
//+------------------------------------------------------------------+
uchar G(color clr)
  {
//--- return the value of the red color component
   return(uchar)((clr & 0x00FF00)>>8);
  }
//+------------------------------------------------------------------+
//| Blue color component                                             |
//+------------------------------------------------------------------+
uchar B(color clr)
  {
//--- return the value of the blue color component
   return(uchar)((clr & 0xFF0000)>>16);
  }
//+------------------------------------------------------------------+
//| Set the value of the red color component                         |
//+------------------------------------------------------------------+
void SetR(uchar red,color &clr)
  {
   clr=color((clr & 0xFFFF00)|red);
  }
//+------------------------------------------------------------------+
//| Set the value of the green color component                       |
//+------------------------------------------------------------------+
void SetG(uchar green,color &clr)
  {
   clr=color((clr & 0xFF00FF)|(green<<8));
  }
//+------------------------------------------------------------------+
//| Set the value of the blue color component                        |
//+------------------------------------------------------------------+
void SetB(uchar blue,color &clr)
  {
   clr=color((clr & 0x00FFFF)|(blue<<16));
  }
//+------------------------------------------------------------------+
//| Receive the color from its components                            |
//+------------------------------------------------------------------+
color RGB(uchar red,uchar green,uchar blue)
  {
//--- return the color
   return red+(green<<8)+(blue<<16);
  }
//+------------------------------------------------------------------+
//| Filing the color table                                           |
//+------------------------------------------------------------------+
void FillColorTable(int plotIndex,color lowerClr,color higherClr,int count /*=63*/) export
  {
//--- if the number of passed colors is less than the minimum possible one, the minimum value is used
   if(count<MIN_COLORS_COUNT)
      count=MIN_COLORS_COUNT;
//--- if the number of passed colors exceeds the maximum possible one, the maximum value is used
   if(count>MAX_COLORS_COUNT)
      count=MAX_COLORS_COUNT;
//--- receive components of the lower color
   uchar lowerRed  =R(lowerClr);
   uchar lowerGreen=G(lowerClr);
   uchar lowerBlue =B(lowerClr);
//--- receive components of the upper color
   uchar higherRed  =R(higherClr);
   uchar higherGreen=G(higherClr);
   uchar higherBlue =B(higherClr);
//--- receive and set new colors in the loop
   for(int i=0;i<count;++i)
     {
      //--- receive color components
      uchar currentRed  =uchar(lowerRed  +(i/double(count-1))*(higherRed  -lowerRed));
      uchar currentGreen=uchar(lowerGreen+(i/double(count-1))*(higherGreen-lowerGreen));
      uchar currentBlue =uchar(lowerBlue +(i/double(count-1))*(higherBlue -lowerBlue));
      //--- set the new color
      PlotIndexSetInteger(plotIndex,PLOT_LINE_COLOR,i,RGB(currentRed,currentGreen,currentBlue));
     }
  }
//+------------------------------------------------------------------+
