Python:颜色转换器,一个转换颜色格式、调节颜色的小工具,支持RGB、HSV、16进制互转; 附源代码和下载地址

因为在写gui的时候经常要使用各种颜色,但颜色的调节和选取都十分麻烦,让我非常的暴躁!一气之下我随手写了一个小工具,这样取色就方便多了。效果如下:


颜色转换器示例

下载地址:
exe文件:
https://download.csdn.net/download/weixin_52132159/20272220
py文件:
https://download.csdn.net/download/weixin_52132159/20272319


话不多说直接上代码:

代码

#颜色转换器
#这是一个转换颜色格式、调节颜色的小工具
#可以进行RGB、HSV、16进制色互转,以方便取色
#作者:H_612

from tkinter import Tk
from tkinter import Label
from tkinter import Frame
from tkinter import Entry
from tkinter import Scale
from tkinter import StringVar
from tkinter import Menu
from tkinter import Message
from tkinter import Toplevel
from tkinter.ttk import Separator
from math import floor

#这里的格式转换我就不多说了,各位可以自己去搜索一下
#RGB转HSV
def rgbtohsv(r, g, b):
    
    r, g, b, = float(r), float(g), float(b) 
    r, g, b = r/255.0, g/255.0, b/255.0
    _max = max(r, g, b)
    _min = min(r, g, b)
    V = _max
    if V == 0:
        return 0, 0, 0
    S = (_max - _min) / _max
    if r == g == b:
        return 0, 0, round(V, 3)
    if r == _max:
        H = (g - b) / (_max - _min) * 60
    if g == _max:
        H = 120 + (b - r)/(_max - _min) * 60
    if b == _max:
        H = 240 + (r - g)/(_max - _min) * 60
    if H < 0:
        H += 360
    #H = H
    #S = S * 255
    #V = V * 255
    
    return int(H), round(S, 3), round(V, 3)

#HSV转RGB
def hsvtorgb(h, s, v):
    
    h = float(h)
    s = float(s)
    v = float(v)
    
    h60 = h / 60.0
    h60f = floor(h60)
    hi = int(h60f) % 6
    f = h60 - h60f
    p = v * (1 - s)
    q = v * (1 - f * s)
    t = v * (1 - (1 - f) * s)
    r, g, b = 0, 0, 0
    
    if hi == 0:
        r, g, b = v, t, p
    elif hi == 1:
        r, g, b = q, v, p
    elif hi == 2:
        r, g, b = p, v, t
    elif hi == 3:
        r, g, b = p, q, v
    elif hi == 4:
        r, g, b = t, p, v
    elif hi == 5:
        r, g, b = v, p, q
    r, g, b = int(r * 255), int(g * 255), int(b * 255)
    
    return r, g, b

#RGB转16进制
def rgbtohex(r, g, b):
    
    text = '#' + ''.join([hex(i)[-2:].replace('x','0') for i in list(map(int, [r, g, b]))])
    
    return text

#16进制转RGB
def hextorgb(text: str):
    
    r = int('0x' + text[1:3], 16)
    g = int('0x' + text[3:5], 16)
    b = int('0x' + text[5:7], 16)
    
    return r, g, b

#GUI界面
class gui:
    
    def __init__(self):
        #gui界面
        self.root = Tk(className = '颜色转换器')
        self.root.attributes('-topmost', True)
        self.root.resizable(0, 0)
        self.root.iconbitmap('colours_icon.ico')


        #RGB、HSV框
        self.frm_rgbhev = Frame(self.root)
        self.frm_rgbhev.grid(column=0, row=0, padx=5, pady=5)

        #RGB部分
        self.rvar = StringVar() #实例化StringVar
        self.gvar = StringVar()
        self.bvar = StringVar()
        rgbargs = {'command': lambda value: self.show('rgb'), 'from_': 0, 'to': 255, 'resolution': 5, 'sliderlength': 20, 'showvalue': False, 'orient': 'horizontal'}   #一些滑块参数
        #R
        Label(self.frm_rgbhev, text='红0~255(r):', anchor='w').grid(column=0, row=0, sticky='we')
        self.entry_r = Entry(self.frm_rgbhev, textvariable=self.rvar, width=10)
        self.entry_r.grid(column=1, row=0, sticky='we')
        self.scale_r = Scale(self.frm_rgbhev, variable=self.rvar, **rgbargs)
        self.scale_r.grid(column=2, row=0, sticky='we')
        #G
        Label(self.frm_rgbhev, text='绿0~255(g):', anchor='w').grid(column=0, row=1, sticky='we')
        self.entry_g = Entry(self.frm_rgbhev, textvariable=self.gvar, width=10)
        self.entry_g.grid(column=1, row=1, sticky='we')
        self.scale_g = Scale(self.frm_rgbhev, variable=self.gvar, **rgbargs)
        self.scale_g.grid(column=2, row=1, sticky='we')
        #B
        Label(self.frm_rgbhev, text='蓝0~255(b):', anchor='w').grid(column=0, row=2, sticky='we')
        self.entry_b = Entry(self.frm_rgbhev, textvariable=self.bvar, width=10)
        self.entry_b.grid(column=1, row=2, sticky='we')
        self.scale_b = Scale(self.frm_rgbhev, variable=self.bvar, **rgbargs)
        self.scale_b.grid(column=2, row=2, sticky='we')
        #为了得到最新值,我们等10毫秒再获取输入框的值。后面的也和这里一样:
        self.entry_r.bind('', lambda event: self.root.after(10, lambda:self.show('rgb')))
        self.entry_g.bind('', lambda event: self.root.after(10, lambda:self.show('rgb')))
        self.entry_b.bind('', lambda event: self.root.after(10, lambda:self.show('rgb')))
        
        #分界线
        Separator(self.frm_rgbhev, orient='horizontal').grid(column=0, row=3, columnspan=3, pady=5, sticky='we')

        #HSV部分
        self.hvar = StringVar()     #实例化StringVar
        self.svar = StringVar()
        self.vvar = StringVar()
        hsvargs = {'command': lambda value: self.show('hsv'), 'sliderlength': 20, 'showvalue': False, 'orient': 'horizontal'}   #一些滑块参数
        #H
        Label(self.frm_rgbhev, text='色调0~360(h):', anchor='w').grid(column=0, row=4, sticky='we')
        self.entry_h = Entry(self.frm_rgbhev, textvariable=self.hvar, width=10)
        self.entry_h.grid(column=1, row=4, sticky='we')
        self.scale_h = Scale(self.frm_rgbhev, variable=self.hvar, from_=0, to=360, resolution=10, **hsvargs)
        self.scale_h.grid(column=2, row=4, sticky='we')
        #S
        Label(self.frm_rgbhev, text='纯度0~1(s):', anchor='w').grid(column=0, row=5, sticky='we')
        self.entry_s = Entry(self.frm_rgbhev, textvariable=self.svar, width=10)
        self.entry_s.grid(column=1, row=5, sticky='we')
        self.scale_s = Scale(self.frm_rgbhev, variable=self.svar, from_=0, to=1, resolution=0.1, **hsvargs)
        self.scale_s.grid(column=2, row=5, sticky='we')
        #V
        Label(self.frm_rgbhev, text='明度0~1(v):', anchor='w').grid(column=0, row=6, sticky='we')
        self.entry_v = Entry(self.frm_rgbhev, textvariable=self.vvar, width=10)
        self.entry_v.grid(column=1, row=6, sticky='we')
        self.scale_v = Scale(self.frm_rgbhev, variable=self.vvar, from_=0, to=1, resolution=0.1, **hsvargs)
        self.scale_v.grid(column=2, row=6, sticky='we')
        self.entry_h.bind('', lambda event: self.root.after(10, lambda:self.show('hsv')))
        self.entry_s.bind('', lambda event: self.root.after(10, lambda:self.show('hsv')))
        self.entry_v.bind('', lambda event: self.root.after(10, lambda:self.show('hsv')))

        
        #HEX输入框、显示框
        self.hexvar = StringVar()
        #Frame
        self.showfrm = Frame(self.root)
        self.showfrm.grid(column=2, row=0, rowspan=2, sticky='wens')
        #HEX
        Label(self.showfrm, text='16进制(hex):', anchor='w').grid(column=0, row=0, sticky='ns')
        self.entry_hex = Entry(self.showfrm, textvariable=self.hexvar, width=15)
        self.entry_hex.grid(column=1, row=0, sticky='ns', padx=(0,5), pady=(5,0))
        #显示颜色
        self.lab_show = Label(self.showfrm, bg='black')
        self.lab_show.grid(column=0, row=1, columnspan=2, sticky='wens', padx=(0,5), pady=(0,5))
        self.showfrm.grid_rowconfigure(1, weight=1)
        self.entry_hex.bind('', lambda event: self.root.after(10, lambda:self.show('hex')))

        
        self.root.mainloop()

    #在标签内显示颜色,并同步颜色值
    def show(self, form):
        if form == 'rgb':
            self.list_rgb = list()
            for i in [self.rvar, self.gvar, self.bvar]:
                self.list_rgb.append(int(i.get()) if i.get() else 0)
            self.list_hsv = [*rgbtohsv(*self.list_rgb)]
            self.hvar.set(self.list_hsv[0])
            self.svar.set(self.list_hsv[1])
            self.vvar.set(self.list_hsv[2])
            self.hexvar.set(rgbtohex(*self.list_rgb))
            self.lab_show.configure(bg=rgbtohex(*self.list_rgb))
        elif form == 'hsv':
            self.list_hsv = list()
            for i in [self.hvar, self.svar, self.vvar]:
                self.list_hsv.append(float(i.get()) if i.get() else 0)
            self.list_rgb = [*hsvtorgb(*self.list_hsv)]
            self.rvar.set(self.list_rgb[0])
            self.gvar.set(self.list_rgb[1])
            self.bvar.set(self.list_rgb[2])
            self.hexvar.set(rgbtohex(*self.list_rgb))
            self.lab_show.configure(bg=rgbtohex(*self.list_rgb))
        else:
            if len(self.hexvar.get()) == 7:
                self.list_rgb = [*hextorgb(self.hexvar.get())]
                self.list_hsv = [*rgbtohsv(*self.list_rgb)]
                self.rvar.set(self.list_rgb[0])
                self.gvar.set(self.list_rgb[1])
                self.bvar.set(self.list_rgb[2])
                self.hvar.set(self.list_hsv[0])
                self.svar.set(self.list_hsv[1])
                self.vvar.set(self.list_hsv[2])
                try: self.lab_show.configure(bg=self.hexvar.get())
                except: pass
    

if __name__ == '__main__':
    gui()

CSDN同名搜 H_612,看更多Python文章
CSDN博客:https://blog.csdn.net/weixin_52132159

你可能感兴趣的:(Python:颜色转换器,一个转换颜色格式、调节颜色的小工具,支持RGB、HSV、16进制互转; 附源代码和下载地址)