Add clamping to color from input file trunk
authorLukas Jiriste <ljiriste@student.42prague.com>
Sat, 18 Jan 2025 14:56:04 +0000 (15:56 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 18 Jan 2025 14:56:04 +0000 (15:56 +0100)
src/parse_small.c

index 65dee7ff82f2017b1eb54504896e098b1c45eb7e..f4b99c227cfd067936209abc9901b5c8875969dd 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/01/14 15:04:18 by ljiriste          #+#    #+#             */
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/01/14 15:04:18 by ljiriste          #+#    #+#             */
-/*   Updated: 2025/01/14 15:13:05 by ljiriste         ###   ########.fr       */
+/*   Updated: 2025/01/18 15:54:27 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -45,17 +45,27 @@ double      node_to_double(const t_parse_tree_node *double_node)
        return (sign * (whole + decimal));
 }
 
        return (sign * (whole + decimal));
 }
 
+static char    clamp_itoc(int a)
+{
+       if (a < 0)
+               return (0);
+       else if (a > 255)
+               return ((unsigned char)255);
+       else
+               return (a);
+}
+
 t_color        node_to_linear_rgb(const t_parse_tree_node *rgb_node)
 {
        const t_parse_tree_node *int_node;
        t_color_srgb                    color;
 
        int_node = ft_cget_node_child(rgb_node, 0);
 t_color        node_to_linear_rgb(const t_parse_tree_node *rgb_node)
 {
        const t_parse_tree_node *int_node;
        t_color_srgb                    color;
 
        int_node = ft_cget_node_child(rgb_node, 0);
-       color.r = node_to_int(int_node);
+       color.r = clamp_itoc(node_to_int(int_node));
        int_node = ft_cget_node_child(rgb_node, 2);
        int_node = ft_cget_node_child(rgb_node, 2);
-       color.g = node_to_int(int_node);
+       color.g = clamp_itoc(node_to_int(int_node));
        int_node = ft_cget_node_child(rgb_node, 4);
        int_node = ft_cget_node_child(rgb_node, 4);
-       color.b = node_to_int(int_node);
+       color.b = clamp_itoc(node_to_int(int_node));
        return (color_srgb_to_lin(color));
 }
 
        return (color_srgb_to_lin(color));
 }