Skip to content

Instantly share code, notes, and snippets.

@harukaeru
Last active February 15, 2020 04:59
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 harukaeru/31999fbfcf47117692390425dc4675e2 to your computer and use it in GitHub Desktop.
Save harukaeru/31999fbfcf47117692390425dc4675e2 to your computer and use it in GitHub Desktop.
color-gradation-generator
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)
@harukaeru
Copy link
Author

Result:

0h: #000000
1h: #071829
2h: #0e3052
3h: #15487b
4h: #1c60a4
5h: #2378cd
6h: #2d92fb
7h: #4b9fd8
8h: #69acb5
9h: #87b992
10h: #a5c66f
11h: #c3d34c
12h: #e1e029
13h: #ffee00
14h: #ffc700
15h: #ffa000
16h: #ff7900
17h: #ff5200
18h: #ff2b00
19h: #ff0000
20h: #cc0000
21h: #990000
22h: #660000
23h: #330000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment