Skip to content

Colors

View Markdown

Import Color and colors from openbacktesting. Colors are immutable RGBA values used by plotting and color inputs.

Color

from openbacktesting import Color, colors

up = colors.GREEN
faded = up.with_transparency(80)
custom = Color.rgb(30, 120, 220, transparency=25)
parsed = Color.from_hex("#2277AA")
print(custom.to_hex())

middle = Color.from_gradient(
    50, lower=0, upper=100, lower_color=colors.RED, upper_color=colors.GREEN,
)
print(middle.to_hex())

rgb takes integer red/green/blue components in 0..255. Transparency is 0..100: zero is opaque, 100 is invisible. from_hex accepts only #RRGGBB or #RRGGBBAA; the alpha byte uses 255 for opaque. to_hex() returns uppercase #RRGGBBAA for saving or passing to another application.

from_gradient(value, lower, upper, lower_color, upper_color) is called with keyword bounds/colors as shown above. It interpolates RGBA components and clamps values outside the bounds. Bounds must be finite and strictly increasing; endpoint colors must be Color values. Convert NumPy/indicator scalars with float(...) after checking validity.

Use self.plotting.line(self.average, color=faded) for a static color, or compute a scalar gradient in next() and pass it to self.plotting.bar_color(...). Color arrays are not accepted. For a user setting, declare inputs.color(colors.TEAL, title="Line color"); the runtime value is a Color.

Color

Color(red, green, blue, alpha=255)

Immutable color compared by all four channels; alpha means opacity, with no implicit equality to strings.

Attribute Type Meaning
red int Red byte from 0 to 255.
green int Green byte from 0 to 255.
blue int Blue byte from 0 to 255.
alpha int Opacity byte: 0 is transparent and 255 is opaque.

Attributes

Attribute Meaning
transparency 0–100 transparency derived from quantized alpha; it may differ from the original percentage.

Color.from_hex

Color.from_hex(value)

Parse six- or eight-digit hexadecimal colors; append FF for six digits.

Color.rgb

Color.rgb(red, green, blue, *, transparency=0)

Construct from integer RGB and 0–100 transparency; 0 is opaque, 100 is fully transparent.

Color.with_transparency

Color.with_transparency(transparency)

Replace alpha using 0–100 transparency and return a new value; do not modify or multiply the original color.

Color.to_hex

Color.to_hex()

Return uppercase #RRGGBBAA; fully transparent colors retain RGB and do not become None.

Color.from_gradient

Color.from_gradient(value, *, lower, upper, lower_color, upper_color)

Linearly interpolate a scalar between unpremultiplied RGBA bytes at both endpoints; clamp outside the interval.

Named presets

The colors module exposes these Color constants. Use them directly or call .with_transparency(...).

Preset RGBA
colors.AQUA #00BCD4FF
colors.BLACK #363A45FF
colors.BLUE #2196F3FF
colors.FUCHSIA #E040FBFF
colors.GRAY #787B86FF
colors.GREEN #4CAF50FF
colors.LIME #00E676FF
colors.MAROON #880E4FFF
colors.NAVY #311B92FF
colors.OLIVE #808000FF
colors.ORANGE #FF9800FF
colors.PURPLE #9C27B0FF
colors.RED #F23645FF
colors.SILVER #B2B5BEFF
colors.TEAL #089981FF
colors.WHITE #FFFFFFFF
colors.YELLOW #FDD835FF

Plotting · Inputs · User manual