Skip to content

Instantly share code, notes, and snippets.

@Hylian
Last active June 13, 2025 03:40
Show Gist options
  • Select an option

  • Save Hylian/4fd10532989184b063f563c07fdc9dc1 to your computer and use it in GitHub Desktop.

Select an option

Save Hylian/4fd10532989184b063f563c07fdc9dc1 to your computer and use it in GitHub Desktop.
Inky Frame Dither Test
from picographics import PicoGraphics, DISPLAY_INKY_FRAME_7 as DISPLAY
graphics = PicoGraphics(DISPLAY)
WIDTH, HEIGHT = graphics.get_bounds()
BLACK = 0
WHITE = 1
GREEN = 2
BLUE = 3
RED = 4
YELLOW = 5
ORANGE = 6
colors = {
'black': BLACK,
'white': WHITE,
'red': RED,
'green': GREEN,
'blue': BLUE,
'yellow': YELLOW,
'orange': ORANGE
}
color_pairs = [
('white', 'orange'), ('yellow', 'white'), ('white', 'red'), ('green', 'white'), ('white', 'blue'), ('blue', 'orange'), ('orange', 'red'),
('green', 'orange'), ('orange', 'yellow'), ('yellow', 'red'), ('red', 'green'), ('green', 'blue'),('blue', 'yellow'), ('yellow', 'green'),
('black', 'orange'), ('yellow', 'black'), ('black', 'red'), ('green', 'black'), ('black', 'blue'), ('red', 'blue'), ('black', 'white')
]
# --- Bayer Dithering Matrix ---
# A 4x4 Bayer matrix for ordered dithering.
# The values are scaled by 16.
BAYER_MATRIX_4X4 = [
[0, 8, 2, 10],
[12, 4, 14, 6],
[3, 11, 1, 9],
[15, 7, 13, 5]
]
def apply_bayer_dither(x_start, y_start, width, height, color1, color2):
"""
Applies Bayer dithering to a rectangular area of the display.
Args:
x_start (int): The starting x-coordinate of the pattern.
y_start (int): The starting y-coordinate of the pattern.
width (int): The width of the pattern.
height (int): The height of the pattern.
color1 (int): The first color (pen index).
color2 (int): The second color (pen index).
"""
for y in range(height):
for x in range(width):
# Calculate the gradient value from left (0) to right (1)
gradient = x / (width - 1)
# Get the threshold from the Bayer matrix
threshold = BAYER_MATRIX_4X4[y % 4][x % 4] / 16.0
# Determine which color to use based on the gradient and threshold
if gradient > threshold:
graphics.set_pen(color2)
else:
graphics.set_pen(color1)
# Draw the pixel on the display buffer
graphics.pixel(x_start + x, y_start + y)
def add_labels(color_pairs, cols, pattern_width, pattern_height):
"""
Adds text labels to each dithering pattern on the display.
Args:
color_pairs (list): A list of tuples, where each tuple contains
the names of the two colors in a pair.
cols (int): The number of columns in the grid.
pattern_width (int): The width of each pattern.
pattern_height (int): The height of each pattern.
"""
graphics.set_font('bitmap8')
for idx, (color1_name, color2_name) in enumerate(color_pairs):
row = idx // cols
col = idx % cols
# Calculate the position for the label
x = col * pattern_width
y = row * pattern_height
# Create the label text (e.g., "whi->ora")
label = f"{color1_name[:3]}->{color2_name[:3]}"
# Set the pen to black for the text shadow
graphics.set_pen(BLACK)
graphics.text(label, x + 2 + 20, y + 2 + 66, scale=2)
# Set the pen to white for the main text
graphics.set_pen(WHITE)
graphics.text(label, x + 1 + 20, y + 1 + 66, scale=2)
def generate_test_pattern():
"""
Generates the complete dithering test pattern on the Inky Frame display.
"""
print("Generating dithering test pattern for Inky Frame 7.3\"...")
# Clear the display to white
graphics.set_pen(WHITE)
graphics.clear()
# Define the grid layout
cols = 7
rows = 3
# Calculate the dimensions of each pattern
pattern_width = WIDTH // cols
pattern_height = HEIGHT // rows
# Generate and draw each dithered pattern
for idx, (color1_name, color2_name) in enumerate(color_pairs):
row = idx // cols
col = idx % cols
x_start = col * pattern_width
y_start = row * pattern_height
print(f"Generating pattern {idx+1}/{len(color_pairs)}: {color1_name} to {color2_name}")
apply_bayer_dither(x_start, y_start, pattern_width, pattern_height,
colors[color1_name], colors[color2_name])
# Add the labels to the patterns
add_labels(color_pairs, cols, pattern_width, pattern_height)
# Update the display to show the generated pattern
print("Updating the display...")
graphics.update()
print("Done.")
# --- Main Execution ---
if __name__ == "__main__":
generate_test_pattern()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment