Last active
June 26, 2021 22:47
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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