Skip to content

Colors

Colors is a module which contains class definitions for Color and Pixel as well as helper functions for converting colors between formats

I appologise to all british programmers who spell color as colour, but within the programming world we spell it color. This will be the cause of 90% of your bugs if you're not used to programming with the color spelling

Color(r, g, b)

A class representing a color

r property

Red component 0-255

g property

Green component 0-255

b property

Blue component 0-255

example
myColor = Color(255, 100, 120)
print(myColor.r) # 255
print(myColor.g) # 100
print(myColor.b) # 120

rgb(r, g, b) staticmethod

An alias for the constructor, values between 0 and 255

example
red = Color.rgb(255, 0, 0)
blue = Color.rgb(0, 255, 0)
green = Color.rgb(0, 0, 255)

hsl(hue, sat, lig) staticmethod

Get a color from hsl format, values between 0 and 1.0

hex(s) staticmethod

Get a color from a string hex code, in format "#FFFFFF"

bit_string(i) staticmethod

conver the 24bit encoded int to tuple of R, G, and B. int bitmap encoded as GGGGGGGGRRRRRRRRBBBBBBBB

random(saturation=1, lightness=0.6) staticmethod

Generate a random color. The random value is for the Hue. The saturation and lightness can be specified

different_from(color) staticmethod

Generate a random color which is different from the color passed into it, maintaining the same hue and saturation

black() staticmethod

The color black / off

red() staticmethod

The color red

orange() staticmethod

The color orange

amber() staticmethod

The color amber

yellow() staticmethod

The color yellow

lime() staticmethod

The color lime

green() staticmethod

The color green

emerald() staticmethod

The color emeral

teal() staticmethod

The color teal

cyan() staticmethod

The color cyan

sky() staticmethod

The color sky

blue() staticmethod

The color blue

indigo() staticmethod

The color indigo

violet() staticmethod

The color violet

purple() staticmethod

The color purple

fuchsia() staticmethod

The color fuchia

pink() staticmethod

The color pink

rose() staticmethod

The color rose

white() staticmethod

The color white

mix(a, b, x) staticmethod

Mix two colors together

Parameters:

Name Type Description Default
a Color

The first color

required
b Color

The second color

required
x float

the amount to mix by, 0.5 is average, 0 gives a, 1 gives b

required

to_tuple()

Returns the tuple of the R, G and B, values between 0 and 255

to_hex()

to_hex Get the hex value of the current color

Convert the current color to the hex value representing it and then return

Returns:

Name Type Description
str str

The current color in the format #RRGGBB

to_hsl()

Returns the HSL values of the color, between 0 and 1.0"

to_bit_string()

Return the color as an byte string integer, int bitmap encoded as GGGGGGGGRRRRRRRRBBBBBBBB

on()

on Set the color to on

Sets the color to the fully on state (white, RGB(255,255,255))

Examples:

my_color = Color.red() # Creates a new color that is red
my_color.on() # The color is now white (255,255,255)

off()

off Set the color to off

Sets the color to the fully off state (black, RGB(0,0,0))

Examples:

my_color = Color.red() # Creates a new color that is red
my_color.off() # The color is now black (0,0,0)

fade(n=1.1)

fade Fades a color

Fade the color slightly n

Parameters:

Name Type Description Default
n float

Controls the speed of the fade. The larger the number, the faster it will fade. Values less than 1 cause the color to get brighter to a max color of white. Defaults to 1.1.

1.1

lerp(target, n, override=False, fn=linear)

Linearly interpolate the color from its current color to the target color over n frames.

Each successive call to lerp will advance the interpolation by a frame. After n amount of calls, it will be the target color. Any change to the target or frames amount will reset the interpolation from the current color. fn provides a way to choose an interpolation method, defaults to linear

Examples:

>>> my_color = Color.red() # (255, 0, 0)
>>> my_color.lerp((0, 0, 0), 5) # (205, 0, 0)
>>> my_color.lerp((0, 0, 0), 5) # (153, 0, 0)
>>> my_color.lerp((0, 0, 0), 5) # (102, 0, 0)
>>> my_color.lerp((0, 0, 0), 5) # (51, 0, 0)
>>> my_color.lerp((0, 0, 0), 5) # (0, 0, 0)
# once reacing the target, lerp has no effect
>>> my_color.lerp((0, 0, 0), 5) # (0, 0, 0)

lerp_reset()

lerp_reset Reset to lerp step 0

This method sets the previous lerp state to the current color, and sets the step number to 0

set_lerp(target, time, override=False, fn=linear)

This resets the lerp and starts interpolation to target from current value. Successive calls will not change the target unless override is set to True. Use with cont_lerp to have the same effect as lerp()

cont_lerp()

Advanced the lerp one step.

set(c)

Set the color to another color by value

set_rgb(r, g, b)

Set the red, green and blue values of the color, values between 0 and 255

set_hsl(hue, sat, lig)

Set the color via HSL, values between 0 and 1.0

set_hex(s)

Set the color with a string hex code, in format "#FFFFFF"

set_bit_string(i)

Set the color with a string hex code, in format "#FFFFFF"

set_random(saturation=1, lightness=0.6)

Set the color to a random color. The random value is for the Hue. The saturation and lightness can be specified

set_different_from(color)

Set the color to a random color which is different from the color passed into it, maintaining the same hue and saturation

set_different_from_self()

Set the color to a random color which is different to the current color, maintaining the same hue and saturation

Pixel(id, coord, tree, color=Color.black())

Bases: Color

The pixel class extends the Color class by adding 3D coordinates to a color. All the same methods and attributes exist on a pixel so they act the same way

Coordintates are in the GIFT format so range between -1 and 1 on X and Y axis, and 0 and tree.height on the Z axis

Attributes: x: float: The x axis position y: float: The y axis position z: float: The z axis position a: float: The polar angle in radians from the x axis going clockwise when looking downward on the tree d: float: The polar distance from the Z axis (trunk)

id property

The id in the LED sequence

x property

The X coordinate, left (-1) to right (+1)

y property

The Y coordinate, front (+1) to back (-1)

z property

The Z coordinate bottom (0) to top (height())

xyz property

The tuple containing the xyz coordinates

a property

The angle clockwise from the x+ direction around the tree

d property

The distance from the center line (trunk) of the tree

distance_to(p)

Find the distance to the passed pixel example: ``` my_pixel = pixels(0) print(my_pixel.distance_to(pixels(10))) # 0.45

nearest(n)

Find the nearest n pixels from the current

example
my_pixel = pixels(0)
neighbors = my_pixel.nearest(4)
len(neighbors) # 4, nearest pixels

within(d)

Find all pixels that are within a certain radius

example
my_pixel = pixels(0)
neighbors = my_pixel.within(0.4) # pixels within 0.4 radius

int2tuple(c)

conver the 24bit encoded int to tuple of R, G, and B. int bitmap encoded as GGGGGGGGRRRRRRRRBBBBBBBB

tuple2int(t)

conver rgb to 24bit encoded int. int bitmap encoded as GGGGGGGGRRRRRRRRBBBBBBBB

tuple2hex(t)

Convert an RGB tuple to hex string

hex2tuple(h)

Convert a hex string to an RGB tuple