colorpicker.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:pythonista-scripts 作者: khilnani 项目源码 文件源码
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))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号