Skip to content

Instantly share code, notes, and snippets.

@cclauss
Last active August 8, 2023 14:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cclauss/8169809 to your computer and use it in GitHub Desktop.
Save cclauss/8169809 to your computer and use it in GitHub Desktop.
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 …
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment