#! /usr/bin/env python3

from numpy import *
from scipy.misc import imsave

def quartic_kernel(x):
    if not (-1.0 < x < 1.0):
        return 0.0
    return 15.0 / 16.0 * (1 - x ** 2) ** 2

class Gradient:

    def __init__(self):
        self.colors = []
        self.steps = []

    def add_color(self, color, step):
        self.colors.append(array(color))
        self.steps.append(step)

    def value(self, at):
        i = 0
        while not (self.steps[i] <= at <= self.steps[i + 1]):
            i += 1

        d = 1 - (self.steps[i + 1] - at) / (self.steps[i + 1] - self.steps[i])
        fr = self.colors[i]
        to = self.colors[i + 1]

        return (to - fr) * d + fr

    def to_image(self, alpha=False):
        img = zeros((8, 256, 4))
        for x_ in range(img.shape[1]):
            x = x_ / img.shape[1]
            val = self.value(x)
            for i in range(img.shape[0]):
                img[i][x_] = val
                if not alpha:
                    img[i][x_][3] = 1.0
        return img

    def to_csrc(self, name):
        ret = "rgba_t " + name + "[] = {\n\t{    0,    0,    0,    0 },\n"
        for x in range(1, 256):
            clr = self.value(x / 255)
            ret += "\t{ % 4d, % 4d, % 4d, % 4d },\n" % \
                tuple(min(int(c * 256), 255) for c in clr)
        ret += "};\n"
        return ret

gradient_heat = Gradient()
gradient_heat.add_color([0.0, 0.0, 0.0, 0.3], 0.00)
gradient_heat.add_color([0.0, 0.0, 1.0, 0.3], 0.15)
gradient_heat.add_color([0.0, 0.5, 1.0, 0.3], 0.25)
gradient_heat.add_color([0.0, 1.0, 0.0, 0.3], 0.40)
gradient_heat.add_color([1.0, 1.0, 0.0, 0.4], 0.60)
gradient_heat.add_color([1.0, 0.5, 0.0, 0.5], 0.80)
gradient_heat.add_color([1.0, 0.0, 0.0, 0.6], 1.00)

gradient_grayscale = Gradient()
gradient_grayscale.add_color([0.0, 0.0, 0.0, 0.3], 0.00)
gradient_grayscale.add_color([1.0, 1.0, 1.0, 0.6], 1.00)

gradients = [
    ("heat", gradient_heat),
    ("grayscale", gradient_grayscale)
]

print()
print("#include \"colormaps.h\"")
print()
print("// Generated by utils/gencolormaps.py")
print()
for name, grad in gradients:
    print(grad.to_csrc("colormap_" + name))
    imsave("gradient_" + name + ".png", grad.to_image())

#! /usr/bin/env python3

from numpy import *
from scipy.misc import imsave

def quartic_kernel(x):
    if not (-1.0 < x < 1.0):
        return 0.0
    return 15.0 / 16.0 * (1 - x ** 2) ** 2

class Gradient:

    def __init__(self):
        self.colors = []
        self.steps = []

    def add_color(self, color, step):
        self.colors.append(array(color))
        self.steps.append(step)

    def value(self, at):
        i = 0
        while not (self.steps[i] <= at <= self.steps[i + 1]):
            i += 1

        d = 1 - (self.steps[i + 1] - at) / (self.steps[i + 1] - self.steps[i])
        fr = self.colors[i]
        to = self.colors[i + 1]

        return (to - fr) * d + fr

    def to_image(self, alpha=False):
        img = zeros((8, 256, 4))
        for x_ in range(img.shape[1]):
            x = x_ / img.shape[1]
            val = self.value(x)
            for i in range(img.shape[0]):
                img[i][x_] = val
                if not alpha:
                    img[i][x_][3] = 1.0
        return img

    def to_csrc(self, name):
        ret = "rgba_t " + name + "[] = {\n\t{    0,    0,    0,    0 },\n"
        for x in range(1, 256):
            clr = self.value(x / 255)
            ret += "\t{ % 4d, % 4d, % 4d, % 4d },\n" % \
                tuple(min(int(c * 256), 255) for c in clr)
        ret += "};\n"
        return ret

gradient_heat = Gradient()
gradient_heat.add_color([0.0, 0.0, 0.0, 0.3], 0.00)
gradient_heat.add_color([0.0, 0.0, 1.0, 0.3], 0.15)
gradient_heat.add_color([0.0, 0.5, 1.0, 0.3], 0.25)
gradient_heat.add_color([0.0, 1.0, 0.0, 0.3], 0.40)
gradient_heat.add_color([1.0, 1.0, 0.0, 0.4], 0.60)
gradient_heat.add_color([1.0, 0.5, 0.0, 0.5], 0.80)
gradient_heat.add_color([1.0, 0.0, 0.0, 0.6], 1.00)

gradient_grayscale = Gradient()
gradient_grayscale.add_color([0.0, 0.0, 0.0, 0.3], 0.00)
gradient_grayscale.add_color([1.0, 1.0, 1.0, 0.6], 1.00)

gradients = [
    ("heat", gradient_heat),
    ("grayscale", gradient_grayscale)
]

print()
print("#include \"colormaps.h\"")
print()
print("// Generated by utils/gencolormaps.py")
print()
for name, grad in gradients:
    print(grad.to_csrc("colormap_" + name))
    imsave("gradient_" + name + ".png", grad.to_image())

#! /usr/bin/env python3