Last active
February 15, 2020 04:59
-
-
Save harukaeru/31999fbfcf47117692390425dc4675e2 to your computer and use it in GitHub Desktop.
color-gradation-generator
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
class Color: | |
def __init__(self, red, green, blue): | |
self.red = red | |
self.green = green | |
self.blue = blue | |
def __add__(self, other): | |
return Color( | |
self.red + other.red, | |
self.green + other.green, | |
self.blue + other.blue | |
) | |
def __sub__(self, other): | |
return Color( | |
self.red - other.red, | |
self.green - other.green, | |
self.blue - other.blue | |
) | |
def __truediv__(self, num): | |
return Color( | |
int(self.red / num), | |
int(self.green / num), | |
int(self.blue / num) | |
) | |
def __mul__(self, num): | |
return Color( | |
self.red * num, | |
self.green * num, | |
self.blue * num | |
) | |
def inverted(self): | |
return Color( | |
255 - self.red, | |
255 - self.green, | |
255 - self.blue, | |
) | |
@property | |
def hexred(self): | |
return hex(self.red)[2:].zfill(2) | |
@property | |
def hexgreen(self): | |
return hex(self.green)[2:].zfill(2) | |
@property | |
def hexblue(self): | |
return hex(self.blue)[2:].zfill(2) | |
def __str__(self): | |
return f'Color(red={self.hexred}, green={self.hexgreen}, blue={self.hexblue})' | |
def colorstring(self): | |
if self.is_valid(): | |
return f'#{self.hexred}{self.hexgreen}{self.hexblue}' | |
else: | |
return 'NOT VALID AS A COLOR' | |
def is_valid(self): | |
return (0 <= self.red <= 255) and (0 <= self.green <= 255) and (0 <= self.blue <= 255) | |
def get_color(c): | |
red = int(c[1:3], 16) | |
green = int(c[3:5], 16) | |
blue = int(c[5:], 16) | |
return Color(red, green, blue) | |
midnight_black = get_color('#000000') | |
early_morning_skyblue = get_color('#2d92fb') | |
daylight_yellow = get_color('#ffee00') | |
evening_orange = get_color('#ff0000') | |
def show(start_color, start_hour, end_color, end_hour): | |
num = end_hour - start_hour | |
unit_distance = (end_color - start_color) / num | |
for i in range(num): | |
color = start_color + unit_distance * i | |
print(f'{start_hour + i}h: {color.colorstring()}') | |
# print(f'<div><span style="color: white; background-color: {color.colorstring()};">{start_hour + i}h: {color.colorstring()}</span></div>') | |
show(midnight_black, 0, early_morning_skyblue, 6) | |
show(early_morning_skyblue, 6, daylight_yellow, 13) | |
show(daylight_yellow, 13, evening_orange, 19) | |
show(evening_orange, 19, midnight_black, 24) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: