forked from tdamdouni/Pythonista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetColor.py
45 lines (39 loc) · 2.55 KB
/
getColor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import print_function
# https://gist.github.com/cclauss/8169809
# Creates a dict of 752 Pythonista scene.Colors from the tkinter color palette. This allow you to get colors like: 'light goldenrod yellow', 'light steel blue', 'SlateGray4', 'etc. I would recommend using this code to find the colors that work for your app and then hardcoding them into you app. Loading all 752 colors every time your app runs will not make you popular with your users.
import bs4, collections, console, requests, scene
tkColorDict = collections.OrderedDict() # key = tkinter color name
def loadTkColorDict(): # will automaticly be called by getColor() if needed
tkColorURL = 'http://www.tcl.tk/man/tcl8.6/TkCmd/colors.htm'
print('Loading tkinter colors from: ' + tkColorURL)
tkColorSoup = bs4.BeautifulSoup(requests.get(tkColorURL).text).tbody
print('Soup is ready. Creating color table...')
for tableRow in tkColorSoup.find_all('tr'):
colorInfo = [x.text for x in tableRow.find_all('p')]
if colorInfo[0] != 'Name': # skip the table header
tkColorDict[colorInfo[0]] = (int(colorInfo[1]) / 255.0, # red
int(colorInfo[2]) / 255.0, # green
int(colorInfo[3]) / 255.0) # blue
# optionaly show the results...
for colorName in tkColorDict: # 752 colors
#console.set_color(*tkColorDict[colorName]) # some colors are not visible
print('{:<22} = {}'.format(colorName, tkColorDict[colorName]))
print('tkColorDict now contains {} colors.\n'.format(len(tkColorDict)))
def getColor(inColorName = 'grey'):
if not tkColorDict: # if tkColorDict has not been initialized
loadTkColorDict() # then put tkinter colors into tkColorDict
try:
return scene.Color(*tkColorDict[inColorName])
except KeyError:
print("'{}' is not a valid color. Substituting grey...".format(inColorName))
return getColor()
if __name__ == '__main__':
lgy = getColor('light goldenrod yellow')
#console.set_color(lgy.r, lgy.g, lgy.b) # some colors are not visble
print("getColor('{}') = ({}, {}, {})".format('light goldenrod yellow', lgy.r, lgy.g, lgy.b))
testColorNames = ('black white red green blue Bob').split()
for testColorName in testColorNames:
testColor = getColor(testColorName)
console.set_color(testColor.r, testColor.g, testColor.b)
print("getColor('{}') = ({}, {}, {})".format(testColorName, testColor.r, testColor.g, testColor.b))
console.set_color(0, 0, 0) # back to black