2018-03-30 18:42:32 -07:00
|
|
|
import numpy as np
|
2018-03-30 18:19:23 -07:00
|
|
|
import random
|
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
from colour import Color
|
2018-04-01 10:51:54 -07:00
|
|
|
from constants import WHITE
|
|
|
|
from constants import PALETTE
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
from utils.bezier import interpolate
|
2018-08-15 16:23:29 -07:00
|
|
|
from utils.space_ops import get_norm
|
2018-03-30 18:19:23 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def color_to_rgb(color):
|
|
|
|
return np.array(Color(color).get_rgb())
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
|
|
|
def color_to_rgba(color, alpha=1):
|
2018-03-30 18:19:23 -07:00
|
|
|
return np.append(color_to_rgb(color), [alpha])
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def rgb_to_color(rgb):
|
|
|
|
try:
|
2018-04-06 13:58:59 -07:00
|
|
|
return Color(rgb=rgb)
|
2018-03-30 18:19:23 -07:00
|
|
|
except:
|
|
|
|
return Color(WHITE)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def rgba_to_color(rgba):
|
|
|
|
return rgb_to_color(rgba[:3])
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def rgb_to_hex(rgb):
|
2018-04-06 13:58:59 -07:00
|
|
|
return "#" + "".join('%02x' % int(255 * x) for x in rgb)
|
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
|
|
|
|
def invert_color(color):
|
|
|
|
return rgb_to_color(1.0 - color_to_rgb(color))
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def color_to_int_rgb(color):
|
2018-04-06 13:58:59 -07:00
|
|
|
return (255 * color_to_rgb(color)).astype('uint8')
|
2018-03-30 18:19:23 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-05-21 12:11:46 -07:00
|
|
|
def color_to_int_rgba(color, opacity=1.0):
|
|
|
|
alpha = int(255 * opacity)
|
2018-03-30 18:19:23 -07:00
|
|
|
return np.append(color_to_int_rgb(color), alpha)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def color_gradient(reference_colors, length_of_output):
|
|
|
|
if length_of_output == 0:
|
|
|
|
return reference_colors[0]
|
2018-08-09 17:56:05 -07:00
|
|
|
rgbs = list(map(color_to_rgb, reference_colors))
|
2018-03-30 18:19:23 -07:00
|
|
|
alphas = np.linspace(0, (len(rgbs) - 1), length_of_output)
|
|
|
|
floors = alphas.astype('int')
|
|
|
|
alphas_mod1 = alphas % 1
|
2018-04-06 13:58:59 -07:00
|
|
|
# End edge case
|
2018-03-30 18:19:23 -07:00
|
|
|
alphas_mod1[-1] = 1
|
|
|
|
floors[-1] = len(rgbs) - 2
|
|
|
|
return [
|
2018-04-06 13:58:59 -07:00
|
|
|
rgb_to_color(interpolate(rgbs[i], rgbs[i + 1], alpha))
|
2018-03-30 18:19:23 -07:00
|
|
|
for i, alpha in zip(floors, alphas_mod1)
|
|
|
|
]
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def interpolate_color(color1, color2, alpha):
|
|
|
|
rgb = interpolate(color_to_rgb(color1), color_to_rgb(color2), alpha)
|
|
|
|
return rgb_to_color(rgb)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def average_color(*colors):
|
2018-08-09 17:56:05 -07:00
|
|
|
rgbs = np.array(list(map(color_to_rgb, colors)))
|
2018-03-30 18:19:23 -07:00
|
|
|
mean_rgb = np.apply_along_axis(np.mean, 0, rgbs)
|
|
|
|
return rgb_to_color(mean_rgb)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
def random_bright_color():
|
|
|
|
color = random_color()
|
|
|
|
curr_rgb = color_to_rgb(color)
|
|
|
|
new_rgb = interpolate(
|
|
|
|
curr_rgb, np.ones(len(curr_rgb)), 0.5
|
|
|
|
)
|
2018-04-06 13:58:59 -07:00
|
|
|
return Color(rgb=new_rgb)
|
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
|
|
|
|
def random_color():
|
2018-04-06 13:58:59 -07:00
|
|
|
return random.choice(PALETTE)
|
2018-08-15 16:23:29 -07:00
|
|
|
|
|
|
|
|
|
|
|
def get_shaded_rgb(rgb, point, unit_normal_vect, light_source):
|
|
|
|
to_sun = light_source - point
|
|
|
|
to_sun /= get_norm(to_sun)
|
|
|
|
factor = 0.5 * np.dot(unit_normal_vect, to_sun)**3
|
|
|
|
if factor < 0:
|
|
|
|
factor *= 0.5
|
|
|
|
return np.clip(rgb + factor, 0, 1)
|