rscder-pub/test.py
2023-04-17 13:05:10 +08:00

36 lines
888 B
Python

import numpy as np
style = [
[0, 255, 0, 0],
[1, 0, 255, 0]
]
def get_color(v):
first_color = []
second_color = []
first_value = -1
second_value = -1
for s in style:
if s[0] <= v:
first_value = s[0]
first_color = s[1:]
else:
second_value = s[0]
second_color = s[1:]
break
if second_value == -1:
return np.array(style[-1][1:])
first_dis = (v - first_value) / (second_value - first_value)
second_dis = (second_value - v) / (second_value - first_value)
first_color = np.array(first_color)
second_color = np.array(second_color)
color = first_color* first_dis + second_color * second_dis
return color
get_color = np.frompyfunc(get_color, nin=1, nout=1)
value = np.array([0, 0.5, 0.7, 1])
print( np.stack(get_color(value)).reshape((2, 2, 3)))