tingbot / tingbot-python

🔩 Python APIs to write apps for Tingbot
Other
18 stars 9 forks source link

Add Color type #32

Open joerick opened 8 years ago

joerick commented 8 years ago

Currently colors are represented in tingbot using tuples or strings. While this is convenient, it holds us back from adding functionality around colors e.g. blending, darker/lighter methods, HSL constructors.

I propose a Color type to solve this. Common usage will be using a rgb() or rgba() constructor functions:

import tingbot
from tingbot import *

def loop():
    screen.fill(color=rgb(20,20,20))
    screen.text('Hello, world!', color=rgba(255,128,128,0.5))

tingbot.run(loop)

The syntax deliberately matches the CSS syntax for rgb and rgba. We'll also provide hsl and hsla functions for color-wheel color picking.

More advanced usage using the object directly-

import tingbot
from tingbot import *

def loop():
    bg_color = Color.from_name('green')
    screen.fill(color=bg_color)
    screen.text('Hello, world!', color=bg_color.darker())

tingbot.run(loop)

All the graphics routines that take a color parameter will continue to accept tuples/strings, and convert to color objects internally.