Generalize palette construction
authorLukas Jiriste <ljiriste@student.42prague.com>
Sat, 11 Nov 2023 17:44:45 +0000 (18:44 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 11 Nov 2023 17:44:45 +0000 (18:44 +0100)
Add general approach to constructing palletes with "equidistant
partitioning" of colors. Change mandelbrot in main accordingly, but it
should probably be separated to another file (fractals.c?)
Remove (comment out) enhancement of computed fractal when no event
happens, because it causes valgrind to report issues. Further
investigation needed.

color.c
color.h
main.c

diff --git a/color.c b/color.c
index a146089d0efa026f284a3159f62e24dc29a4e487..f3b481fc319b842060dfdbc54a06c1b97bcc6ffd 100644 (file)
--- a/color.c
+++ b/color.c
@@ -1,4 +1,5 @@
 
+#include <stddef.h>
 #include "color.h"
 
 t_color        to_color(unsigned int num)
@@ -20,10 +21,10 @@ t_color     lin_interp_color(t_color zero, t_color one, double norm_par)
                norm_par = 0.;
        else if (norm_par > 1.)
                norm_par = 1.;
-       res.a = zero.a + norm_par * one.a;
-       res.r = zero.r + norm_par * one.r;
-       res.g = zero.g + norm_par * one.g;
-       res.b = zero.b + norm_par * one.b;
+       res.a = (1 - norm_par) * zero.a + norm_par * one.a;
+       res.r = (1 - norm_par) * zero.r + norm_par * one.r;
+       res.g = (1 - norm_par) * zero.g + norm_par * one.g;
+       res.b = (1 - norm_par) * zero.b + norm_par * one.b;
        return (res);
 }
 
@@ -34,24 +35,31 @@ t_color     basic_palette(double normalized_par)
        return (lin_interp_color(to_color(0x000000FF), to_color(0x00FFFF00), normalized_par));
 }
 
-t_color        double_grad(double normalized_par)
+t_color        general_palette(double np, const t_color *array, size_t size)
 {
-       const double    breakpoint = 0.1;
+       size_t  i;
 
-       if (normalized_par < breakpoint)
-               return (lin_interp_color(to_color(0x00000000), to_color(0x000000FF), normalized_par / breakpoint));
-       return (lin_interp_color(to_color(0x000000FF), to_color(0x00FFFF00), (normalized_par - breakpoint) / breakpoint));
+       if (!array)
+               return (to_color(0x00000000));
+       if (np < 0 || size <= 1)
+               return (array[0]);
+       i = 1;
+       while (i + 1 < size)
+       {
+               if (np < (i + 1)/(double)size)
+                       return (lin_interp_color(array[i], array[i + 1], np * size - i));
+               ++i;
+       }
+       return (lin_interp_color(array[i], array[1], np * size - i));
 }
 
 t_color        tri_color(double np)
 {
-       if (np < 0)
-               np = 0;
-       else if (np > 1)
-               np = 1;
-       if (np < 1/3.)
-               return (lin_interp_color(to_color(0x000000FF), to_color(0x00FFFF00), np * 3));
-       if (np < 2/3.)
-               return (lin_interp_color(to_color(0x00FFFF00), to_color(0x00FF0000), np * 3 - 1));
-       return (lin_interp_color(to_color(0x00FF0000), to_color(0x000000FF), np * 3 - 2));
+       t_color colors[4];
+
+       colors[0] = to_color(0x00000000);
+       colors[1] = to_color(0x000000FF);
+       colors[2] = to_color(0x00FFFF00);
+       colors[3] = to_color(0x00FF0000);
+       return (general_palette(np, colors, 4));
 }
diff --git a/color.h b/color.h
index 8a67ca0b1b7ed45ffc02bfed955bb2f0a423bec3..96fa09208664fb6e72dc0b093dd9e418653b8780 100644 (file)
--- a/color.h
+++ b/color.h
@@ -13,7 +13,7 @@ typedef struct s_color t_color;
 
 t_color        lin_interp_color(t_color zero, t_color one, double norm_par);
 t_color        basic_palette(double normalized_par);
-t_color        double_grad(double normalized_par);
+t_color        general_palette(double np, const t_color *array, size_t size);
 t_color        tri_color(double np);
 
 #endif
diff --git a/main.c b/main.c
index cbf446e6b64228b18f7f4fced871f0734d002dde..c643fcdea83662c95dc8c1c4746ec4f3cf5b1940 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/10/27 14:29:26 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/11/11 17:31:25 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/11/11 18:41:55 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -90,7 +90,7 @@ double        mandelbrot(double x, double y, double resolution)
                ++count;
        }
        if (count == max_count)
-               return (0);
+               return (-1);
        return (fmod(count / 100., 1.));
 }
 
@@ -159,9 +159,15 @@ int        handle_mouse_press(int button, int x, int y, t_session *s)
        return (0);
 }
 
-int    no_event_handle(__attribute__((unused)) t_session *s)
+/*     valgrind indicates an issue when the draw_fractal is in proccess
+ *     while and ESC is pressed thus ending the program.
+ *
+ *     Setting the condition to less than 1 basically turns this redrawing
+ *     off.
+ */
+int    no_event_handle(t_session *s)
 {
-       if (s->view.resolution < 100)
+       if (s->view.resolution < 1)
        {
                s->view.draw_whole = 0;
                s->view.resolution *= 2;
@@ -187,8 +193,8 @@ void        parse_args(int argc, char **argv, t_session *s)
 {
        if (argc == 0)
                free(argv);
-       s->img.width = 1024;
-       s->img.height = 768;
+       s->img.width = 200;
+       s->img.height = 200;
        init_view(s);
        return ;
 }