Skip to content

Instantly share code, notes, and snippets.

@varunchandak
Created August 7, 2024 11:13
Show Gist options
  • Save varunchandak/cd886d694e16585749ef2ebb3f31f75e to your computer and use it in GitHub Desktop.
Save varunchandak/cd886d694e16585749ef2ebb3f31f75e to your computer and use it in GitHub Desktop.
Gmail Label Coloring Script
{
"allowed_colors": [
{
"background": [
"#000000",
"#04502e",
"#076239",
"#094228",
"#0b4f30",
"#0b804b",
"#0d3472",
"#0d3b44",
"#149e60",
"#16a765",
"#16a766",
"#1a764d",
"#1c4587",
"#285bac",
"#2a9c68",
"#2da2bb",
"#3c78d8",
"#3d188e",
"#3dc789",
"#41236d",
"#42d692",
"#434343",
"#43d692",
"#44b984",
"#464646",
"#4986e7",
"#4a86e8",
"#594c05",
"#653e9b",
"#662e37",
"#666666",
"#684e07",
"#68dfa9",
"#6d9eeb",
"#711a36",
"#7a2e0b",
"#7a4706",
"#822111",
"#83334c",
"#89d3b2",
"#8a1c0a",
"#8e63ce",
"#98d7e4",
"#994a64",
"#999999",
"#a0eac9",
"#a2dcc1",
"#a46a21",
"#a479e2",
"#a4c2f4",
"#aa8831",
"#ac2b16",
"#b3efd3",
"#b65775",
"#b694e8",
"#b6cff5",
"#b99aff",
"#b9e4d0",
"#c2c2c2",
"#c6f3de",
"#c9daf8",
"#cc3a21",
"#cca6ac",
"#cccccc",
"#cf8933",
"#d0bcf1",
"#d5ae49",
"#e07798",
"#e3d7ff",
"#e4d7f5",
"#e66550",
"#e7e7e7",
"#eaa041",
"#ebdbde",
"#efa093",
"#efefef",
"#f2b2a8",
"#f2c960",
"#f3f3f3",
"#f691b2",
"#f691b3",
"#f6c5be",
"#f7a7c0",
"#fad165",
"#fb4c2f",
"#fbc8d9",
"#fbd3e0",
"#fbe983",
"#fcda83",
"#fcdee8",
"#fce8b3",
"#fdedc1",
"#fef1d1",
"#ff7537",
"#ffad46",
"#ffad47",
"#ffbc6b",
"#ffc8af",
"#ffd6a2",
"#ffdeb5",
"#ffe6c7",
"#ffffff"
]
},
{
"text": [
"#000000",
"#04502e",
"#076239",
"#094228",
"#0b4f30",
"#0b804b",
"#0d3472",
"#0d3b44",
"#149e60",
"#16a765",
"#16a766",
"#1a764d",
"#1c4587",
"#285bac",
"#2a9c68",
"#2da2bb",
"#3c78d8",
"#3d188e",
"#3dc789",
"#41236d",
"#42d692",
"#434343",
"#43d692",
"#44b984",
"#464646",
"#4986e7",
"#4a86e8",
"#594c05",
"#653e9b",
"#662e37",
"#666666",
"#684e07",
"#68dfa9",
"#6d9eeb",
"#711a36",
"#7a2e0b",
"#7a4706",
"#822111",
"#83334c",
"#89d3b2",
"#8a1c0a",
"#8e63ce",
"#98d7e4",
"#994a64",
"#999999",
"#a0eac9",
"#a2dcc1",
"#a46a21",
"#a479e2",
"#a4c2f4",
"#aa8831",
"#ac2b16",
"#b3efd3",
"#b65775",
"#b694e8",
"#b6cff5",
"#b99aff",
"#b9e4d0",
"#c2c2c2",
"#c6f3de",
"#c9daf8",
"#cc3a21",
"#cca6ac",
"#cccccc",
"#cf8933",
"#d0bcf1",
"#d5ae49",
"#e07798",
"#e3d7ff",
"#e4d7f5",
"#e66550",
"#e7e7e7",
"#eaa041",
"#ebdbde",
"#efa093",
"#efefef",
"#f2b2a8",
"#f2c960",
"#f3f3f3",
"#f691b2",
"#f691b3",
"#f6c5be",
"#f7a7c0",
"#fad165",
"#fb4c2f",
"#fbc8d9",
"#fbd3e0",
"#fbe983",
"#fcda83",
"#fcdee8",
"#fce8b3",
"#fdedc1",
"#fef1d1",
"#ff7537",
"#ffad46",
"#ffad47",
"#ffbc6b",
"#ffc8af",
"#ffd6a2",
"#ffdeb5",
"#ffe6c7",
"#ffffff"
]
}
]
}

Gmail Label Coloring Script

This script allows you to color-code your Gmail labels programmatically using the Gmail API. It ensures good contrast between background and text colors and cycles through available colors without repetition until all options have been used.

Features

  • Automatically colors Gmail labels with user-defined colors.
  • Ensures that background colors are only repeated after all have been used.
  • Offers flexibility to use either dark backgrounds with light text or light backgrounds with dark text.
  • Supports nested labels, applying the same color to parent and nested labels.

Prerequisites

  • Python 3.x installed on your system.
  • Access to OAuth 2.0 Playground

Getting Started

  1. Clone this gist or download the files directly.

  2. Install Required Python Packages:

    pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client requests
    
  3. Download the gmail-label-colors.json file. This file defines the available background and text colors to be used by the script.

  4. Obtain an Access Token using OAuth 2.0 Playground: To interact with Gmail programmatically, follow these steps to get an ACCESS_TOKEN using OAuth 2.0 Playground:

    1. Visit OAuth 2.0 Playground
    2. In the left panel, scroll down to "Gmail API v1" and select https://www.googleapis.com/auth/gmail.labels.
    3. Click on the "Authorize APIs" button.
    4. Sign in with your Google account and grant the necessary permissions.
    5. After authorization, you’ll be redirected back to the OAuth 2.0 Playground.
    6. Click on "Exchange authorization code for tokens" to get your Access Token.
    7. Copy the Access Token displayed.

Note: The access token is temporary and will expire after one hour. You can repeat the above steps to generate a new token when needed.

  1. Run the Script With the prerequisites in place, you can run the script as follows:

    python gmail-label-colors.py
    
  2. Customize the Script Toggle Dark or Light Backgrounds: Modify the use_dark_background variable in the script to choose whether you want dark backgrounds with light text or light backgrounds with dark text.

License

This project is open-source and available under the MIT License.

import os
import json
import random
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
import requests
def load_colors_from_file(file_path):
with open(file_path, 'r') as file:
colors = json.load(file)
return colors
def categorize_colors(color_list):
dark_colors = [color for color in color_list if not color.startswith('#f')]
light_colors = [color for color in color_list if color.startswith('#f')]
return dark_colors, light_colors
def choose_color_combination(available_backgrounds, text_colors, used_backgrounds, use_dark_background=True):
# Choose a background color that hasn't been used yet
if not available_backgrounds: # If no available backgrounds left, reset
available_backgrounds.extend(used_backgrounds)
used_backgrounds.clear()
background_color = random.choice(available_backgrounds)
available_backgrounds.remove(background_color)
used_backgrounds.add(background_color)
# Select an appropriate text color based on the brightness of the background
dark_text_colors = [color for color in text_colors if not color.startswith('#f')]
light_text_colors = [color for color in text_colors if color.startswith('#f')]
if background_color.startswith('#f'): # Light background
text_color = random.choice(dark_text_colors)
else: # Dark background
text_color = random.choice(light_text_colors)
return {"background": background_color, "text": text_color}
def authenticate_gmail():
creds = None
token = os.getenv('ACCESS_TOKEN')
if token:
creds = Credentials(token)
else:
print("ACCESS_TOKEN environment variable is not set.")
return None
return creds
def fetch_labels(service):
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
return labels
def color_gmail_label(label_id, label_name, color):
url = f"https://gmail.googleapis.com/gmail/v1/users/me/labels/{label_id}"
headers = {
"Authorization": f"Bearer {os.getenv('ACCESS_TOKEN')}",
"Content-Type": "application/json"
}
data = {
"name": label_name,
"labelListVisibility": "labelShow",
"messageListVisibility": "show",
"color": {
"backgroundColor": color['background'],
"textColor": color['text']
}
}
response = requests.patch(url, headers=headers, json=data)
if response.status_code == 200:
print(f"Label '{label_name}' colored successfully.")
else:
print(f"Failed to color label '{label_name}': {response.status_code}, {response.text}")
def main():
# Load colors from the JSON file
colors_file_path = 'gmail-label-colors.json'
allowed_colors = load_colors_from_file(colors_file_path)
background_colors = allowed_colors['allowed_colors'][0]['background']
text_colors = allowed_colors['allowed_colors'][1]['text']
# Categorize colors into dark and light
dark_backgrounds, light_backgrounds = categorize_colors(background_colors)
dark_texts, light_texts = categorize_colors(text_colors)
# Choose whether to use dark backgrounds with light texts or vice versa
use_dark_background = True # Set to False if you prefer light background with dark text
# Tracking used and available colors
available_backgrounds = dark_backgrounds if use_dark_background else light_backgrounds
used_backgrounds = set()
creds = authenticate_gmail()
if not creds:
return
service = build('gmail', 'v1', credentials=creds)
labels = fetch_labels(service)
label_colors = {}
for label in labels:
parent_label = label['name'].split('/')[0] # Get the parent label name
if parent_label not in label_colors:
# Choose an appropriate color combination
label_colors[parent_label] = choose_color_combination(
available_backgrounds,
text_colors,
used_backgrounds,
use_dark_background
)
color = label_colors[parent_label]
color_gmail_label(label['id'], label['name'], color)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment