这是基于Python/Tkinter的16位RGB颜色查询器,因为之前在做WEB前端设计配色方案的时候,需要了解色值,每次用网页查询颜色对应的RGB值和HEX值,很不方便,所以就想写个单机程序,原本是采用win32插件,但实际上win32插件包以及颜色插件大都只包含选择颜色功能,并不具备查询功能,后来索性将颜色值Label写入到GUI中,并且加入查询色值的功能,这个简单的颜色查询小工具就诞生了。后来又有了获取某个区域色值的需求,因此也就加上了获取鼠标点击处色值的附加功能,现在这个小工具依然安装在自己电脑上,如果需要查询色值,偶尔还会用到。今天将代码贴出来,代码本身并不复杂,大家可以一起学习讨论!


代码如下:
1.包引入以及主类初始化
#-*- coding:utf-8 -*-
#颜色查询程序,查询rgb和hex对应的16位颜色真值,以及部分颜色的查看表,适用于初学者
#-------------------------------------------------------------------------
#添加了点击鼠标左键,然后显示鼠标所在位置的hex值和颜色
import operator
import os
import Pmw
import time
import tkMessageBox
from PIL import ImageGrab
from Tkinter import *
#corol_contraset 查询rgb对应的颜色,以及部分颜色值表
class Corol_contrast():
def __init__(self):
self.tople=None
bas=os.getcwd()
icth=bas+'\color1.ico'
self.root=Tk()
self.root.geometry('630x510+300+100')
self.root.title('RGB颜色查询器')
self.root.iconbitmap(icth )
#self.root.grab_set_global()
self.balloon=Pmw.Balloon(self.root,xoffset=-250,yoffset=5)
self.frm=Frame(self.root,borderwidth=1)
self.frm.pack(side=TOP,expand=1,fill=BOTH,pady=10)
self.frm1=Frame(self.frm)
self.frm1.pack(side=TOP,fill=X)
self.frm2=Pmw.ScrolledFrame(self.frm,labelpos=N,label_text=u'颜色对照表',
label_font=('Verdana Bold',12))
self.frm2.pack(side=TOP,fill=X,padx=5,pady=10)
self.frm2_fr=self.frm2.interior()
self.frm3=Frame(self.frm,borderwidth=2,relief=FLAT)
self.frm3.pack(side=TOP,padx=20,pady=5)
self.but_mouse=Button(self.frm3,text="Mouse_Col", command=self.mouse_col,font=('Verdana Bold',10))
self.but_mouse.pack(side=TOP)
wenben='点击Mouse_Col后,单击鼠标中键,获取鼠标所在位置的RGB和色彩,点击左键消除显示,双击左键释放鼠标抓!'
self.balloon.bind(self.but_mouse,wenben)
2.16位色值转RGB值
def hex2rgb(self,hexcolor):
#----------------------16进制转rgb-----------------------------
rgb = [(hexcolor >> 16) & 0xff,
(hexcolor >> 8) & 0xff,
hexcolor & 0xff
]
return rgb
3.RGB值转16位色值
def rgb2hex(self,rgbcolor):
#-------------------rgb转16进制----------------------------
#r, g, b = rgbcolor
if rgbcolor[0]<16:
R_zhi='0'+str(hex(rgbcolor[0]))[2:]
else:
R_zhi=str(hex(rgbcolor[0]))[2:]
if rgbcolor[1]<16:
G_zhi='0'+str(hex(rgbcolor[1]))[2:]
else:
G_zhi=str(hex(rgbcolor[1]))[2:]
if rgbcolor[2]<16:
B_zhi='0'+str(hex(rgbcolor[2]))[2:]
else:
B_zhi=str(hex(rgbcolor[2]))[2:]
hex_='#'+R_zhi+G_zhi+B_zhi
return hex_
4.鼠标绑定操作函数
def mouse_col(self):
#该函数是鼠标点击并且显示hex和颜色的函数
self.root.grab_set_global()
#这句是获得全屏幕的指针抓
self.root.bind('<Button-2>',self.printEvent_2)
#右键绑定事件
self.root.bind('<Button-1>',self.printevent_1)
#左键绑定事件
self.root.bind('<Double-Button-1>',self.double_mouse)
#双击左键绑定事件
self.root.wm_iconify()
self.root.grab_set_global()
5.鼠标抓取和释放函数操作
def double_mouse(self,event):
#_--------------双击左键-----------
self.root.grab_release()
def printevent_1(self,event):
#--------------------单击左键---------------
if self.tople!=None:
self.tople.destroy()
self.root.grab_set_global()
else:
self.root.grab_set_global()
self.root.grab_release()
def printEvent_2(self,event):
#-------------------单击右键--------------------
#self.root.grab_set_global()
if self.tople==None:
pass
else:
self.tople.destroy()
im=ImageGrab.grab()