def gen_sweep_rand(n_frames, n_frames_fade, n_frames_rand, colors): """ Fades all pixels from one color to the next one following a linear curve :param n_frames: Duration between two consecutive colors :param n_frames_fade: Duration of initial fade :param n_frames_rand: Random seed for this pixel, extra duration added to n_frames to get the total duration :param colors: list of colors to sweep """ h0, s0, v0 = colors[0] num_cols = len(colors) n_frames = n_frames + n_frames_rand # This loop fades up and is also the random seed for f in xrange(n_frames_fade): yield hsv_to_rgb(h0, s0, v0*(float(f)/n_frames_fade)) while True: # Selection of the next couple of colors (col_1, col_2) for col_1 in range(num_cols): col_2 = (col_1 + 1) % num_cols h1, s1, v1 = colors[col_1] h2, s2, v2 = colors[col_2] # Linearly fading col_1 to col_2 for f in xrange(n_frames): factor_2 = float(f)/n_frames factor_1 = 1 - factor_2 yield hsv_to_rgb(h1*factor_1 + h2*factor_2, s1*factor_1 + s2*factor_2, v1*factor_1 + v2*factor_2)
def gen_sweep_rand(n_frames, n_frames_fade, n_frames_rand, colors): """ Fades all pixels from one color to the next one following a linear curve :param n_frames: Duration between two consecutive colors :param n_frames_fade: Duration of initial fade :param n_frames_rand: Random seed for this pixel, extra duration added to n_frames to get the total duration :param colors: list of colors to sweep """ h0, s0, v0 = colors[0] num_cols = len(colors) n_frames = n_frames + n_frames_rand # This loop fades up and is also the random seed for f in xrange(n_frames_fade): yield hsv_to_rgb(h0, s0, v0 * (float(f) / n_frames_fade)) while True: # Selection of the next couple of colors (col_1, col_2) for col_1 in range(num_cols): col_2 = (col_1 + 1) % num_cols h1, s1, v1 = colors[col_1] h2, s2, v2 = colors[col_2] # Linearly fading col_1 to col_2 for f in xrange(n_frames): factor_2 = float(f) / n_frames factor_1 = 1 - factor_2 yield hsv_to_rgb(h1 * factor_1 + h2 * factor_2, s1 * factor_1 + s2 * factor_2, v1 * factor_1 + v2 * factor_2)
def gen_random_flashing(n_frames, n_frames_fade, n_frames_rand, colors): """ Fades all pixels to white (saturation = 0) following an exponential curve :param n_frames: Duration of two consecutive colors :param n_frames_fade: Duration of initial fade :param n_frames_rand: Random seed for this pixel, extra duration added to n_frames to get the total duration :param colors: At least 2 HSV elements to fade """ h0, s0, v0 = colors[0] n_frames = n_frames + n_frames_rand # This loop fades up for f in xrange(n_frames_fade): yield hsv_to_rgb(h0, s0, v0 * (float(f) / n_frames_fade)) base = 1.1 # Exponential base. Higher the base is, lower the duration of fade will be def yield_exp(step): e = 1 - base**(step - n_frames / 2 + 1) # e = 1..0 return hsv_to_rgb(h0, e * s0, v0) while True: # Exponential rise for f in xrange(n_frames / 2): yield yield_exp(f) # Exponential fall for f in xrange(n_frames / 2, -1, -1): yield yield_exp(f)
def gen_random_flashing(n_frames, n_frames_fade, n_frames_rand, colors): """ Fades all pixels to white (saturation = 0) following an exponential curve :param n_frames: Duration of two consecutive colors :param n_frames_fade: Duration of initial fade :param n_frames_rand: Random seed for this pixel, extra duration added to n_frames to get the total duration :param colors: At least 2 HSV elements to fade """ h0, s0, v0 = colors[0] n_frames = n_frames + n_frames_rand # This loop fades up for f in xrange(n_frames_fade): yield hsv_to_rgb(h0, s0, v0*(float(f)/n_frames_fade)) base = 1.1 # Exponential base. Higher the base is, lower the duration of fade will be def yield_exp(step): e = 1 - base**(step - n_frames/2 +1) # e = 1..0 return hsv_to_rgb(h0, e*s0, v0) while True: # Exponential rise for f in xrange(n_frames/2): yield yield_exp(f) # Exponential fall for f in xrange(n_frames/2, -1, -1): yield yield_exp(f)
def gen_sweep_async(n_frames, n_frames_fade, n_frames_rand, colors): """ Browse the full color wheel (hue component) :param n_frames: Duration between two consecutive colors :param n_frames_fade: Unused :param n_frames_rand: Duration of fade + random seed :param colors: 1-element list containing the first color of the wheel """ h0, s0, v0 = colors[0] # This loop fades up and is also the random seed for f in xrange(n_frames_rand): yield hsv_to_rgb(h0, s0, v0 * (float(f) / n_frames_rand)) # Infinite loop on color sequence while True: for f in xrange(n_frames): yield hsv_to_rgb((h0 - float(f) / n_frames) % 1., s0, v0)
def gen_sweep_async(n_frames, n_frames_fade, n_frames_rand, colors): """ Browse the full color wheel (hue component) :param n_frames: Duration between two consecutive colors :param n_frames_fade: Unused :param n_frames_rand: Duration of fade + random seed :param colors: 1-element list containing the first color of the wheel """ h0, s0, v0 = colors[0] # This loop fades up and is also the random seed for f in xrange(n_frames_rand): yield hsv_to_rgb(h0, s0, v0*(float(f)/n_frames_rand)) # Infinite loop on color sequence while True: for f in xrange(n_frames): yield hsv_to_rgb((h0-float(f)/n_frames) % 1., s0, v0)
def __init__(self, model, height, width, num_bins, num_bands, vertical=True): self.model = model self.height = height self.width = width self.num_bands = num_bands self.num_bins = num_bins self.vertical = vertical self.colors = [hsv_to_rgb((float(c)/self.num_bands, 1., 1.)) for c in range(self.num_bands)] # A window stores the last len_window samples to scale the height of the spectrum self.max = 150 # Empiric value of an average sum to start with self.window = deque() self.len_window = 50
def yield_exp(step): e = 1 - base**(step - n_frames / 2 + 1) # e = 1..0 return hsv_to_rgb(h0, e * s0, v0)
def event(self): action = False if self.args.autostart and self.state == 'init': self.first_spawns() action = True events = self.arbalet.events.get() button_event = len([e for e in events if e.type == pygame.JOYBUTTONDOWN]) != 0 hat_events = [e.value for e in events if e.type == pygame.JOYHATMOTION] for e in hat_events: if e[1] == 1: self.joy_dir = 'up' elif e[1] == -1: self.joy_dir = 'down' elif e[0] == 1: self.joy_dir = 'right' elif e[0] == -1: self.joy_dir = 'left' elif e == (0, 0): self.joy_dir = '' keys = pygame.key.get_pressed() if(self.ai): dir_list = ['up', 'down', 'left', 'right'] direction = dir_list[random.randint(0,len(dir_list)-1)] steps = 1 #random.randint(0,10) else: direction ='' steps = 1 for it in range(steps): #time.sleep(0.05) #print direction, it, steps if keys[K_UP] or self.joy_dir == 'up' or direction == 'up': self.offset_y = (self.offset_y - 1) % size[1] self.vector = 'up' action = True elif keys[K_DOWN] or self.joy_dir == 'down' or direction == 'down': self.offset_y = (self.offset_y + 1) % size[1] self.vector = 'down' action = True elif keys[K_RIGHT] or self.joy_dir == 'right' or direction == 'right': self.offset_x = (self.offset_x + 1) % size[0] self.vector = 'right' action = True elif keys[K_LEFT] or self.joy_dir == 'left' or direction == 'left': self.offset_x = (self.offset_x - 1) % size[0] self.vector = 'left' action = True elif keys[K_SPACE] or button_event: if self.state == 'init': self.first_spawns() action = True if keys[K_ESCAPE]: self.state = 'end' return if not action: self.speed = max(self.speed/1.2, MIN_SPEED) self.fade = max(self.fade/1.2, 1) if self.speed!=MIN_SPEED: if self.vector=='up': self.offset_y = (self.offset_y - 1) % size[1] elif self.vector=='down': self.offset_y = (self.offset_y + 1) % size[1] elif self.vector=='left': self.offset_x = (self.offset_x - 1) % size[0] elif self.vector=='right': self.offset_x = (self.offset_x + 1) % size[0] action = True if self.state == 'init': return elif self.state == 'running' and action: with self.model: # Changing color brightness = max(rgb_to_hsv(self.color)[1] - 1./self.fade, 0) self.color = hsv_to_rgb(rgb_to_hsv(self.color)[0], brightness, rgb_to_hsv(self.color)[2]) collide_spawn = None # if we are on a spot x = (self.offset_x + self.x)%size[0] y = (self.offset_y + self.y)%size[1] for spawn in self.spawns: if (x, y) == (spawn.x, spawn.y): collide_spawn = spawn if collide_spawn is not None: spawn = collide_spawn self.last_spawn_color = spawn.color #print self.last_spawn_color self.base_color = spawn.color r = int(round(spawn.color[0] * 255)) g = int(round(spawn.color[1] * 255)) b = int(round(spawn.color[2] * 255)) self.image.putpixel(((self.offset_x + self.x) % size[0], (self.offset_y + self.y) % size[1]), (r, g, b)) self.color = self.base_color # play sound sound = spawn.get_sound(self.color_level[spawn.color_id]) sound.play() sound.fadeout(3000) # draw spawn points = spawn.get_points(self.color_level[spawn.color_id], size) for point in points: self.image.putpixel((point[0], point[1]),(r, g, b)) self.spawns.remove(spawn) # generate two new spawns random.seed() self.spawn_source([random.randint(floor(self.x-self.width/2)+1,floor(self.x+self.width/2)-1), random.randint(floor(self.y-self.height/2)+1,floor(self.y + self.height/2)-1)],parent=spawn) self.spawn_source([random.randint(0, size[0]-1), random.randint(0, size[1]-1)]) # delete two old spawns if too many spawns #print len(self.spawns), MAX_SPAWNS if len(self.spawns)>=MAX_SPAWNS: spawns_to_remove = 2 indices_to_remove = [] iterations = 0 while(len(indices_to_remove) <2 and iterations<MAX_SPAWNS): tmp_index = random.randint(0,floor(MAX_SPAWNS/2)) iterations += 1 tmp_spawn = self.spawns[tmp_index] if (tmp_spawn.x<=(self.x-self.width/2) or tmp_spawn.x>=(self.x + self.width/2) or tmp_spawn.y <= (self.y - self.height/2) or tmp_spawn.y >= (self.y + self.height/2)): #self.spawns.remove(tmp_index) if not tmp_index in indices_to_remove : indices_to_remove.append(tmp_index) spawns_to_remove -=1 #print indices_to_remove for index in indices_to_remove: self.spawns.remove(self.spawns[index]) indices_to_remove = [] #print len(self.spawns) # get information about speed and fading self.speed = spawn.get_speed(self.color_level[spawn.color_id]) self.fade = spawn.get_fading(self.color_level[spawn.color_id]) self.color_level[spawn.color_id] = min(self.color_level[spawn.color_id]+1, 3) # else we tranform the current color else: # transforming player color to PIL RGB r = int(round(self.color[0] * 255)) g = int(round(self.color[1] * 255)) b = int(round(self.color[2] * 255)) # we only print on the image if we still have color if not (rgb_to_hsv(self.color)[0] == 0.): actual_color = self.image.getpixel( ((self.offset_x + self.x) % size[0], (self.offset_y + self.y) % size[1])) if actual_color != (255, 255, 255): r, g, b = self.mix_color((r, g, b), (actual_color[0], actual_color[1], actual_color[2])) self.image.putpixel(((self.offset_x + self.x) % size[0], (self.offset_y + self.y) % size[1]), (r, g, b)) # random diffusion on the sides with bluuuueee ! if self.last_spawn_color == [0.047058823529411764, 0.14901960784313725,0.7019607843137254] and self.color_level[2]==3: # blue is hard-coded #print "fuuuuuusioooon" tmp_brightness = max(rgb_to_hsv(self.color)[1] -random.randint(1.,8.)/self.fade, 0) tmp_color = hsv_to_rgb(rgb_to_hsv(self.color)[0], tmp_brightness, rgb_to_hsv(self.color)[2]) r = int(round(tmp_color[0] * 255)) g = int(round(tmp_color[1] * 255)) b = int(round(tmp_color[2] * 255)) # uncomment to smoothen the sides of the blue track #r, g, b = self.mix_color((r, g, b), (actual_color[0], actual_color[1], actual_color[2])) if self.vector == 'up': self.image.putpixel(((self.offset_x + self.x+1) % size[0], (self.offset_y + self.y+1) % size[1]), (r, g, b)) self.image.putpixel(((self.offset_x + self.x-1) % size[0], (self.offset_y + self.y+1) % size[1]), (r, g, b)) elif self.vector == 'down': self.image.putpixel(((self.offset_x + self.x+1) % size[0], (self.offset_y + self.y-1) % size[1]), (r, g, b)) self.image.putpixel(((self.offset_x + self.x-1) % size[0], (self.offset_y + self.y-1) % size[1]), (r, g, b)) elif self.vector == 'left': self.image.putpixel(((self.offset_x + self.x+1) % size[0], (self.offset_y + self.y+1) % size[1]), (r, g, b)) self.image.putpixel(((self.offset_x + self.x+1) % size[0], (self.offset_y + self.y-1) % size[1]), (r, g, b)) elif self.vector == 'right': self.image.putpixel(((self.offset_x + self.x-1) % size[0], (self.offset_y + self.y+1) % size[1]), (r, g, b)) self.image.putpixel(((self.offset_x + self.x-1) % size[0], (self.offset_y + self.y-1) % size[1]), (r, g, b)) if self.last_spawn_color == [0.06666666666666667, 0.6470588235294118, 0.2784313725490196] and self.color_level[1]==1: tmp_brightness = max(rgb_to_hsv(self.color)[1] -1./self.fade, 0) tmp_color = hsv_to_rgb(rgb_to_hsv(self.color)[0], tmp_brightness, rgb_to_hsv(self.color)[2]) r = int(round(tmp_color[0] * 255)) g = int(round(tmp_color[1] * 255)) b = int(round(tmp_color[2] * 255)) if self.vector == 'up': if ((self.y + self.offset_y)%2 == 0): self.image.putpixel(((self.offset_x + self.x+1) % size[0], (self.offset_y + self.y+1) % size[1]), (r, g, b)) else: self.image.putpixel(((self.offset_x + self.x-1) % size[0], (self.offset_y + self.y+1) % size[1]), (r, g, b)) elif self.vector == 'down': if ((self.y + self.offset_y)%2 == 0): self.image.putpixel(((self.offset_x + self.x+1) % size[0], (self.offset_y + self.y-1) % size[1]), (r, g, b)) else : self.image.putpixel(((self.offset_x + self.x-1) % size[0], (self.offset_y + self.y-1) % size[1]), (r, g, b)) elif self.vector == 'left': if ((self.x + self.offset_x)%2 == 0): self.image.putpixel(((self.offset_x + self.x+1) % size[0], (self.offset_y + self.y+1) % size[1]), (r, g, b)) else : self.image.putpixel(((self.offset_x + self.x+1) % size[0], (self.offset_y + self.y-1) % size[1]), (r, g, b)) elif self.vector == 'right': if ((self.x + self.offset_x)%2 ==0): self.image.putpixel(((self.offset_x + self.x-1) % size[0], (self.offset_y + self.y+1) % size[1]), (r, g, b)) else : self.image.putpixel(((self.offset_x + self.x-1) % size[0], (self.offset_y + self.y-1) % size[1]), (r, g, b)) else: self.color=(1.0, 1.0, 1.0) for i in range(len(self.color_level)): self.color_level[i] = 0 self.speed = max(self.speed / 1.2, MIN_SPEED) # finaly, we draw the grid self.draw_grid()
def yield_exp(step): e = 1 - base**(step - n_frames/2 +1) # e = 1..0 return hsv_to_rgb(h0, e*s0, v0)