def fill_rect(self, xorg, yorg, width, height, color=(1, 1, 1)): w, h = self.whWorld(width, height) x, y = self.xyWorld(xorg, yorg) logger.debug('fill rect xy:%s, wh:%s with:%s' % ((x, y + h), (w, -h), color)) with ui.GState(): ui.set_color(color) ui.set_blend_mode(ui.BLEND_NORMAL) rect = ui.Path.rect(x, y + h, w, -h) # prevent neg height rect.eo_fill_rule = True rect.close() rect.fill() self.path.append_path(rect)
def draw(self): self.y = 0 square_size = max(self.width, self.height) N = self.N Nb = self.Nb dx = square_size * 1.0 / (N + 3) 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 range(0, N + 1): for j in range(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 * self.height / Nb), round(dx), round(self.height + 1 / Nb)) #draw V slider for k in range(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.5) * dx), round(k * self.height / Nb), round(dx), round(self.height / Nb + 0.5)) #highlight selection if all([c >= 0 for c in self.current]): # h,s selection ui.set_color(colorsys.hsv_to_rgb(h - 0.1, s, 1 - 0.5 * v)) p = ui.Path.rect(i0 * dx, j0 * (self.height + 10) / Nb, dx, self.height / Nb) p.line_width = 4 p.stroke() # v selection ui.set_color(colorsys.hsv_to_rgb(h, s, 1 - 0.5 * (0.2 + v))) p = ui.Path.rect((N + 1.5) * dx, k0 * (self.height - 10) / Nb, dx, self.height / Nb) p.line_width = 4 p.stroke() self.rgb = colorsys.hsv_to_rgb(self.current[0], self.current[1], self.current[2]) r, g, b = self.rgb self.superview.superview.set_colorView(r, g, b)
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.hidden = True #self.add_subview(iv) 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): 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 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()
# Test blend modes in ui package import ui # image on image img = ui.Image.named('Test_Lenna') with ui.ImageContext(200,200) as ctx: img.draw(0,0,200,200) ui.set_blend_mode(ui.BLEND_MULTIPLY) img.draw(50,50,100,100) result = ctx.get_image() result.show() # rect on image with ui.ImageContext(200,200) as ctx: img.draw(0,0,200,200) ui.set_blend_mode(ui.BLEND_MULTIPLY) ui.set_color('red') ui.fill_rect(50,50,100,100) result = ctx.get_image() result.show() # image on rect with ui.ImageContext(200,200) as ctx: ui.set_color('red') ui.fill_rect(0,0,200,200) ui.set_blend_mode(ui.BLEND_MULTIPLY) img.draw(50,50,100,100) result = 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) offset = 1.0 - (math.sqrt(Nb - 1) * 1. / math.sqrt(Nb)) # k0 holds the number of field in value slider # at drawing in line 45 k and Nb are scaled by square root # # v = (math.sqrt(k) * 1. / math.sqrt(Nb) # v * math.sqrt(NB) = math.sqrt(k) # v * v * NB = k scv = self.current[2] - offset # remove when debugging is done k0 = round(scv * scv * Nb) #print (f" sel {float(scv):.3} * Nb = {float(scv * Nb):3.3} ^2*Nb= {float(scv * scv * Nb):3.3} k0 = {k0}") # poking around - should do my math # k0 = #draw H/S grid for i in range(0, N): print(f"{i}:{N}") for j in range(0, N): ui.set_color( colorsys.hsv_to_rgb(i * 1.0 / N, j * 1.0 / (N - 1), 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 range(0, Nb): ui.set_color( colorsys.hsv_to_rgb( h, s, offset + (math.sqrt(k) * 1. / math.sqrt(Nb)))) #if self.__debugprint : #print (f"draw {k} = {(math.sqrt(k) * 1. / math.sqrt(Nb)):.3} off {offset:.3}") # 1-(..) would look nicer but is lot of rework in code # v=1.0 should always be in reach -> how can this be certain? # dont need the darker shades, but more od the lighter ones # add offset? # steeper curve? quadratic would maybe be to steep ui.set_blend_mode(ui.BLEND_NORMAL) ui.fill_rect(round((N + 1) * dx), round(k * dxb), round(dx), round(dxb + 0.5)) #self.__debugprint=False #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))) ui.set_color( colorsys.hsv_to_rgb(1 - 0.5 * (1 - h), (1 - 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(1 - 0.5 * (1 - h), (1 - 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))