color.new and color.rgb in MQL5: colours without transparency

Short answer

MQL5 colours have no alpha channel: write C'255,152,0' or a constant like clrOrange. Plot and chart-object colours cannot be transparent, so color.new transparency is blended with the chart background; real alpha needs a bitmap drawn with ColorToARGB through CCanvas.

color.new(c, transp) sets a colour's transparency from 0 (opaque) to 100 (invisible), and color.rgb(r, g, b, transp) builds a colour from channels. MQL5's color type is 0x00BBGGRR with no alpha channel: write literals as C'255,152,0' or use constants like clrOrange. Indicator plot colours and standard chart objects cannot be transparent, so the transparency has to be blended with the chart background; real alpha needs a bitmap drawn with ColorToARGB through the CCanvas library.

What color.new and color.rgb compute

A Pine colour carries red, green, blue and transparency. color.new(color.blue, 70) is the same blue at 70% transparency; color.rgb(255, 152, 0) is opaque orange and color.rgb(255, 152, 0, 50) half transparent. Channels can be computed per bar, which is how gradients and heat maps are drawn, and color.r, color.g, color.b, color.t read them back. A colour can also be na, which hides the plot on that bar.

Transparency matters visually: bgcolor(color.new(color.green, 90)) is a faint tint, and fills between plots almost always use 70-90.

The MQL5 equivalent

The example colours a line by candle direction with a three-colour palette, where the third colour is a transparency-blended orange, and draws a rectangle object behind the bars in a blended colour, the usual stand-in for a translucent box.new.

//@version=6
indicator("color demo", overlay=true)
color lineColor = close > open ? color.green : close < open ? color.red : color.new(color.orange, 70)
plot(hlc3, "hlc3", color=lineColor, linewidth=2)
color custom = color.rgb(255, 152, 0)
var box b = na
if barstate.islast
    b := box.new(bar_index - 20, low[20], bar_index, high, bgcolor=color.new(color.blue, 80), border_color=custom)
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrGreen, clrRed, clrGray
#property indicator_width1  2
#property indicator_label1  "hlc3"

double LineBuf[];
double ColorIdxBuf[];   // index into indicator_color1, not a colour

// Pine: color.rgb(r, g, b)  -- MQL5 colours are 0x00BBGGRR with no alpha
color Rgb(const int r, const int g, const int b)
{
   return (color)((r & 0xFF) | ((g & 0xFF) << 8) | ((b & 0xFF) << 16));
}

// Pine: color.new(c, transparency) for a buffer: blend with the chart
// background, since the terminal has no alpha for plot colours
color Fade(const color c, const int transparency)
{
   color bg = (color)ChartGetInteger(0, CHART_COLOR_BACKGROUND);
   double a = MathMin(MathMax(transparency, 0), 100) / 100.0;
   int r = (int)MathRound((c & 0xFF) * (1 - a) + (bg & 0xFF) * a);
   int g = (int)MathRound(((c >> 8) & 0xFF) * (1 - a) + ((bg >> 8) & 0xFF) * a);
   int b = (int)MathRound(((c >> 16) & 0xFF) * (1 - a) + ((bg >> 16) & 0xFF) * a);
   return Rgb(r, g, b);
}

int OnInit()
{
   SetIndexBuffer(0, LineBuf, INDICATOR_DATA);
   SetIndexBuffer(1, ColorIdxBuf, INDICATOR_COLOR_INDEX);
   // Pine: plot(..., color=color.new(color.orange, 70)) -- the faded colour becomes palette slot 2
   PlotIndexSetInteger(0, PLOT_LINE_COLOR, 2, Fade(Rgb(255, 152, 0), 70));

   // Pine: box.new(..., bgcolor=color.new(color.blue, 80))
   // Standard objects are opaque: blend the colour and draw it behind the bars
   string name = "demo_box";
   datetime t0 = iTime(_Symbol, _Period, 20), t1 = iTime(_Symbol, _Period, 0);
   double p0 = iLow(_Symbol, _Period, 20), p1 = iHigh(_Symbol, _Period, 0);
   if(ObjectCreate(0, name, OBJ_RECTANGLE, 0, t0, p0, t1, p1))
   {
      ObjectSetInteger(0, name, OBJPROP_FILL, true);
      ObjectSetInteger(0, name, OBJPROP_BACK, true);
      ObjectSetInteger(0, name, OBJPROP_COLOR, Fade(Rgb(41, 98, 255), 80));
   }
   return INIT_SUCCEEDED;
}

int OnCalculate(const int rates_total, const int prev_calculated,
                const datetime &time[], const double &open[],
                const double &high[], const double &low[],
                const double &close[], const long &tick_volume[],
                const long &volume[], const int &spread[])
{
   int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
   for(int i = start; i < rates_total; i++)
   {
      LineBuf[i] = (high[i] + low[i] + close[i]) / 3.0;
      // Pine: color = close > open ? color.green : close < open ? color.red : color.new(color.orange, 70)
      ColorIdxBuf[i] = close[i] > open[i] ? 0 : (close[i] < open[i] ? 1 : 2);
   }
   return rates_total;
}

void OnDeinit(const int reason) { ObjectDelete(0, "demo_box"); }

Per-bar colours in MQL5 are palette indices: the plot type is DRAW_COLOR_LINE (or another DRAW_COLOR_*), the palette is declared in indicator_color1 or set with PlotIndexSetInteger(plot, PLOT_LINE_COLOR, index, colour), and a second buffer of type INDICATOR_COLOR_INDEX holds the index per bar. The blend is computed against the background when the indicator starts, so it is correct for that chart's colour scheme.

Where the two differ

AspectPineMQL5
Literal#FF9800, color.orangeC'255,152,0', clrOrange (different shade from TradingView's)
From channelscolor.rgb(r, g, b)(color)(r | g << 8 | b << 16)
Transparency on plots0-100none; blend with background
Transparency on objects0-100standard objects are opaque; blend, set OBJPROP_BACK, or draw a CCanvas bitmap with ColorToARGB(c, alpha)
Per-bar colourany colour expressionindex into a palette of up to 64 colours
No colournaclrNONE
Read channelscolor.r(c) …c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF

What the Stratlate converter emits

color.new(c, 0) is the colour itself; any other transparency becomes pine_color_new(c, transp), which mixes the colour with CHART_COLOR_BACKGROUND by the transparency and returns clrNONE at 100. color.rgb(r, g, b) becomes pine_color_rgb, which clamps each channel to 0-255; a fourth transparency argument is dropped with an informational note (PM230), and color.t always returns 0 with the same note. Per-bar plot colours are collected into a palette at run time and written as indices to a colour-index buffer. A per-bar bgcolor or barcolor is reported as an approximation; see barcolor and bgcolor in MetaTrader.

Pitfalls

  • Writing a colour value into a colour-index buffer: the buffer wants 0, 1, 2…, not clrRed. Symptom: the line is drawn in the first palette colour or not at all.
  • Assuming clrOrange equals TradingView's orange: the named constants are web colours. Use C'r,g,b' with TradingView's values to match.
  • Dropping transparency instead of blending: an 80%-transparent background tint becomes a solid block that hides the candles.
  • Blending against a hard-coded white: on a dark chart the "faint" colour becomes bright. Read CHART_COLOR_BACKGROUND.
  • Byte order: 0xFF9800 as an MQL5 colour is blue-ish, because the low byte is red. Build colours from channels or use C'...' literals.

FAQ

Can MQL5 indicator plots be transparent?

No. Plot colours have no alpha, so a transparent Pine plot must be drawn in a colour mixed with the chart background. Standard chart objects are opaque too; only a bitmap drawn with the CCanvas library and ColorToARGB carries real alpha.

What is color.rgb in MQL5?

A color value built from channels: C'255,152,0' for constants, or (color)(r | (g << 8) | (b << 16)) for computed channels. The byte order is blue-green-red from high to low, the reverse of a web hex code.

How do I convert Pine transparency to an MQL5 alpha?

Alpha runs the other way and over 0-255: alpha = (100 - transparency) * 2.55, rounded. Pine transparency 80 is alpha 51. Pass it to ColorToARGB when drawing on a CCanvas bitmap; buffers and standard objects have no alpha.

Convert the whole script

Paste a Pine Script indicator or strategy and get MQL5 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.

Open the converter

Related