"""
Implementation of the node algorith to convert a position to a color
"""


class OgnPositionToColor:
    """
    This node takes positional data (double3) and converts to a color, and outputs
    as color3f[] (which seems to be the default color connection in USD)
    """

    @staticmethod
    def compute(db):
        """Run the algorithm to convert a position to a color"""

        position = db.inputs.position
        scale_value = db.inputs.scale
        color_offset = db.inputs.color_offset

        color = (abs(position[:])) / scale_value
        color += color_offset

        ramp = (scale_value - abs(position[0])) / scale_value
        ramp = max(ramp, 0)
        color[0] -= ramp
        if color[0] < 0:
            color[0] = 0
        color[1] += ramp
        if color[1] > 1:
            color[1] = 1
        color[2] -= ramp
        if color[2] < 0:
            color[2] = 0

        db.outputs.color_size = 1
        db.outputs.color[0] = color

        return True
