Colour and transparency

Every drawing takes a colour.

There are three ways to name one: a colour constant, a colour built from components with color.rgb, and a constant given a new opacity with color.new.

The named constants

Eighteen colours are provided as constants. The names describe the colours; their hexadecimal and RGB values are listed below.

RoleConstants
Monochromecolor.black, color.white, color.gray, color.silver
Warmcolor.red, color.maroon, color.orange, color.yellow, color.olive
Greencolor.green, color.lime, color.teal
Bluecolor.blue, color.navy, color.aqua, color.default
Purplecolor.purple, color.fuchsia
NameHexRGB Values
color.default#483D8Bcolor.rgb(72, 61, 139)
color.aqua#00FFFFcolor.rgb(0, 255, 255)
color.black#000000color.rgb(0, 0, 0)
color.blue#0000FFcolor.rgb(0, 0, 255)
color.fuchsia#FF00FFcolor.rgb(255, 0, 255)
color.gray#808080color.rgb(128, 128, 128)
color.green#008000color.rgb(0, 128, 0)
color.lime#00FF00color.rgb(0, 255, 0)
color.maroon#800000color.rgb(128, 0, 0)
color.navy#000080color.rgb(0, 0, 128)
color.olive#808000color.rgb(128, 128, 0)
color.orange#FFA500color.rgb(255, 165, 0)
color.purple#800080color.rgb(128, 0, 128)
color.red#FF0000color.rgb(255, 0, 0)
color.silver#C0C0C0color.rgb(192, 192, 192)
color.teal#008080color.rgb(0, 128, 128)
color.white#FFFFFFcolor.rgb(255, 255, 255)
color.yellow#FFFF00color.rgb(255, 255, 0)

Each constant is const-form, so a constant can go anywhere a colour is accepted.

Opacity runs from 0 to 1, and 1 is solid

color.new takes a colour and an opacity and returns the same colour at that opacity.

color.new(const color color, const float alpha) → const color
color.new(input color color, input float alpha) → input color
color.new(series color color, series float alpha) → series color
color.new(simple color color, simple float alpha) → simple color

The opacity runs from 0 to 1. A value of 1 is solid and a value of 0 is invisible. Read the argument as how much of the colour you want, not how much you want taken away. A value outside the range is clamped to it rather than rejected, so an out-of-range number will not fail the script and will not warn you either.

Both arguments are required.

indicator("Opacity")
plot(close, title = "Close", color = color.new(color.blue, 0.6))
plot(hl2, title = "Midpoint", color = color.rgb(255, 165, 0))

Building a colour from components

color.rgb(const int r, const int g, const int b, const float a) → const color
color.rgb(input int r, input int g, input int b, input float a) → input color
color.rgb(series int r, series int g, series int b, series float a) → series color
color.rgb(simple int r, simple int g, simple int b, simple float a) → simple color

color.rgb uses two scales in one call. The three components run from 0 to 255. The opacity runs from 0 to 1, the same scale color.new uses. Components and opacity are both clamped to their range.

The three components are required. The opacity is not: leave it out and the colour is solid.

Reading a colour back

Four functions take a colour apart. Reading is asymmetric with writing.

color.r(const color color) → const int
color.r(input color color) → input int
color.r(series color color) → series int
color.r(simple color color) → simple int
color.g(const color color) → const int
color.g(input color color) → input int
color.g(series color color) → series int
color.g(simple color color) → simple int
color.b(const color color) → const int
color.b(input color color) → input int
color.b(series color color) → series int
color.b(simple color color) → simple int
color.a(const color color) → const float
color.a(input color color) → input float
color.a(series color color) → series float
color.a(simple color color) → simple float

color.r, color.g and color.b return whole numbers from 0 to 255. color.a returns a fraction from 0 to 1. The two scales that went into color.rgb come back out the same way round.

Colour functions and na

Every colour function returns na when the colour it is given is na. The result is not a black, and it is not a zero.

Forms

Each colour function carries four signatures, one per form of its arguments. The form goes in and the same form comes out: give color.new a const colour and a const opacity and you get a const colour; give it series arguments and you get a series colour. That is what lets a colour vary from bar to bar and still satisfy a series-form colour parameter.

What is not here

There is no gradient helper. There is no separate transparency function. The colour namespace contains exactly six functions, and neither of those is among them.

Use color.new to set opacity on an existing colour. That is the whole of the transparency surface.

A script that calls a gradient helper, or a separate transparency function, fails.

Ready to put this into practice?
Try GoCharting Premium
Unlock advanced orderflow, market profile, options desk, and real-time data — everything you just read about, live in your charts.
Upgrade