Skip to content

Instantly share code, notes, and snippets.

@gurunars
Last active June 26, 2021 22:47
Show Gist options
  • Save gurunars/4f6dc4ee5adf5f184915 to your computer and use it in GitHub Desktop.
Save gurunars/4f6dc4ee5adf5f184915 to your computer and use it in GitHub Desktop.
Fetches css color specs from https://www.w3schools.com/cssref/css_colors.asp and prints an Android color resource file content. Depends on BeautifulSoup.
"""
requests
beautifulsoup4
"""
import requests
from bs4 import BeautifulSoup
def to_rgb(hex):
return [int(it, 16) for it in [hex[1:3], hex[3:5], hex[5:7]]]
def get_colors():
resp = requests.get("https://www.w3schools.com/cssref/css_colors.asp")
soup = BeautifulSoup(resp.text, 'html.parser')
boxes = soup.findAll("div", {"class": "colorbox"})
for box in boxes:
name = box.find("span", {"class": "colornamespan"}).find("a").getText()
color_hex = box.find("span", {"class": "colorhexspan"}).find("a").getText()
r, g, b = to_rgb(color_hex)
print("val {} = Color({}, {}, {})".format(name, r, g, b))
if __name__ == "__main__":
get_colors()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment