-
-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Refactor colors #718
base: canon
Are you sure you want to change the base?
Changes from 13 commits
1c851e8
83dd3a4
038e82b
49ad451
0104079
4382568
4a491ac
fb85d0f
ffccb05
3766dda
0222156
107fedb
b7aa44b
76831d1
14ae1c4
6da63f3
a5a7729
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from dataclasses import dataclass | ||
import colorsys | ||
|
||
@dataclass() | ||
class Color(): | ||
"""An RGB color, with red, green, and blue values ranging from 0-255.""" | ||
red: int | ||
green: int | ||
blue: int | ||
|
||
def __post_init__(self): | ||
for key, value in {'red': self.red, 'green': self.green, 'blue': self.blue}.items(): | ||
if value < 0: | ||
raise ValueError(f'{key} cannot be less than 0.') | ||
elif value > 255: | ||
raise ValueError(f'{key} cannot be greater than 255.') | ||
|
||
def __iter__(self): | ||
return (self.red, self.green, self.blue) | ||
|
||
@staticmethod | ||
def from_hsv(hue: float, saturation: float, value: float): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather have @pathunstrom your thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Concerned about that approach - I think there should only be one class for color so that it is intuitive to users that that’s the class they should plug in whenever they need to use a color. Don’t want people to accidentally use an HSV color when the class is expecting an RGB color, for example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to refactor it into an abstract base class |
||
"""Convert the given HSV color values to an RGB color.""" | ||
red, green, blue = colorsys.hsv_to_rgb(hue, saturation, value) | ||
return Color(int(red * 256), int(green * 256), int(blue * 256)) | ||
|
||
|
||
BLACK = Color(0, 0, 0) | ||
WHITE = Color(255, 255, 255) | ||
GRAY = Color(127, 127, 127) | ||
RED = Color(255, 0, 0) | ||
GREEN = Color(0, 255, 0) | ||
BLUE = Color(0, 0, 255) | ||
CYAN = Color(0, 255, 255) | ||
MAGENTA = Color(255, 0, 255) | ||
YELLOW = Color(255, 255, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to call this
RGB
instead?