Skip to content

Instantly share code, notes, and snippets.

@RamiAwar
Created October 27, 2023 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RamiAwar/ffeb53754ad435f5766f3a02ce7b497d to your computer and use it in GitHub Desktop.
Save RamiAwar/ffeb53754ad435f5766f3a02ce7b497d to your computer and use it in GitHub Desktop.
Generate very light colors in a spectrum to display as background for columns in an excel or csv. Light enough to show black text in front, different enough to be distinguishable, and lacking a red component to allow for validation errors be highlighted in red.
import colorsys
class ColorGenerator:
def __init__(self, hue=0.2, saturation=0.2, value=0.8):
self.hue = hue # Range [0.0, 1.0]
self.saturation = saturation # Range [0.0, 1.0]
self.value = value # Range [0.0, 1.0]
def get_color(self):
# Convert HSV to RGB
r, g, b = colorsys.hsv_to_rgb(self.hue, self.saturation, self.value)
return f"{int(r * 255):02X}{int(g * 255):02X}{int(b * 255):02X}"
def update_color(self):
# Update the hue to generate different colors
self.hue = (self.hue + 0.05) % 1.0 # Small step within [0.0, 1.0]
# Avoid reddish hues by constraining hue value
if self.hue < 0.1 or self.hue > 0.8:
self.hue = 0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment