Skip to content

Instantly share code, notes, and snippets.

@jackersson
Created September 17, 2019 09:30
Show Gist options
  • Save jackersson/92102a6d21913beeac46630485506851 to your computer and use it in GitHub Desktop.
Save jackersson/92102a6d21913beeac46630485506851 to your computer and use it in GitHub Desktop.
class ColorPicker:
def __init__(self, channels: int = 4):
self._color_by_id = {}
self._channels = channels
def get(self, idx):
if idx not in self._color_by_id:
self._color_by_id[idx] = self._generate_color(channels=self._channels)
return self._color_by_id[idx]
@staticmethod
def _generate_color(low: int = 0, high: int = 255, channels: int = 4):
if channels == 4:
return (random.randint(low, high), random.randint(low, high), random.randint(low, high), 255)
return (random.randint(low, high), random.randint(low, high), random.randint(low, high))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment