Sidechair

array.new_color()

Browse the reference

Builds an array of colors that works as a palette, read back by index to color plots and backgrounds.

Try it in the editor

A color array is a palette: build it once with array.new_color(3, color.gray), overwrite chosen slots with array.set(), then read one by index to color a plot or a background.

Without an initial_value the slots are na, and a plot given an na color draws nothing for that bar. A color made with color.new() keeps its transparency when it is stored.

Syntax

Pine Script
array.new_color(size: series int (optional), initial_value: series color (optional)) -> array<color>

Arguments

NameTypeDefaultDescription
sizeseries int0How many palette slots to start with: 0 or more, up to 100,000. Three slots cover a neutral, an up and a down color.
initial_valueseries colornaThe color written into every slot, such as color.gray as a neutral base. Left out, each slot is na and draws nothing.

Returns

array<color>: A new array of colors with the requested size, every slot holding initial_value or na.

Example

Pine ScriptPalette indexed by candle directionOpen in editor
//@version=6
indicator("array.new_color: state colors", overlay = true)
var shades = array.new_color(3, color.gray)
if barstate.isfirst
    array.set(shades, 1, color.green)
    array.set(shades, 2, color.red)
state = close > open ? 1 : close < open ? 2 : 0
plot(close, "Close", array.get(shades, state), 2)
bgcolor(color.new(array.get(shades, state), 90))

See also

Runs on Sidechair