New opportunities to work with color in Pine

May 13, 2021

Your scripts will sparkle with bright colors now that we’ve added new series colors and gradients from the RGB palette.

A new function ‘color.rgb (red, green, blue, transp)’ has been added to Pine. It allows you to set the color by passing the values of the components of red, green, blue and transparency (transp) as arguments. Now you can get rid of the definition of the entire color palette used in the script, and instead calculate the color based on the passed arguments. Take a look at this small example, which shows how much easier it has become to use complex color transitions:

//@version=4
study("Relative Bars Color Saturation", "RBCS", overlay=true)

i_barcolor = input(color.rgb(33, 150, 243), "Bar Color", input.color)

f_grad_transp(_c_col, _transp) =>
    _c_red = color.r(_c_col)
    _c_green = color.g(_c_col)
    _c_blue = color.b(_c_col)
    color.rgb(_c_red, _c_green, _c_blue, _transp)

f_indexedPercent(_currentValue, _relativeValue, _maxValue) =>
    _currentValue * _maxValue / _relativeValue

transp = f_indexedPercent(abs(close - open), high - low, 100)
c_color = f_grad_transp(i_barcolor, transp)
barcolor(c_color)

The value of the color components is set in the range from 0 (least intense) to 255 (most intense). For a transparency component, this range is from 0 (full opaque) to 100 (completely transparent).

If you need to get the numeric value of a specific color or transparency component, try using the special functions ‘color.r()’, ‘color.g()’, ‘color.b()’ and ‘color.t()’ by passing them a color as an argument. 

And the ‘color.from_gradient()’ function allows you to create impressive color transitions on each bar, the colors of the scripts are limited only by your imagination now. Here is an example that shows how much easier it has become to describe the color depending on the value of the relative strength index: 

//@version=4
study("RGB RSI")
i_src = input(close, "Source", input.source)
i_length = input(14, "Length", input.integer)

f_grad_transp(_c_col, _transp) =>
    _c_red = color.r(_c_col)
    _c_green = color.g(_c_col)
    _c_blue = color.b(_c_col)
    color.rgb(_c_red, _c_green, _c_blue, _transp)

rsi = rsi(i_src, i_length)
c_grad = color.from_gradient(rsi, 30, 70, color.lime, color.red)

plot(rsi, color=c_grad, linewidth=3)
band1 = hline(70, "Overbought", color=color.rgb(255, 82, 82))
band0 = hline(30, "Oversold", color=color.rgb(76, 175, 80))
fill(band1, band0, color=f_grad_transp(c_grad, 90), title="Background")

Read about all Pine updates in the Release Notes section in the User Manual.

Some built-in indicators (Momentum, TRIX, Price Oscillator, Detrended Price Oscillator, Chaikin Oscillator, Volume Oscillator, Ease of Movement, Rate of Change) have been updated and now use these new features, you can add them to your chart to see the RGB functionality in effect.

And here are some impressive examples of indicators with RGB colors from the community developers:

Directional Value by RicardoSantos

Color Gradient Framework by LucF, for PineCoders

Delta Volume Columns Pro by LucF

We hope you find the new features useful. And please do keep giving us feedback and suggestions — we’re building TradingView for you, and we’re always keen to know what you think about our platform updates.

Look first Then leap

TradingView is built for you, so make sure you're getting the most of our awesome features
Launch Chart