def tscheme_pixel(x, y, c):
"""Draw a filled box of pixels (default 1 pixel) at (x, y) in color c."""
check_type(c, scheme_stringp, 0, "pixel")
color = eval(c)
canvas = turtle.getcanvas()
w, h = canvas.winfo_width(), canvas.winfo_height()
if not hasattr(tscheme_pixel, 'image'):
_tscheme_prep()
tscheme_pixel.image = tkinter.PhotoImage(width=w, height=h)
canvas.create_image((0, 0), image=tscheme_pixel.image, state="normal")
size = tscheme_pixel.size
for dx in range(size):
for dy in range(size):
screenx, screeny = x * size + dx, h-(y * size + dy)
if 0 < screenx < w and 0 < screeny < h:
tscheme_pixel.image.put(color, (screenx, screeny))
return okay
python类color()的实例源码
def draw_line(self, pos, horizontal=False, label=None,
color='gray90', label_color='gray35', **_):
"""Draw one horizontal or vertical line with optional color and label"""
if pos is None:
return
if pos < self.zoom_area[0 + horizontal] or pos > self.zoom_area[2 + horizontal]:
return
turtle.penup()
xscale = turtle.getscreen().xscale
yscale = turtle.getscreen().yscale
if label:
font_family, font_size = self.font
turtle.color(label_color)
turtle.setposition(*self.label_pos(pos=pos, label=label, horizontal=horizontal))
turtle.write(label, align='right' if horizontal else'center',
font=(font_family, font_size))
turtle.setposition(-4 / xscale if horizontal else pos,
pos if horizontal else -4 / yscale)
turtle.pendown()
turtle.setposition(1 / xscale if horizontal else pos,
pos if horizontal else 1 / yscale)
turtle.color(color)
turtle.pendown()
turtle.setposition(self.plot_area[2] if horizontal else pos,
pos if horizontal else self.plot_area[3])
turtle.penup()
def plot_table(self, *gamma, colors=['red', 'green', 'blue'], draw_speed=16, scale_x=1):
"""Plot gamma table"""
if len(gamma) == 1 and len(gamma[0]) == 3:
gamma = gamma[0]
if all(x == gamma[0] for x in gamma):
gamma = gamma[:1]
turtle.penup()
turtle.tracer(0, 16)
turtle.speed(0)
turtle.color('black')
for color, points_y in enumerate(gamma):
if len(gamma) == len(colors):
turtle.color(colors[color])
elif len(colors) == 1:
turtle.color(colors[0])
for x, y in enumerate(points_y):
trace = x and x % draw_speed == 0
if trace:
turtle.tracer(1)
turtle.setposition(x * scale_x, y)
if trace:
turtle.tracer(0)
if x == 0:
turtle.showturtle()
turtle.pendown()
turtle.penup()
turtle.hideturtle()
turtle.update()
def run(self):
"""Test Plot class client thread"""
p = self.plot
try:
p.plot([512 for i in range(256)], draw_speed=4)
p.clear(lines=
[{'pos': i, 'label': str(i)} for i in list(range(16, 256-15, 16)) + [255]] +
[{'pos': i, 'horizontal': True, 'label': str(i)}
for i in range(64, 1024-63, 64)])
p.plot([512 for i in range(256)], draw_speed=4)
p.clear(lines=[{'pos': 128, 'label': 'Green line\nlabel', 'color': 'green'},
{'pos': 136, 'label': 'Blue\nline\nlabel', 'priority': 1,
'color': 'blue'}])
p.plot([512 for i in range(256)], draw_speed=4)
p.zoom(4, (0, -1))
p.clear(lines=[{'pos': 128, 'label': 'Green line\nlabel', 'color': 'green'},
{'pos': 136, 'label': 'Blue\nline\nlabel', 'color': 'blue'}])
p.plot([512 for i in range(256)], draw_speed=4)
p.clear()
p.zoom()
p.plot([512 for i in range(256)], draw_speed=4)
p.clear(lines=[{'pos': i} for i in range(256)])
p.plot([512 for i in range(256)], draw_speed=2)
p.clear(lines=[{'pos': 127}, {'pos':128}])
p.plot([i << 2 | i >> 6 for i in range(256)])
p.plot([i << 2 | i >> 6 for i in range(255, -1, -1)], colors=['red'])
p.zoom(4, (0, 0))
p.plot([i << 2 | i >> 6 for i in range(256)], draw_speed=2, colors=['green'])
p.zoom(2, (-1, 0))
p.plot([i << 2 | i >> 6 for i in range(256)], draw_speed=2, colors=['green'])
p.zoom()
p.enqueue(lambda: turtle.setpos(100, 512))
p.enqueue(lambda: turtle.write('test done'))
print('test done')
except PlotClosed as err:
print(err)
def tscheme_color(c):
"""Set the color to C, a string such as '"red"' or '"#ffc0c0"' (representing
hexadecimal red, green, and blue values."""
_tscheme_prep()
check_type(c, scheme_stringp, 0, "color")
turtle.color(eval(c))
return okay
def tscheme_rgb(red, green, blue):
"""Return a color from red, green, and blue values from 0 to 1."""
colors = (red, green, blue)
for x in colors:
if x < 0 or x > 1:
raise SchemeError("Illegal color intensity in " + str(colors))
scaled = tuple(int(x*255) for x in colors)
return '"#%02x%02x%02x"' % scaled
def tscheme_color(c):
"""Set the color to C, a string such as '"red"' or '"#ffc0c0"' (representing
hexadecimal red, green, and blue values."""
_tscheme_prep()
check_type(c, scheme_stringp, 0, "color")
turtle.color(eval(c))
return okay
def tscheme_color(c):
"""Set the color to C, a string such as '"red"' or '"#ffc0c0"' (representing
hexadecimal red, green, and blue values."""
_tscheme_prep()
check_type(c, scheme_stringp, 0, "color")
turtle.color(eval(c))
return okay
def run_instruction(t):
if t.data == 'change_color':
turtle.color(*t.children) # We just pass the color names as-is
elif t.data == 'movement':
name, number = t.children
{ 'f': turtle.fd,
'b': turtle.bk,
'l': turtle.lt,
'r': turtle.rt, }[name](int(number))
elif t.data == 'repeat':
count, block = t.children
for i in range(int(count)):
run_instruction(block)
elif t.data == 'fill':
turtle.begin_fill()
run_instruction(t.children[0])
turtle.end_fill()
elif t.data == 'code_block':
for cmd in t.children:
run_instruction(cmd)
else:
raise SyntaxError('Unknown instruction: %s' % t.data)