# Colors



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

[Color](https://docs.openbacktesting.com/reference/colors/#openbacktesting-color)

```python
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.

<a id="openbacktesting-color"></a>



## Color

```python
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 |
| --- | --- | --- |
| <a id="openbacktesting-color-red"></a>`red` | `int` | Red byte from 0 to 255. |
| <a id="openbacktesting-color-green"></a>`green` | `int` | Green byte from 0 to 255. |
| <a id="openbacktesting-color-blue"></a>`blue` | `int` | Blue byte from 0 to 255. |
| <a id="openbacktesting-color-alpha"></a>`alpha` | `int` | Opacity byte: 0 is transparent and 255 is opaque. |

### Attributes

| Attribute | Meaning |
| --- | --- |
| <a id="openbacktesting-color-transparency"></a>`transparency` | 0–100 transparency derived from quantized alpha; it may differ from the original percentage. |

<a id="openbacktesting-color-from-hex"></a>



### Color.from_hex

```python
Color.from_hex(value)
```

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

<a id="openbacktesting-color-rgb"></a>



### Color.rgb

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

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

<a id="openbacktesting-color-with-transparency"></a>



### Color.with_transparency

```python
Color.with_transparency(transparency)
```

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

<a id="openbacktesting-color-to-hex"></a>



### Color.to_hex

```python
Color.to_hex()
```

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

<a id="openbacktesting-color-from-gradient"></a>



### Color.from_gradient

```python
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 |
| --- | --- |
| <a id="openbacktesting-colors-aqua"></a>`colors.AQUA` | `#00BCD4FF` |
| <a id="openbacktesting-colors-black"></a>`colors.BLACK` | `#363A45FF` |
| <a id="openbacktesting-colors-blue"></a>`colors.BLUE` | `#2196F3FF` |
| <a id="openbacktesting-colors-fuchsia"></a>`colors.FUCHSIA` | `#E040FBFF` |
| <a id="openbacktesting-colors-gray"></a>`colors.GRAY` | `#787B86FF` |
| <a id="openbacktesting-colors-green"></a>`colors.GREEN` | `#4CAF50FF` |
| <a id="openbacktesting-colors-lime"></a>`colors.LIME` | `#00E676FF` |
| <a id="openbacktesting-colors-maroon"></a>`colors.MAROON` | `#880E4FFF` |
| <a id="openbacktesting-colors-navy"></a>`colors.NAVY` | `#311B92FF` |
| <a id="openbacktesting-colors-olive"></a>`colors.OLIVE` | `#808000FF` |
| <a id="openbacktesting-colors-orange"></a>`colors.ORANGE` | `#FF9800FF` |
| <a id="openbacktesting-colors-purple"></a>`colors.PURPLE` | `#9C27B0FF` |
| <a id="openbacktesting-colors-red"></a>`colors.RED` | `#F23645FF` |
| <a id="openbacktesting-colors-silver"></a>`colors.SILVER` | `#B2B5BEFF` |
| <a id="openbacktesting-colors-teal"></a>`colors.TEAL` | `#089981FF` |
| <a id="openbacktesting-colors-white"></a>`colors.WHITE` | `#FFFFFFFF` |
| <a id="openbacktesting-colors-yellow"></a>`colors.YELLOW` | `#FDD835FF` |

[Plotting](https://docs.openbacktesting.com/reference/plotting/index.md) · [Inputs](https://docs.openbacktesting.com/reference/inputs/index.md) · [User manual](https://docs.openbacktesting.com/index.md)
