diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/color.c | 114 | ||||
| -rw-r--r-- | src/event.c | 10 | ||||
| -rw-r--r-- | src/render.c | 11 | ||||
| -rw-r--r-- | src/state.c | 76 |
4 files changed, 153 insertions, 58 deletions
diff --git a/src/color.c b/src/color.c new file mode 100644 index 0000000..7fabc9c --- /dev/null +++ b/src/color.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* color.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/25 14:36:12 by cacharle #+# #+# */ +/* Updated: 2020/02/25 15:23:44 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fractol.h" + +t_color_hsl color_rgb_to_hsl(t_color color_rgb) +{ + t_color_hsl color_hsl; + double h; + double s; + double l; + double r; + double g; + double b; + + r = color_rgb.rgb.r / 256.0; + g = color_rgb.rgb.g / 256.0; + b = color_rgb.rgb.b / 256.0; + + double maxColor = ft_fmax(r, ft_fmax(g, b)); + double minColor = ft_fmin(r, ft_fmin(g, b)); + if (maxColor == minColor) // grey + { + h = 0.0; + s = 0.0; + l = minColor; + } + else + { + l = (maxColor + minColor) / 2.0; + + if (l < 0.5) + s = (maxColor - minColor) / (maxColor + minColor); + else + s = (maxColor - minColor) / (2.0 - maxColor - minColor); + + if (r == maxColor) + h = (g - b) / (maxColor - minColor); + else if (g == maxColor) + h = 2.0 + (b - r) / (maxColor - minColor); + else + h = 4.0 + (r - g) / (maxColor - minColor); + + h /= 6; //to bring it to a number between 0 and 1 + if (h < 0) + h++; + } + color_hsl.h = (int)(h * 256.0); + color_hsl.s = (int)(s * 256.0); + color_hsl.l = (int)(l * 256.0); + return (color_hsl); +} + +t_color color_hsl_to_rgb(t_color_hsl color_hsl) +{ + t_color color_rgb; + double h; + double s; + double l; + double r; + double g; + double b; + double temp1, temp2, tempr, tempg, tempb; + + h = color_hsl.h / 256.0; + s = color_hsl.s / 256.0; + l = color_hsl.l / 256.0; + if(s == 0) + { + r = l; + g = l; + b = l; + } + else + { + if (l < 0.5) temp2 = l * (1 + s); + else temp2 = (l + s) - (l * s); + temp1 = 2 * l - temp2; + tempr = h + 1.0 / 3.0; + if (tempr > 1) tempr--; + tempg = h; + tempb = h - 1.0 / 3.0; + if (tempb < 0) tempb++; + + if (tempr < 1.0 / 6.0) r = temp1 + (temp2 - temp1) * 6.0 * tempr; + else if (tempr < 0.5) r = temp2; + else if (tempr < 2.0 / 3.0) r = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempr) * 6.0; + else r = temp1; + + if (tempg < 1.0 / 6.0) g = temp1 + (temp2 - temp1) * 6.0 * tempg; + else if (tempg < 0.5) g = temp2; + else if (tempg < 2.0 / 3.0) g = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempg) * 6.0; + else g = temp1; + + if(tempb < 1.0 / 6.0) b = temp1 + (temp2 - temp1) * 6.0 * tempb; + else if(tempb < 0.5) b = temp2; + else if(tempb < 2.0 / 3.0) b = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempb) * 6.0; + else b = temp1; + } + color_rgb.hexcode = 0x0; + color_rgb.rgb.r = (int)(r * 256.0); + color_rgb.rgb.g = (int)(g * 256.0); + color_rgb.rgb.b = (int)(b * 256.0); + return (color_rgb); +} diff --git a/src/event.c b/src/event.c index 2c59e9d..42aaa93 100644 --- a/src/event.c +++ b/src/event.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:52:35 by cacharle #+# #+# */ -/* Updated: 2020/02/25 07:35:38 by cacharle ### ########.fr */ +/* Updated: 2020/02/25 15:24:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,9 +35,17 @@ int event_keydown(int key, t_state *state) else if (key == MLXK_RIGHT) state->center.r += state->plane.r * MOVE_SPEED; else if (key == MLXK_PLUS) + { state->iterations++; + state_update_palette(state); + } else if (key == MLXK_MINUS) + { state->iterations--; + if (state->iterations < 1) + state->iterations = 1; + state_update_palette(state); + } else return (0); state->updated = false; diff --git a/src/render.c b/src/render.c index 925e0dc..a217125 100644 --- a/src/render.c +++ b/src/render.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:54:59 by cacharle #+# #+# */ -/* Updated: 2020/02/25 08:42:34 by cacharle ### ########.fr */ +/* Updated: 2020/02/25 15:30:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,7 @@ static void st_render_fractal(t_state *state) t_color color; t_complex z; /* pthread_t threads[WINDOW_HEIGHT */ + int tmp; color.hexcode = 0xffffff; offset = 0; @@ -36,7 +37,13 @@ static void st_render_fractal(t_state *state) { z.r = ((double)j / (double)WINDOW_WIDTH) * state->plane.r - (state->plane.r / 2.0) + state->center.r; z.i = ((double)i / (double)WINDOW_HEIGHT) * state->plane.i - (state->plane.i / 2.0) + state->center.i; - ((t_color*)state->window.data)[offset] = state->palette[(int)(((double)state->func(state, z) / (double)state->iterations) * (double)PALETTE_SIZE) % PALETTE_SIZE]; + tmp = state->func(state, z); + /* if (tmp > state->iterations) */ + /* ((t_color*)state->window.data)[offset].hexcode = 0x000000; */ + /* else */ + /* for (int i = 0; i < state->iterations; i++) */ + /* printf("%d %d %d\n", state->palette[tmp].rgb.r, state->palette[tmp].rgb.g, state->palette[tmp].rgb.b); */ + ((t_color*)state->window.data)[offset] = state->palette[tmp]; offset++; } } diff --git a/src/state.c b/src/state.c index 23a722d..1e276f7 100644 --- a/src/state.c +++ b/src/state.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:58:01 by cacharle #+# #+# */ -/* Updated: 2020/02/25 08:42:51 by cacharle ### ########.fr */ +/* Updated: 2020/02/25 15:34:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,63 +17,28 @@ /* #define PALETTE_START 0x000000 */ /* #define PALETTE_END 0xffffff */ -static void st_state_init_palette(t_state *state) +void state_update_palette(t_state *state) { - t_color tmp; - int step_r; - int step_g; - int step_b; - int i; - t_color colors[5]; + int i; + t_color_hsl hsl; - colors[0].hexcode = 0x0; - colors[1].hexcode = 0xffdf00; - /* colors[2].hexcode = 0xffffff; */ - /* colors[3].hexcode = 0x0000ff; */ - /* colors[4].hexcode = 0x000088; */ - - int c1 = 0; - int c2 = 1; - int block_size = PALETTE_SIZE / 2; + free(state->palette); + if ((state->palette = malloc(sizeof(t_color) * (state->iterations + 1))) == NULL) + { + state->running = false; + return ; + } i = -1; - double colorDelta = 1.0 / (2 - 1); - while (++i < PALETTE_SIZE) + while (++i < state->iterations) { - double globalRel = (double)i / (PALETTE_SIZE - 1); - double localRel = (globalRel - c1 * colorDelta) / colorDelta; - if (i % block_size) - { - c1++; - c2++; - } - int dr = colors[c1].rgb.r - colors[c2].rgb.r; - int dg = colors[c1].rgb.g - colors[c2].rgb.g; - int db = colors[c1].rgb.b - colors[c2].rgb.b; - - state->palette[i].rgb.r = (int)(colors[c1].rgb.r + localRel * dr); - state->palette[i].rgb.g = (int)(colors[c1].rgb.g + localRel * dg); - state->palette[i].rgb.b = (int)(colors[c1].rgb.b + localRel * db); + hsl.h = (int)(255.0 * ((double)i / (double)state->iterations)); + hsl.s = 100; + hsl.l = 127; + state->palette[i] = color_hsl_to_rgb(hsl); } - - - - - - - - /* tmp.hexcode = PALETTE_START; */ - /* step_r = ft_abs((PALETTE_END >> 16) - (PALETTE_START >> 16) ) / PALETTE_SIZE; */ - /* step_g = ft_abs(((PALETTE_END >> 8) & 0xff) - ((PALETTE_START >> 8) & 0xff)) / PALETTE_SIZE; */ - /* step_b = ft_abs((PALETTE_END & 0xff) - (PALETTE_START & 0xff) ) / PALETTE_SIZE; */ - /* i = -1; */ - /* while (++i < PALETTE_SIZE) */ - /* { */ - /* #<{(| tmp.rgb.r = (int)sqrt((double)i / PALETTE_SIZE);//tmp; |)}># */ - /* state->palette[i] = tmp; */ - /* tmp.rgb.r += step_r; */ - /* tmp.rgb.g += step_g; */ - /* tmp.rgb.b += step_b; */ - /* } */ + state->palette[i].hexcode = 0x0; + /* for (int i = 0; i < state->iterations; i++) */ + /* printf("%d %d %d\n", state->palette[i].rgb.r, state->palette[i].rgb.g, state->palette[i].rgb.b); */ } static int st_state_dispatch_func(t_state *state, char *fractal_name) @@ -117,8 +82,9 @@ int state_init(t_state *state, char *fractal_name) state->center.i = 0.0; state->plane.r = 4.0; state->plane.i = 4.0; - state->iterations = 20; - st_state_init_palette(state); + state->iterations = 30; + state->palette = NULL; + state_update_palette(state); state->updated = false; return (0); } |
