Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from __future__ import division, print_function
from gi.repository import Clutter
import random
from random import randint
from colorsys import rgb_to_hls, hls_to_rgb
from sys import argv
Clutter.init([])
def random_color(random_alpha=False, base_color=None, index=0):
'''Generate a random ClutterColor.
If base_color is provided then randomize one of the hsl channels of the color.
Index selects the channel to randomize.
'''
if base_color:
rgb = Clutter.Color.from_string(base_color)[1]
rgb = [float(c) / 255 for c in (rgb.red, rgb.green, rgb.blue)]
hls = list(rgb_to_hls(*rgb))
hls[index] = random.random()
rgb = [c * 255 for c in list(hls_to_rgb(*hls))]
else:
rgb = [randint(0, 255) for x in range(3)]
rgba = rgb + [randint(0, 255) if random_alpha else 255]
return Clutter.Color.new(*rgba)
class Rect(Clutter.Actor):
def __init__(self):
Clutter.Actor.__init__(self)
self.set_size(100, 100)
self.set_pivot_point(0.5, 0.5)
self.set_background_color(Clutter.color_from_string('#DC13B5')[1])
self.set_position(*(z / 2 for z in stage_size))
self.rotate()
def rotate(self):
trans = Clutter.PropertyTransition.new('rotation-angle-z')
trans.set_animatable(self)
trans.set_duration(10000)
trans.set_repeat_count(-1)
trans.set_from(0)
trans.set_to(360)
self.add_transition('rotate', trans)
trans.start()
class Cell(Clutter.Actor):
def __init__(self):
Clutter.Actor.__init__(self)
self.set_pivot_point(0.5, 0.5)
self.set_background_color(Clutter.color_from_string('#DC13B5')[1])
stage_size = 1280, 720
stage = Clutter.Stage()
stage.set_size(*stage_size)
stage.set_background_color(Clutter.color_from_string('white')[1])
grid_visible = True
rect_visible = True
def on_key_press_event(stage, event):
global grid_visible, rect_visible
if event.keyval in (Clutter.KEY_PowerOff, Clutter.KEY_Escape):
Clutter.main_quit()
if event.keyval in (Clutter.KEY_Help, Clutter.KEY_g):
if grid_visible:
grid.hide()
else:
grid.show()
grid_visible = not grid_visible
if event.keyval in (Clutter.KEY_Cancel, Clutter.KEY_r):
if rect_visible:
rect.hide()
else:
rect.show()
rect_visible = not rect_visible
stage.connect('key_press_event', on_key_press_event)
# stage.set_use_alpha(True)
try:
cell_size = int(argv[1])
except:
cell_size = 100
spacing = 5
grid = Clutter.Actor()
grid_layout = Clutter.GridLayout()
grid_layout.set_column_spacing(5)
grid_layout.set_row_spacing(5)
grid_layout.set_orientation(Clutter.Orientation.VERTICAL)
grid.set_layout_manager(grid_layout)
# grid.set_offscreen_redirect(Clutter.OffscreenRedirect.ALWAYS)
for y in range(int(stage_size[1] / (cell_size + spacing)) + 1):
for x in range(int(stage_size[0] / (cell_size + spacing)) + 1):
cell = Clutter.Actor()
cell.set_size(cell_size, cell_size)
cell.set_background_color(random_color(random_alpha=False, base_color='#C4F26D', index=0))
grid_layout.attach(cell, x, y, 1, 1)
stage.add_child(grid)
rect = Rect()
stage.add_child(rect)
stage.show()
stage.queue_relayout()
Clutter.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.