Skip to main content
Functions for creating and manipulating colors in your indicators.

color.new()

Create a new color with optional transparency.

Signature

color.new(color: string, transparency?: number): string

Parameters

ParameterTypeDescription
colorstringBase color (name or hex)
transparencynumberTransparency 0-100 (0=opaque, 100=transparent)

Example

var semiTransparent = color.new("red", 50);
var opaqueBlue = color.new("#0000FF", 0);
var lightGreen = color.new("green", 80);

color.rgb()

Create a color from RGB values.

Signature

color.rgb(red: number, green: number, blue: number, transparency?: number): string

Parameters

ParameterTypeDescription
rednumberRed component (0-255)
greennumberGreen component (0-255)
bluenumberBlue component (0-255)
transparencynumberTransparency 0-100

Example

var customRed = color.rgb(255, 0, 0);
var customPurple = color.rgb(128, 0, 128);
var transparentBlue = color.rgb(0, 0, 255, 50);

color.from_gradient()

Create a color from a gradient based on a value.

Signature

color.from_gradient(value: number, min: number, max: number, colorMin: string, colorMax: string): string

Parameters

ParameterTypeDescription
valuenumberCurrent value
minnumberMinimum value for gradient
maxnumberMaximum value for gradient
colorMinstringColor at minimum value
colorMaxstringColor at maximum value

Example

//@version=2

define(title="RSI Gradient", position="offchart", axis=true);

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var rsiValue = rsi(source=ohlcvData.close, period=14);

// Create gradient color from green (low RSI) to red (high RSI)
var rsiColor = color.from_gradient(rsiValue, 0, 100, "green", "red");

plotLine(value=rsiValue, width=2, colors=[rsiColor], label=["RSI"], desc=["RSI with gradient color"]);

Practical Examples

Conditional Coloring

//@version=2

define(title="Trend Colors", position="onchart", axis=false);

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);

var ema20 = ema(source=ohlcvData.close, period=20);
var isUptrend = ohlcvData.close > ema20;

// Use different colors based on trend
var trendColor = isUptrend ? color.new("green", 0) : color.new("red", 0);

plotLine(value=ema20, width=2, colors=[trendColor], label=["EMA"], desc=["Trend EMA"]);

RSI Color Zones

//@version=2

define(title="RSI Zones", position="offchart", axis=true);

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);
var rsiValue = rsi(source=ohlcvData.close, period=14);

// Color based on RSI zones
var rsiColor = rsiValue > 70 ? color.new("red", 0) :
               rsiValue < 30 ? color.new("green", 0) :
               color.new("blue", 0);

plotLine(value=rsiValue, width=2, colors=[rsiColor], label=["RSI"], desc=["Zone-colored RSI"]);

Transparency for Overlays

//@version=2

define(title="Transparent Bands", position="onchart", axis=false);

timeseries ohlcvData = ohlcv(symbol=currentSymbol, exchange=currentExchange);

var smaValue = sma(source=ohlcvData.close, period=20);
var upperBand = smaValue * 1.02;
var lowerBand = smaValue * 0.98;

// Use transparent colors for bands
var bandColor = color.new("blue", 80);

plotLine(value=upperBand, width=1, colors=[bandColor], label=["Upper"], desc=["Upper Band"]);
plotLine(value=smaValue, width=2, colors=["blue"], label=["SMA"], desc=["Middle"]);
plotLine(value=lowerBand, width=1, colors=[bandColor], label=["Lower"], desc=["Lower Band"]);

Color Constants

kScript provides predefined color constants:
ColorHex Value
red#FF0000
green#008000
blue#0000FF
yellow#FFFF00
orange#FFA500
purple#800080
white#FFFFFF
black#000000
gray#808080

Tips

Transparency

Use transparency (50-80) for overlapping elements to avoid obscuring important data.

Semantic Colors

Use consistent color meanings:
  • Green for bullish/positive
  • Red for bearish/negative
  • Blue for neutral
  • Orange/Yellow for warnings

Hex Colors

For precise color control, use hex values: "#FF6B35", "#3B82F6".

Accessibility

Choose colors with sufficient contrast. Avoid relying solely on color to convey information.