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
rgb(r, g, b)
staticmethod
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
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()
off()
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
set_black()
set_black Sets the color to black
set_red()
set_red Sets the color to red
set_orange()
set_orange Sets the color to orange
set_amber()
set_amber Sets the color to amber
set_yellow()
set_yellow Sets the color to yellow
set_lime()
set_lime Sets the color to lime
set_green()
set_green Sets the color to green
set_emerald()
set_emerald Sets the color to emerald
set_teal()
set_teal Sets the color to teal
set_cyan()
set_cyan Sets the color to cyan
set_sky()
set_sky Sets the color to sky
set_blue()
set_blue Sets the color to blue
set_indigo()
set_indigo Sets the color to indigo
set_violet()
set_violet Sets the color to violet
set_purple()
set_purple Sets the color to purple
set_fuchsia()
set_fuchsia Sets the color to fuchsia
set_pink()
set_pink Sets the color to pink
set_rose()
set_rose Sets the color to rose
set_white()
set_white Sets the color to white
Pixel(id, 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)
x
property
x Get the x coordinate of the Pixel
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
the x coordinate |
y
property
y Get the y coordinate of the Pixel
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
the y coordinate |
z
property
z Get the z coordinate of the Pixel
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
the z coordinate |
xyz
property
xyz Get the location of the pixel
Combines the x, y and z coordinates into a single tuple
Returns:
| Type | Description |
|---|---|
tuple[float, float, float]
|
tuple[float, float, float]: a tuple where in order you get: x, y, z as floats |
a
property
a Return the polar angle for this Pixel
The angle is measured counterclockwise from the polar axis
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The polar angle in radians from the x axis going clockwise when looking downward on the tree |
d
property
d Return the distance to the pixel from the origin
The distance from the pole to the pixel
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The polar distance from the Z axis (trunk) |
id
property
The id in the LED sequence
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)
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