Color

Showcase panel 02: fill, stroke and tint, HSB, hex, named colours.

Showcase panel 02: fill, stroke and tint, HSB, hex, named colours.
Try it live: run uv run python examples/showcase.py, then hover panel 02, or see the showcase.

kiln.Color holds red, green, blue and alpha, each from 0 to 255. Anywhere you could give fill, stroke, tint or background three numbers, you can give a Color.

Making colours

import kiln
from kiln import Color

def draw():
    kiln.background(Color(24, 26, 38))
    kiln.fill(Color(255, 120, 80))
    kiln.circle(110, 150, 70)
    kiln.fill(Color(80, 200, 255, 160))     # with alpha
    kiln.circle(190, 150, 70)
    kiln.fill(Color(230))                    # a gray
    kiln.circle(310, 150, 40)

show(draw)

Fill and stroke colours

A shape can have two colours: fill fills it and stroke outlines it. Each takes any colour form, alpha included, so a see-through fill can keep a crisp, solid edge. stroke(None) drops the outline again, and fill(None) the fill. Pictures, Fbos and meshes have no fill or stroke: they take the third colour, tint.

import math
import kiln
from kiln import Color

def draw():
    kiln.background(Color(24, 26, 38))
    for i, hue in enumerate((250, 85, 160)):
        a = i / 3 * math.tau - math.pi / 2
        kiln.fill(Color.from_hsb(hue, 210, 255, 90))      # translucent fill
        kiln.stroke(Color.from_hsb(hue, 120, 255), width=4)    # solid outline
        kiln.circle(200 + 48 * math.cos(a), 160 + 48 * math.sin(a), 82)
    kiln.stroke(None)

show(draw, pixel_ratio=2)

Named colours

The usual suspects are class attributes: Color.WHITE, Color.RED, Color.ORANGE, Color.TEAL, Color.SKY_BLUE, and twenty-five more (30 in all). fill("teal") takes the names as strings too.

import kiln
from kiln import Color

names = ["RED", "ORANGE", "GOLD", "LIME", "TEAL", "SKY_BLUE", "INDIGO", "PINK"]

def draw():
    kiln.background(Color.BLACK)
    for i, name in enumerate(names):
        kiln.fill(getattr(Color, name))
        kiln.circle(30 + i * 48, 150, 20)

show(draw)

Hue, saturation, brightness

HSB makes rainbows easy: change the hue and nothing else. Kiln’s hue runs 0 to 255 around the wheel.

import math
import kiln
from kiln import Color

def draw():
    kiln.background(18)
    for i in range(36):
        a = i / 36 * math.tau
        kiln.fill(Color.from_hsb(i / 36 * 255, 220, 255))
        kiln.circle(200 + 100 * math.cos(a), 150 + 100 * math.sin(a), 18)
        kiln.fill(Color.from_hsb(i / 36 * 255, 120, 255))
        kiln.circle(200 + 55 * math.cos(a), 150 + 55 * math.sin(a), 9)

show(draw)

A colour’s hue, saturation and brightness are properties: read them, or assign to change the colour in place (c.hue = 128).

Gradients with lerp

a.lerp(b, 0.5) returns the colour halfway from a to b, as a new Color; neither colour changes.

import kiln
from kiln import Color

start, end = Color(255, 90, 70), Color(60, 80, 255)

def draw():
    kiln.background(18)
    for i in range(20):
        kiln.fill(start.lerp(end, i / 19))
        kiln.circle(30 + i * 18, 150, 14)

show(draw)

Hex

Color.from_hex(0xRRGGBB) and the hex property convert to and from hex, and every colour argument takes a "#rrggbb" string directly.

import kiln
from kiln import Color

def draw():
    kiln.background(Color.from_hex(0x14161F))
    kiln.fill("#ff6b52")
    kiln.circle(140, 150, 60)
    kiln.fill(Color.from_hex(0x52C7FF, 180))
    kiln.circle(230, 150, 60)

show(draw)
print(hex(Color(255, 107, 82).hex))

0xff6b52

Try this

A colour wheel that turns, each ring a slightly different hue, brightness fading toward the middle.

import math
import kiln
from kiln import Color

class Wheel(kiln.App):
    def setup(self):
        self.t = 0.0

    def update(self):
        self.t += 0.04

    def draw(self):
        kiln.background(Color.from_hex(0x10121C))
        for ring in range(6):
            for i in range(24):
                a = i / 24 * math.tau + self.t * (1 if ring % 2 else -1)
                r = 25 + ring * 22
                hue = (i / 24 * 255 + ring * 30 + self.t * 40) % 255
                kiln.fill(Color.from_hsb(hue, 200, 120 + ring * 27))
                kiln.circle(200 + r * math.cos(a), 150 + r * math.sin(a), 5 + ring)

show_video(Wheel(), frames=90, fps=30)