def draw_safe(self, display, y): x, y = self.coord(y) pygame.draw.rect(display, THECOLORS.get('lightpink'), ((x - self.safety_border[0]) * ZOOM, (y - self.safety_border[1]) * ZOOM, (self.size[0] + self.safety_border[0] + 1) * ZOOM, (self.size[1] + self.safety_border[1] + 1) * ZOOM))
def printcolornames(out = None): from pygame.colordict import THECOLORS as d keys = d.keys() keys.sort() width = max([len(x) for x in keys]) for k in keys: print >> out, k.ljust(width), '%3d %3d %3d' % d[k][:3]
def test_color__name_str_arg_from_colordict(self): """Ensures Color objects can be created using str names from the THECOLORS dict.""" for name, values in THECOLORS.items(): color = pygame.Color(name) self.assertEqual(color.r, values[0]) self.assertEqual(color.g, values[1]) self.assertEqual(color.b, values[2]) self.assertEqual(color.a, values[3])
def render(self): self.DISPLAY.fill(THECOLORS.get('white')) pygame.draw.rect(self.DISPLAY, THECOLORS.get('grey'), (0, 0, ROAD_W * ZOOM, ROAD_H * ZOOM)) for v in self.ov: v.draw_safe(self.DISPLAY, self.car.pos[1]) self.car.draw_safe(self.DISPLAY, self.car.pos[1]) for v in self.ov: v.draw(self.DISPLAY, self.car.pos[1]) self.car.draw(self.DISPLAY, self.car.pos[1]) pygame.display.update() bluepx = pygame.surfarray.pixels_blue(self.DISPLAY) bluepx = np.array(bluepx).astype(int) redpx = pygame.surfarray.pixels_red(self.DISPLAY) redpx = np.array(redpx).astype(int) greenpx = pygame.surfarray.pixels_green(self.DISPLAY) greenpx = np.array(greenpx).astype(int) greenpx[greenpx != 255] = 0 greenpx[greenpx == 255] = 1 bluepx[bluepx != 255] = 0 bluepx[bluepx == 255] = 1 redpx[redpx != 255] = 0 redpx[redpx == 255] = 1 self.road.pole = 2 * bluepx[::ZOOM, :: ZOOM] + 2 * greenpx[::ZOOM, :: ZOOM] + redpx[::ZOOM, :: ZOOM] - 1 quit_ = False for event in pygame.event.get(): if event.type == QUIT: quit_ = True return quit_
def draw_letter(self, index, letter, line = 0): letter_surface = self.font.render(letter, True, random.choice(COLORS.values())) letter_x = self.font_size / 2 * index if letter in ['i', 'l', '1', ')']: letter_x += 5 elif letter in ['(']: letter_x += 10 target_rect = pygame.locals.Rect(letter_x, (letter_surface.get_rect().height + 5) * line , letter_surface.get_rect().width, letter_surface.get_rect().height) self.surface.blit(letter_surface, target_rect) #------------------------------FUNCTIONS---------------------------------------#
def simplified_color_table(): """De-dupe the colour table.""" by_hex = {} for name, color in THECOLORS.items(): if name[-1].isdigit(): continue html = html_color(color) by_hex.setdefault(html, []).append(name) return { ' / '.join(sorted(names)): THECOLORS[names[0]] for names in by_hex.values() }
def parse_color(c): from pygame.colordict import THECOLORS c = c.strip().lower() if THECOLORS.has_key(c): return THECOLORS[c][:3] try: r, g, b = map(lambda s: int(s), c.split(',', 2)) except ValueError: import sys print >> sys.stderr, 'Invalid color string:', c return None return r, g, b
from core.sim_engine import gui_get from core.world_patch_block import Block, Patch, World def is_acceptable_color(rgb): """ Require reasonably bright colors (sum_rgb >= 150) for which r, g, and b are not too close to each other, i.e., not too close to gray. The numbers 150 and 100 are arbitrary. """ sum_rgb = sum(rgb) avg_rgb = sum_rgb / 3 return avg_rgb >= 160 and sum(abs(avg_rgb - x) for x in rgb) > 100 # These are colors defined by pygame that satisfy is_acceptable_color() above. PYGAME_COLORS = [(name, rgba[:3]) for (name, rgba) in THECOLORS.items() if is_acceptable_color(rgba[:3])] # These are NetLogo primary colors -- more or less. NETLOGO_PRIMARY_COLORS = [(color_name, Color(color_name)) for color_name in [ 'gray', 'red', 'orange', 'brown', 'yellow', 'green', 'limegreen', 'turquoise', 'cyan', 'skyblue3', 'blue', 'violet', 'magenta', 'pink' ]] SQRT_2 = sqrt(2) class Agent(Block): color_palette = choice([NETLOGO_PRIMARY_COLORS, PYGAME_COLORS])
by_hex = {} for name, color in THECOLORS.items(): if name[-1].isdigit(): continue html = html_color(color) by_hex.setdefault(html, []).append(name) return { ' / '.join(sorted(names)): THECOLORS[names[0]] for names in by_hex.values() } SIMPLE_COLORS = simplified_color_table() omitted_colors = sorted(THECOLORS.keys() - SIMPLE_COLORS.keys()) some_color = omitted_colors[0] def gen_table(f): colors = list(SIMPLE_COLORS.items()) colors.sort(key=color_key) last_hue = None for name, color in colors: html = html_color(color) arr = np.array(color) / 255 rgb = arr[:3] a = arr[3]
def lookup_debug_color(number): """Choose a unique colour for this number, to aid debugging""" return Color(list(THECOLORS.keys())[number])
import pygame,sys,random from pygame.colordict import THECOLORS pygame.init() screen = pygame.display.set_mode([640,380]) screen.fill([255,255,255]) for i in range(100): width = random.randint(0,250) height= random.randint(0,100) top = random.randint(0,400) left = random.randint(0,500) color_name = random.choice(list(THECOLORS.keys())) color = THECOLORS[color_name] line_width = random.randint(0,3) pygame.draw.rect(screen,color,[left,top,width,height],line_width) pygame.display.flip() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit()
def __init__(self, pos, size, v, color='blue'): self.pos = np.array(pos) self.size = size self.v = v self.safety_border = (1, 1) self.color = THECOLORS.get(color)
import pygame from pygame.locals import * from pprint import pprint as pp import math import random import os import sys SCREEN = pygame.Rect(0, 0, 640, 480) # pull colors in to module, uppercased from pygame.colordict import THECOLORS globals().update(dict((k.upper(), v) for k, v in THECOLORS.iteritems())) del THECOLORS class Point(object): "Cartesian 2d point" def __init__(self, x, y): self.x = float(x) self.y = float(y) @property def screen(self): return (int(self.x), int(SCREEN.bottom + -self.y)) def line_intersect(x1, y1, x2, y2, x3, y3, x4, y4): # denominator d = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1))
import pygame, sys, random from pygame.colordict import THECOLORS pygame.init() screen = pygame.display.set_mode([640, 380]) screen.fill([255, 255, 255]) for i in range(100): width = random.randint(0, 250) height = random.randint(0, 100) top = random.randint(0, 400) left = random.randint(0, 500) color_name = random.choice(list(THECOLORS.keys())) color = THECOLORS[color_name] line_width = random.randint(0, 3) pygame.draw.rect(screen, color, [left, top, width, height], line_width) pygame.display.flip() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit()
import pygame from pygame.locals import * from pprint import pprint as pp import math import random import os import sys SCREEN = pygame.Rect(0,0,640,480) # pull colors in to module, uppercased from pygame.colordict import THECOLORS globals().update(dict((k.upper(),v) for k,v in THECOLORS.iteritems())) del THECOLORS class Point(object): "Cartesian 2d point" def __init__(self, x, y): self.x = float(x) self.y = float(y) @property def screen(self): return (int(self.x), int(SCREEN.bottom + -self.y)) def line_intersect(x1,y1,x2,y2,x3,y3,x4,y4): # denominator d = ((y4-y3)*(x2-x1)-(x4-x3)*(y2-y1)) if d == 0: # parallel
from core.pairs import Pixel_xy, RowCol, Velocity, XY, heading_and_speed_to_velocity from core.world_patch_block import Block, Patch, World def is_acceptable_color(rgb): """ Require reasonably bright colors (sum_rgb >= 150) for which r, g, and b are not too close to each other, i.e., not too close to gray. The numbers 150 and 100 are arbitrary. """ sum_rgb = sum(rgb) avg_rgb = sum_rgb/3 return avg_rgb >= 160 and sum(abs(avg_rgb-x) for x in rgb) > 100 # These are colors defined by pygame that satisfy is_acceptable_color() above. PYGAME_COLORS = [(name, rgba[:3]) for (name, rgba) in THECOLORS.items() if is_acceptable_color(rgba[:3])] # These are NetLogo primary colors -- more or less. NETLOGO_PRIMARY_COLORS = [(color_name, Color(color_name)) for color_name in ['gray', 'red', 'orange', 'brown', 'yellow', 'green', 'limegreen', 'turquoise', 'cyan', 'skyblue3', 'blue', 'violet', 'magenta', 'pink']] SQRT_2 = sqrt(2) class Agent(Block): color_palette = choice([NETLOGO_PRIMARY_COLORS, PYGAME_COLORS]) half_patch_pixel = pairs.Pixel_xy((HALF_PATCH_SIZE(), HALF_PATCH_SIZE()))