| title | summary | icon | publish |
|---|---|---|---|
color |
Methods for creating colours in a variety of different colourspaces and selecting colours from the built-in palette. |
palette |
true |
Represents an RGBA colour.
The color type provides methods for creating colours from RGBA, HSV, and OKLCH colour spaces. Colours can be adjusted, blended, and combined using a range of utility methods.
Regardless of how a colour is constructed, it is stored internally as a premultiplied RGBA value. color is a type of brush object and can be used interchangeably with them for different effects.
The premultiplied RGBA color as a 32 bit unsigned integer.
The following static methods create new color objects from different colour representations.
Creates a new color object from red, green, blue, and optional alpha values.
rgb(r, g, b[, a])r, g, b: Colour component values from 0 to 255a: Optional alpha value from 0 to 255
color
def update():
# draw a gradient from cyan to magenta
for x in range(0, 160):
step = (x * 255) / 160
screen.pen = color.rgb(step, 255 - step, 150)
screen.line(x, 0, x, 120)
run(update)Creates a new color object from hue, saturation, value, and optional alpha values.
HSV is not perceptually uniform, so equal changes in its values do not correspond to equal perceived colour changes. This can lead to uneven gradients and unintuitive results when adjusting saturation or brightness.
hsv(h, s, v[, a])h: Hue from 0 to 255s: Saturation from 0 to 255v: Value (brightness) from 0 to 255a: Optional alpha value from 0 to 255
color
def update():
# draw a hue gradient
for x in range(0, 160):
hue = 255 * (x / 160)
saturation = 255
value = 255
screen.pen = color.hsv(hue, saturation, value)
screen.line(x, 0, x, 120)
run(update)Creates a new color object from OKLCH parameters and an optional alpha value.
OKLCH is a perceptually uniform colour space, meaning equal changes in its values produce more consistent visual changes. This makes it better suited for colour adjustment and interpolation than HSV, which can produce uneven or unexpected results.
oklch(l, c, h[, a])l: Lightness from 0 to 255c: Chroma (saturation) from 0 to 255h: Hue from 0 to 255a: Optional alpha value from 0 to 255
color
def update():
for x in range(0, 160):
lightness = 220
chroma = 150
hue = 255 * (x / 160)
screen.pen = color.oklch(lightness, chroma, hue)
screen.line(x, 0, x, 120)
run(update)A set of 16 named colours based on the DawnBringer 16 palette.
These constants provide a convenient starting point for UI elements, sprites, and general drawing.
<style> #palette_grid { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr; grid-gap: 0.25rem; >div { aspect-ratio: 1; font-size: 0.9rem; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; display: flex; align-items: center; justify-content: center; &:nth-child(n+9) { color: var(--black); } } } </style>palette = [
color.black, color.grape, color.navy, color.grey,
color.brown, color.green, color.red, color.taupe,
color.blue, color.orange, color.smoke, color.lime,
color.latte, color.cyan, color.yellow, color.white
]
def update():
for i in range(len(palette)):
screen.pen = palette[i]
screen.circle(32 + (i * 6), 60, 20)
run(update)