Color Gradient Framework [PineCoders]

PineCoders Mod Uppdaterad   

This indicator shows how you can use the new color functions in Pine to generate color gradients. We provide functions that will help Pine coders generate gradients for multiple use cases using base colors for bull and bear states.


For coders interested in maximizing the use of color in their scripts, TradingView has added new color functions and new functionality to existing functions. For us coders, this translates in the ability to generate colors on the fly and use dynamic colors ("series color") in more places.

New functions allow us to:
 • Generate colors dynamically from calculated RGBA components ("A" is the Alpha channel, known to Pine coders as the "transparency"). See color.rgb().
 • Extract RGBA components from existing colors. See color.r(), color.g(), color.b() and color.t().
 • Generate linear gradients between two colors. See color.from_gradient().

Improvements to existing color/plotting functions allow more flexible use of color:
 • plotcandle() now accepts a "series color" argument for its `wickcolor` and `bordercolor` parameters.
 • plotarrow() now accepts a "series color" argument for its `colorup` and `colordown` parameters.

Gradients are not only useful to make script visuals prettier; they can be used to pack more information in your displays. Our gradient #4 goes overboard with the concept by using a different gradient for the source line, its fill, and the background.


The script presents four functions to generate gradients:
f_c_gradientRelative(_source, _min, _max, _c_bear, _c_bull)
f_c_gradientRelativePro(_source, _min, _max, _c_bearWeak, _c_bearStrong, _c_bullWeak, _c_bullStrong)
f_c_gradientAdvDec(_source, _center, _c_bear, _c_bull)
f_c_gradientAdvDecPro(_source, _center, _steps, _c_bearWeak, _c_bearStrong, _c_bullWeak, _c_bullStrong)

The relative gradient functions are useful to generate gradients on a source that oscillates between known upper/lower limits. They use the relative position of the source between the `_min` and `_max` levels to generate the color. A centerline is derived from the `_min` and `_max` levels. The source's position above/below that centerline determines if the bull/bear color is used, and the relative position of the source between the centerline and the max/min level determines the gradient of the bull/bear color.

The advance/decline gradient functions are useful to generate gradients on a source for which min/max levels are unknown. These functions use source advances and declines to determine a gradient level. The `f_c_gradientAdvDec()` version uses the historical maximum of advances/declines to determine how many correspond to the strongest bull/bear colors, making its gradients adaptive. The `f_c_gradientAdvDecPro()` version requires the explicit number of advances/declines that correspond to the strongest bull/bear colors. This is useful when coloring chart bars, for example, where too many gradient levels are difficult to distinguish. Using the Pro version of the function allows you to limit the number of gradient levels to 5, for example, so that transitions are fewer, but more obvious. The `_center` parameter of the advance/decline functions allows them to determine which of the bull/bear colors to use.

Note that the custom `f_colorNew(_color, _transp)` function we use in our script should soon no longer be necessary, as changes are under way to allow to accept series arguments.

The script's inputs demonstrate one way you can allow users to choose base bull/bear colors. Because users can modify any of the colors, only two are technically needed: one for bull, one for bear, as we do for the configuration of the bull/bear colors for the background in the gradient #4 configuration. Providing a few presets from which users can choose can be useful for color-challenged script users, but that type of inputs has the disadvantage of not rendering optimally in all OS/Browser environments.

You can use the inputs to select one of eight gradient demonstrations to display.


Thanks to the PineCoders team for validating the code and description of this publication.
Thanks also to the many TradingView devs from multiple teams who made these improvements to Pine colors possible.

Look first. Then leap.

We added an RGBA color tuner at the bottom of the inputs. You can use it to play around with values and see the result displayed in the indicator's background. Remember to bring down the transparency to make your RGB colors visible.
• Removed `var` declaration inside `f_colorNew()`.
• Aesthetic fix.
v4 now accepts series arguments for both its `color` and `transp` parameters. Accordingly, we replaced calls to our custom `f_colorNew(_color, _transp)` function with calls to

Tools and ideas for all Pine coders:
Our Pine FAQ & Code:
Pine news broadcasts: or
Skript med en öppen källkod

I sann TradingView-anda har författaren publicerat detta skript med öppen källkod så att andra handlare kan förstå och verifiera det. Hatten av för författaren! Du kan använda det gratis men återanvändning av den här koden i en publikation regleras av våra ordningsregler. Du kan ange den som favorit för att använda den i ett diagram.

Frånsägelse av ansvar

Informationen och publikationerna är inte avsedda att vara, och utgör inte heller finansiella, investerings-, handels- eller andra typer av råd eller rekommendationer som tillhandahålls eller stöds av TradingView. Läs mer i Användarvillkoren.

Vill du använda det här skriptet i ett diagram?