/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   color2.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: vvobis <marvin@42.fr>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/11/26 18:58:11 by vvobis            #+#    #+#             */
/*   Updated: 2025/05/31 13:31:42 by victor           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../../minirt.h"

uint color_to_uint(Color c) {
    return (c.r << 16 | c.g << 8 | c.b );
}

Color uint_to_color(uint c) {
    return (Color){(c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff, 0xff};
}

Color set_color(uint r, uint g, uint b) {
    return (Color){r & 0xff, g & 0xff, b & 0xff, 0xff};
}

void    color_print(uint color, int fd)
{
    ft_fprintf(fd, " %u,%u,%u\n", (color >> 16) & 0xff, \
            (color >> 8) & 0xff, color & 0xff);
}

uint    mix_colors(uint base_color, uint reflected_color, double reflectivity)
{
    int    final_color;

    final_color = ((uint)((1.0 - reflectivity) * ((base_color >> 16) & 0xFF) \
                    + reflectivity * ((reflected_color >> 16) & 0xFF)) << 16) \
                    | ((uint)((1.0 - reflectivity) * ((base_color >> 8) & 0xFF) \
                    + reflectivity * ((reflected_color >> 8) & 0xFF)) << 8) \
                    | (uint)((1.0 - reflectivity) * (base_color & 0xFF) \
                    + reflectivity * (reflected_color & 0xFF));
    if (final_color > 0xffffff)
        final_color = 0xffffff;
    else if (final_color < 0)
        final_color = 0;
    return (final_color);
}

uint    parse_body_color(char *params[], int *error)
{
    return (color_to_uint(set_color(set_signed_int(params[0], 0, UCHAR_MAX, error), \
                set_signed_int(params[1], 0, UCHAR_MAX, error), \
                set_signed_int(params[2], 0, UCHAR_MAX, error))));
}
