From: Lukas Jiriste Date: Sat, 18 Jan 2025 14:56:04 +0000 (+0100) Subject: Add clamping to color from input file X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;p=42%2FminiRT.git Add clamping to color from input file --- diff --git a/src/parse_small.c b/src/parse_small.c index 65dee7f..f4b99c2 100644 --- a/src/parse_small.c +++ b/src/parse_small.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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)); } +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); - 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); - 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); - color.b = node_to_int(int_node); + color.b = clamp_itoc(node_to_int(int_node)); return (color_srgb_to_lin(color)); }