def touch_moved(self, data):
if self.dragging_waypoint:
self.w.center = data.location
return
if self.flood_filling:
if Vector(data.location).distance_to(self.touch_start) < 10:
return
else:
self.flood_fill(self.control.edit_menu['flood_fill'])
(w, h) = self.img.size
with ui.ImageContext(w,h) as ctx:
self.img.draw()
blend_mode = ui.BLEND_CLEAR if self.digging else ui.BLEND_NORMAL
ui.set_blend_mode(blend_mode)
ui.set_color('black')
(x, y) = data.location #* self.multiplier
(px, py) = data.prev_location #* self.multiplier
path = ui.Path()
path.move_to(px, py)
path.line_to(x, y)
path.line_width = 30 #* self.multiplier #if self.digging else 1
path.line_cap_style = ui.LINE_CAP_ROUND
path.stroke()
self.img = ctx.get_image()
#self.set_needs_display()
python类set_color()的实例源码
def create_zoom_frame(self):
zoomWidth = self.width / self.zoomLevels[self.zoomCurrent]
zoomHeight = self.height / self.zoomLevels[self.zoomCurrent]
# create an movable image showing the zoom area
with ui.ImageContext(320,200) as ctx:
ui.set_color('black')
line_inside = ui.Path.rect(2,2,316,196)
line_inside.line_width = 4
line_inside.stroke()
ui.set_color('white')
line_outside = ui.Path.rect(0,0,320,200)
line_outside.line_width = 4
line_outside.stroke()
zoomSquare = ctx.get_image()
zoom_frame = ui.ImageView(hidden=True)
zoom_frame.bounds = (0,0,zoomWidth,zoomHeight)
zoom_frame.center = (self.width/2, self.height/2)
zoom_frame.image = zoomSquare
self.add_subview(zoom_frame)
return zoom_frame
def redraw_canvas(self, updategrid=False):
# Gets the pixels covered by the current zoom level
zoomPixels = self.position_pixels()
# Redraw view
self.image_view.image = self.create_new_image()
with ui.ImageContext(self.width, self.height) as ctx:
for i in zoomPixels:
p = self.pixels[i]
ui.set_color(p.color)
pixel_path = ui.Path.rect(p.rect[0],p.rect[1],p.rect[2],p.rect[3])
pixel_path.fill()
pixel_path.line_width = 0.5
pixel_path.stroke()
self.image_view.image = ctx.get_image()
## Todo: insert drawing of preview window:
# Redraw grid
if updategrid == True:
self.grid_layout.image = self.draw_grid_image()
self.grid_layout.alpha = self.gridOpacity
if self.color_check.hidden is False:
self.character_colorcheck()
return True
# Flip colors
def draw_single_pixel(self, pixel):
if pixel.color != self.current_color:
if self.checkDither(pixel.position):
self.store_undo_stroke(pixel.index)
pixel.color = self.current_color
#self.pixel_path.append(pixel)
old_img = self.image_view.image
path = ui.Path.rect(*pixel.rect)
with ui.ImageContext(self.width, self.height) as ctx:
if old_img:
old_img.draw()
ui.set_color(self.current_color)
pixel_path = ui.Path.rect(*pixel.rect)
pixel_path.line_width = 0.5
pixel_path.fill()
pixel_path.stroke()
self.set_image(ctx.get_image())
def draw(self):
ui.set_color((.7,.5,.5))
if not self.vert:
path=ui.Path.rounded_rect((self.barvalue-self.barwidth/2.)*self.width,0,self.barwidth*self.width,self.height,self.height*0.1)
else:
path=ui.Path.rounded_rect(0,(self.barvalue-self.barwidth/2.)*self.height,self.width, self.barwidth*self.height,self.width*0.1)
path.fill()
ui.set_color((0.5,.5,.5))
path.stroke()
ui.set_color((0.5,0,0))
if len(self.touches)==1:
ui.set_color((0.7,0.7,0))
path.fill()
elif len(self.touches)==2:
ui.set_color((0.7,0.0,0.7))
path.fill()
def load_actual(self, burn_waypoints_in=False):
# Clear old waypoints
for wp in self.edit_view.waypoints:
self.edit_view.remove_subview(wp)
self.edit_view.waypoints = []
# Load image
img_filename = self.filename
iv = ui.ImageView(frame=self.bg.bounds)
iv.image = ui.Image(img_filename)
self.edit_view.img = snapshot(iv)
self.multiplier = self.edit_view.img.size[1]/self.edit_view.height
# Load settings
json_filename = img_filename[:-3]+'json'
if os.path.exists(json_filename):
with open(json_filename) as fp:
settings = json.load(fp)
if isinstance(settings, dict):
locations = settings['waypoint_locations']
self.bg.image = ui.Image.named(settings['bg_filename'])
self.edit_view.bg_filename = settings['bg_filename']
else:
locations = settings
self.bg.image = ui.Image.named('backgrounds/caves.jpg')
self.edit_view.bg_filename = None
for loc in locations:
wp = self.edit_view.add_waypoint()
wp.center = loc
if burn_waypoints_in:
with ui.ImageContext(self.edit_view.width, self.edit_view.height) as ctx:
self.edit_view.img.draw()
ui.set_blend_mode(ui.BLEND_CLEAR)
ui.set_color('black')
for wp in self.edit_view.waypoints:
(x,y) = wp.center
path = ui.Path.oval(x-15, y-15, 30, 30)
path.fill()
self.edit_view.img = ctx.get_image()
def draw(self):
if self.tracking:
ui.set_color('black')
ui.fill_rect(0, 0, self.width, self.height)
else:
base_color = tuple([self.color[i] for i in range(3)])
opacity_increment = 1.0/(len(self.current_move)+1) # 0.002
alpha_incremental = 1.0 - self.animate_counter*opacity_increment
if self.animate_counter > 0:
for i in range(1, self.animate_counter):
alpha_actual = max(0, alpha_incremental)
self.draw_segment(base_color + (alpha_actual,), self.current_move[i-1], self.current_move[i])
alpha_incremental += opacity_increment
if len(self.waypoints_hit_this_turn) > 0:
(hit_distance, waypoint_index) = self.waypoints_hit_this_turn[0]
if self.animate_counter >= hit_distance:
sound.play_effect('digital:PowerUp1')
self.waypoints[waypoint_index].background_color = '#6cd655'
self.waypoints_hit_this_turn = self.waypoints_hit_this_turn[1:]
if self.animation_state == 'sploding':
splode_alpha = 1.0-self.splosion_counter*0.15
splode_radius = self.splosion_counter * 4
splode_color = base_color + (splode_alpha,)
(x,y) = self.current_move[-1]
path = ui.Path.oval(x-splode_radius,y-splode_radius,2*splode_radius,2*splode_radius)
ui.set_color(splode_color)
path.fill()
# pos1 = self.current_move[self.animate_counter - 2]
# pos2 = self.current_move[self.animate_counter - 1]
# angle = (Vector(pos2) - Vector(pos1)).degrees
#print(self.animate_counter, angle)
def new_path(self, path_color, line_width=3):
ui.set_color(path_color)
path = ui.Path()
path.line_width = line_width
path.line_cap_style = ui.LINE_CAP_ROUND
return path
def fill(self):
self.history = []
self.future = []
with ui.ImageContext(self.width, self.height) as ctx:
ui.set_color('black')
ui.fill_rect(0, 0, self.width, self.height)
# iv = ui.ImageView(frame=self.bounds)
# iv.image = ui.Image('playfields/origami.png')
#
# img = snapshot(iv)
# blend_mode = ui.B
# ui.set_blend_mode(blend_mode)
#iv.image.draw()
# img.draw()
self.img = ctx.get_image()
def draw(self):
# Set drawing attributes
ui.set_color(self._color)
ui.set_shadow(*self._shadow)
# Calculations
scale_x = self.width / self._pathsize[0]
scale_y = self.height / self._pathsize[0]
# Scale the path
new_path = ui2.path_helpers.scale_path(self._path, (scale_x, scale_y))
new_path.fill()
def create_new_image(self):
path = ui.Path.rect(*self.bounds)
with ui.ImageContext(self.width, self.height) as ctx:
ui.set_color((0, 0, 0, 0))
path.fill()
return ctx.get_image()
def character_colorcheck(self):
(startPos, endPos) = self.get_current_region()
charSize = Settings.charSize
clashCount = 0
pixelScale = self.width/(endPos[0]-startPos[0]+1)/Settings.pixelSize #self.height/Settings.height
#s = self.width/self.row if self.row > self.column else self.height/self.column
with ui.ImageContext(self.width, self.height) as ctx:
ui.set_color('red')
# Grid line per character
for y in xrange(int(startPos[1]/charSize)*charSize, endPos[1]+1, charSize):
for x in xrange(int(startPos[0]/charSize*charSize), endPos[0]+1,4):
# Check this character for color clash
charColors ={(self.background_color[0], self.background_color[1], self.background_color[2])}
startIndex = xy_to_index(x,y)
for pixelRow in range(0, 8):
for pixelCol in range(0, 4):
pixelIndex = startIndex + pixelRow*Settings.actualWidth + pixelCol
charColors.add((self.pixels[pixelIndex].color[0], self.pixels[pixelIndex].color[1], self.pixels[pixelIndex].color[2]))
if len(charColors) > 4:
clashCount = clashCount + 1
pixel_path = ui.Path.rect((x-startPos[0])*pixelScale*2, (y-startPos[1])*pixelScale, pixelScale*charSize, pixelScale*charSize)
pixel_path.line_width = 2
pixel_path.stroke()
self.color_check.image = ctx.get_image()
if clashCount == 0:
self.color_check.image = self.create_new_image() # Clear clash image
self.superview['debugtext'].text = str(clashCount) + " characters have color clashes."
return clashCount
#@ui.in_background
def preview_drawPixels(self):
zoomPixels = self.position_pixels()
old_img = self.unfilteredPreview
with ui.ImageContext(Settings.width, Settings.height) as ctx:
old_img.draw(0,0,Settings.width, Settings.height)
for i in zoomPixels:
p = self.pixels[i]
ui.set_color(p.color)
pixel_path = ui.Path.rect(p.position[0]*Settings.pixelSize,p.position[1],1*Settings.pixelSize,2)
pixel_path.line_width = 0.5
pixel_path.fill()
#pixel_path.stroke()
#self.superview['preview'].image = ctx.get_image()
self.preview_putimg(ctx.get_image())
return True
def draw_index_array(self, img, indexArray, drawColor=False, noCheck=False):
with ui.ImageContext(self.width, self.height) as ctx:
img.draw()
for i in indexArray:
# Drawing negative indices will mess up the image, so only indices from 0 and up
if i >= 0:
# Skip checks and undos and just draw the pixels
if noCheck==True:
p = self.pixels[i]
if drawColor != False:
p.color = drawColor
ui.set_color(p.color)
pixel_path = ui.Path.rect(p.rect[0],p.rect[1],p.rect[2],p.rect[3])
pixel_path.line_width = 0.0
pixel_path.fill()
pixel_path.stroke()
else:
if self.pixels[i].color != self.current_color:
if self.checkDither(self.pixels[i].position):
self.store_undo_stroke(i)
p = self.pixels[i]
if drawColor != False:
p.color = drawColor
ui.set_color(p.color)
# Path.rect(x, y, width, height)
pixel_path = ui.Path.rect(p.rect[0],p.rect[1],p.rect[2],p.rect[3])
pixel_path.line_width = 0.0
pixel_path.fill()
pixel_path.stroke()
img = ctx.get_image()
return img
# Main pixel action function!
# Called by 'touch_began', touch_moved and touch_ended
def set_color(self, color=None):
color = color or self.get_color()
if self['current_color'].background_color == color:
# Set color twice, and the image bg color will be set
self['bg_color'].background_color = color
self.superview['editor'].background_color = color
if self.superview['editor'].color_check.hidden is False:
self.superview['editor'].character_colorcheck()
else:
self['current_color'].background_color = color
self.superview['editor'].current_color = color
self.superview['debugtext'].text = "BG color set to " + str(color_to_255(color))
def choose_color(self, sender):
if sender.name in self.color:
self.color[sender.name] = sender.value
self.set_color()
elif sender in self['palette'].subviews:
self.set_color(sender.background_color)
elif sender.name == 'color_input':
try:
c = sender.text if sender.text.startswith('#') else eval(sender.text)
v = ui.View(background_color=c)
self['color_input'].text = str(v.background_color)
self.set_color(v.background_color)
except Exception as e:
console.hud_alert('Invalid Color', 'error')
def chordtone_color(self,string,fret):
# convert from string/fret to note
key = fretboard.key
thisString = self._scale_notes[string]
for thisFret,thisNote in thisString:
color = 'red'
if fret == thisFret:
scaleTone = (thisNote - key) % 12
if scaleTone == 0:
color = 'green'
break
elif scaleTone in (3,4): # b3 and 3
color = 'yellow'
break
elif scaleTone in (7,8): # 5 and 5#
if scaleTone == 7:
color = 'white'
self.fifthPresent = True
break
elif scaleTone == 8 and not self.fifthPresent:
color = 'white'
break
elif scaleTone in (10,11):
color = 'orange'
break
ui.set_color(color)
return
def get_histogram(img):
if not img.mode.startswith('RGB'):
img = img.convert('RGB')
hist = img.histogram()
max_h = float(max(hist))
height = 180
with ui.ImageContext(430, height) as ctx:
a = 1
rgb = [(1, 0, 0, a), (0, 1, 0, a), (0, 0, 1, a)]
for i, color in enumerate(rgb):
ui.set_color(color)
for j, count in enumerate(hist[i*256:i*256+256]):
bar_height = count / max_h * (height - 5)
ui.fill_rect(2*j, height-bar_height, 2, bar_height)
return ctx.get_image()
def draw(self):
#if the path is larger then 100x100 it will be clipped
path = ui.Path.rect(0, 0, 100, 100)
ui.set_color(self.color)
path.fill()
def draw(self):
self.scr_height = self.height
self.scr_width = self.width
path = ui.Path.rect(0, 0, self.scr_width, self.scr_height)
ui.set_color(self.color)
path.fill()
self.x_off = (self.scr_width - (self.img_width*self.ratio/self.scr_cor)) / 2
self.y_off = (self.scr_height - (self.img_height*self.ratio/self.scr_cor)) / 2
self.img.draw(self.x_off,self.y_off,self.img_width*self.ratio/self.scr_cor,self.img_height*self.ratio/self.scr_cor)
def draw(self):
path = ui.Path.rect(0, 0, self.width, self.height)
ui.set_color(self.color)
path.fill()
img = ui.Image.named('Girl')
img.draw()
def draw(self):
#if the path is larger then 100x100 it will be clipped
path = ui.Path.rect(0, 0, 100, 100)
ui.set_color(self.color)
path.fill()
def draw(self):
'''draw a green box around kb frame, padded by 10 pixels'''
kb=self.get_keyboard_frame()
# print kb
kb_self=self.convert_rect(kb,None,self)
# print kb_self
ui.set_color((0,1,0,0.5))
ui.fill_rect(kb_self[0]-10,kb_self[1]-10, kb_self[2]+20,kb_self[3]+20)
self.t3.text=('orientation {}\n'
'kbframe {}\n'
'kbframe fixed {}\n '
'kbframe in V {}\n').format(self.get_orientation(),ui.get_keyboard_frame(),kb,kb_self)
def draw_grid_image(self):
(startPos, endPos) = self.get_current_region()
yPixelScale = self.width/(endPos[0]-startPos[0]+1)/Settings.pixelSize #self.height/Settings.height
xPixelScale = yPixelScale*2
charSize = Settings.charSize
charScale = yPixelScale*charSize
xRangeStart = int(startPos[0]/charSize*charSize)
pixelGrid = ui.Path.rect(0, 0, *self.frame[2:])
characterGrid = ui.Path.rect(0, 0, *self.frame[2:])
charDrawColor = (1,1,1,0.5) if self.darkGrid == False else (0,0,0,0.5)
lineDrawColor = (0.5,0.5,0.5,0.5) if self.darkGrid == False else (0.25,0.25,0.25,0.5)
with ui.ImageContext(*self.frame[2:]) as ctx:
# Fills entire grid with empty color
ui.set_color((0, 0, 0, 0))
#pixelGrid.fill()
pixelGrid.line_width = 1
# Horizontal gridlines
yEnd = 200 * yPixelScale
for x in xrange(startPos[0]+1, endPos[0]+1):
xPos = (x-startPos[0]) * xPixelScale
if x%4 != 0:
pixelGrid.move_to(xPos,0)
pixelGrid.line_to(xPos,yEnd)
else:
characterGrid.move_to(xPos,0)
characterGrid.line_to(xPos,yEnd)
# Vertical gridlines
xEnd = 160 * xPixelScale
for y in xrange(startPos[1]+1, endPos[1]+1):
yPos = (y-startPos[1]) * yPixelScale
if y%8 != 0:
pixelGrid.move_to(0, yPos)
pixelGrid.line_to(xEnd, yPos)
else:
characterGrid.move_to(0, yPos)
characterGrid.line_to(xEnd, yPos)
ui.set_color(lineDrawColor)
pixelGrid.stroke()
ui.set_color(charDrawColor)
characterGrid.stroke()
return ctx.get_image()
def draw(self):
square_size=min(self.width,self.height)
N=self.N
Nb=self.Nb
dx=square_size*1.0/(N+2)
dxb=N*dx/Nb
h,s,v=self.current
i0,j0,k0=(round(c*N) for c in self.current)
k0=round(self.current[2]*Nb)
#draw H/S grid
for i in xrange(0,N):
for j in xrange(0,N):
ui.set_color(colorsys.hsv_to_rgb(i*1.0/N,j*1.0/N,v))
ui.set_blend_mode(ui.BLEND_NORMAL)
ui.fill_rect(round(i*dx),round(j*dx),round(dx),round(dx))
#draw V slider
for k in xrange(0,Nb):
ui.set_color(colorsys.hsv_to_rgb(h,s,k*1./Nb))
ui.set_blend_mode(ui.BLEND_NORMAL)
ui.fill_rect(round((N+1)*dx),round(k*dxb),round(dx),round(dxb+0.5))
#highlight selection
if all([c>=0 for c in self.current]):
ui.set_color(colorsys.hsv_to_rgb(h,s,1-0.5*(1-v)))
p=ui.Path.rect(i0*dx,j0*dx,dx,dx)
p.line_width=4
p.stroke()
ui.set_color(colorsys.hsv_to_rgb(h,s,1-0.5*(1-v)))
p=ui.Path.rect((N+1)*dx,k0*dxb,dx,dxb)
p.line_width=4
p.stroke()
#preview
ui.set_color(colorsys.hsv_to_rgb(h,s,v))
ui.fill_rect(0,(N+1)*dx,6*dx,dx)
r,g,b=colorsys.hsv_to_rgb(h,s,v)
clip=lambda x:min(max(x,0),1)
rp,gp,bp=colorsys.hsv_to_rgb(1-h,1,clip((0.5-v)*100))
ui.draw_string( ('{:02x}'*3).format(int(r*255),int(g*255),int(b*255)), (0,(N+1)*dx,6*dx,dx),alignment=ui.ALIGN_CENTER,color=(rp,gp,bp))
def draw(self):
# redraw button
def darken(color):
return tuple([0.5*x for x in color])
#set up button size to fit.
padding=10
textsize=ui.measure_string(string=self.title,max_width=0,font=self.font,alignment=ui.ALIGN_CENTER)
#draw border
ui.set_color(self.border_color)
path = ui.Path.rounded_rect(0, 0, self.width, self.height,self.corner_radius)
path.line_width=self.border_width
path.stroke()
#fill button, depending on touch state
if self.touched:
if self.doing_longtouch:
ui.set_color('blue')
else:
ui.set_color('grey')
else :
ui.set_color(self.bg_color)
path.fill()
# fill corner darker, if has children
if self.flow.subviews:
corner = ui.Path()
corner.move_to(self.width-1.5*self.corner_radius,0)
corner.line_to(self.width,0)
corner.line_to(self.width,1.5*self.corner_radius)
corner.line_to(self.width-1.5*self.corner_radius,0)
ui.set_color(darken(darken(self.bg_color)))
corner.stroke()
corner.fill()
# draw title, center vertically, horiz
rect=list(self.bounds)
rect[1]=(rect[3]-textsize[1])/2 #vert center
rect[2]=max(rect[2],textsize[0]+10)
ui.draw_string(self.title, rect=tuple(rect), font=self.font, color=self.tint_color, alignment=ui.ALIGN_CENTER, line_break_mode=ui.LB_WORD_WRAP)
if textsize[0]>self.bounds[2]:
self.width=textsize[0]+10