Working with Colour

It’s useful to be able to transform colour specifiers into hue, saturation, brightness and kelvin values. These are used by LIFX devices to change how they look.

Photons supports all the colour formats used by the LIFX HTTP API as explained in the ColourParser below.

class photons_control.colour.ColourParser

This knows how to convert valid colour specifiers into a SetWaveformOptional you can send to a device.

A valid colour specifier is a combination of any of the following components:

A valid colour name

'blue' 'cyan' 'green' 'orange' 'pink' 'purple' 'red' 'white' 'yellow'

random_colour

The string "random" will randomly choose hsbk values

kelvin

"kelvin:3500" will set kelvin to 3500.

brightness

"brightness:0.5" will set the device to half brightness.

saturation

"saturation:0.5" will set the device to half saturation. 0 saturation is white, and 1 saturation is colour.

hue

"hue:200" will set the device to a hue value of 200, which in this case is a blue.

hex

"hex:#00aabb" or #00aabb will turn that hex value into the appropriate hsbk values. In this case #00aabb transforms into a a light blue.

rgb

"rgb:200,100,120" will take red, green, blue values and convert them. In this example, it’s a light red.

You can use the following classmethods:

classmethod hsbk(components, overrides=None)

Return (h, s, b, k) given a list of colour components

Take into account hue, saturation, brightness and kelvin keys in overrides if provided.

from photons_control.colour import ColourParser


h, s, b, k = ColourParser.hsbk("green")
classmethod msg(components, overrides=None)

Create a SetWaveformOptional message that may be used to change the state of a device to what has been specified.

from photons_control.colour import ColourParser


async def my_action(target, reference):
    msg = ColourParser.msg("green")
    await target.send(msg, reference)
class photons_control.colour.Effects

This has the logic used by the ColourParser to create waveform effects on your devices.

You use them by giving the effect option when you use the ColourParser and any of the extra options used by the effect.

For example:

from photons_control.colour import ColourParser


async def my_action(target, reference):
    msg = ColourParser.msg("red", {"effect": "pulse", "cycles": 2})
    await target.send(msg, refernece)

or from the command line:

lifx lan:transform -- '{"color": "red", "effect": "pulse", "cycles": 2}'
pulse(cycles=1, duty_cycle=0.5, transient=1, period=1.0, skew_ratio=<class 'delfick_project.norms.spec_base.NotSpecified'>, **kwargs)

Options to make the light(s) pulse color and then back to its original color

sine(cycles=1, period=1.0, peak=0.5, transient=1, skew_ratio=<class 'delfick_project.norms.spec_base.NotSpecified'>, **kwargs)

Options to make the light(s) transition to color and back in a smooth sine wave

half_sine(cycles=1, period=1.0, transient=1, **kwargs)

Options to make the light(s) transition to color smoothly, then immediately back to its original color

triangle(cycles=1, period=1.0, peak=0.5, transient=1, skew_ratio=<class 'delfick_project.norms.spec_base.NotSpecified'>, **kwargs)

Options to make the light(s) transition to color linearly and back

saw(cycles=1, period=1.0, transient=1, **kwargs)

Options to make the light(s) transition to color linearly, then instantly back

breathe(cycles=1, period=1, peak=0.5, transient=1, skew_ratio=<class 'delfick_project.norms.spec_base.NotSpecified'>, **kwargs)

Options to make the light(s) transition to color and back in a smooth sine wave

Note that is an alias to the sine effect.

photons_control.colour.make_hsbk(specifier)

Return {"hue", "saturation", "brightness", "kelvin"} dictionary for this specifier.

If it’s a string, use photons_control.colour.ColourParser.hsbk()

If it’s a list, then take h, s, b, k from the list and default to 0, 0, 1, 3500, the list can be 0 to 4 items long.

If it’s a dictionary, get hue, saturation, brightness, kelvin from it and default them to 0, 0, 1, 3500.

photons_control.colour.make_hsbks(colors, overrides=None)

Colors must be an array of [[specifier, length], ...] and this function will yield {"hue": <hue>, "saturation": <saturation>, "brightness": <brightness>, "kelvin": <kelvin} such that we get a flat list of these hsbk values.

We use photons_control.colour.make_hsbk() with each specifier.