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.
| Role | Constants |
|---|---|
| Monochrome | color.black, color.white, color.gray, color.silver |
| Warm | color.red, color.maroon, color.orange, color.yellow, color.olive |
| Green | color.green, color.lime, color.teal |
| Blue | color.blue, color.navy, color.aqua, color.default |
| Purple | color.purple, color.fuchsia |
| Name | Hex | RGB Values |
|---|---|---|
color.default | #483D8B | color.rgb(72, 61, 139) |
color.aqua | #00FFFF | color.rgb(0, 255, 255) |
color.black | #000000 | color.rgb(0, 0, 0) |
color.blue | #0000FF | color.rgb(0, 0, 255) |
color.fuchsia | #FF00FF | color.rgb(255, 0, 255) |
color.gray | #808080 | color.rgb(128, 128, 128) |
color.green | #008000 | color.rgb(0, 128, 0) |
color.lime | #00FF00 | color.rgb(0, 255, 0) |
color.maroon | #800000 | color.rgb(128, 0, 0) |
color.navy | #000080 | color.rgb(0, 0, 128) |
color.olive | #808000 | color.rgb(128, 128, 0) |
color.orange | #FFA500 | color.rgb(255, 165, 0) |
color.purple | #800080 | color.rgb(128, 0, 128) |
color.red | #FF0000 | color.rgb(255, 0, 0) |
color.silver | #C0C0C0 | color.rgb(192, 192, 192) |
color.teal | #008080 | color.rgb(0, 128, 128) |
color.white | #FFFFFF | color.rgb(255, 255, 255) |
color.yellow | #FFFF00 | color.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 colorcolor.new(input color color, input float alpha) → input colorcolor.new(series color color, series float alpha) → series colorcolor.new(simple color color, simple float alpha) → simple colorThe 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 colorcolor.rgb(input int r, input int g, input int b, input float a) → input colorcolor.rgb(series int r, series int g, series int b, series float a) → series colorcolor.rgb(simple int r, simple int g, simple int b, simple float a) → simple colorcolor.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 intcolor.r(input color color) → input intcolor.r(series color color) → series intcolor.r(simple color color) → simple intcolor.g(const color color) → const intcolor.g(input color color) → input intcolor.g(series color color) → series intcolor.g(simple color color) → simple intcolor.b(const color color) → const intcolor.b(input color color) → input intcolor.b(series color color) → series intcolor.b(simple color color) → simple intcolor.a(const color color) → const floatcolor.a(input color color) → input floatcolor.a(series color color) → series floatcolor.a(simple color color) → simple floatcolor.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.