aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_rgb_to_hex.c
blob: 6ae99a801ac2777775e0bd495fcf56e87be85b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_rgb_to_hex.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: joelecle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:28:58 by joelecle          #+#    #+#             */
/*   Updated: 2020/02/14 17:28:58 by joelecle         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <cub3d.h>
#include <stdint.h>

uint32_t
	ft_rgb_to_hex(float dist, t_rgb rgb, t_cub *cl)
{
	uint32_t	res;
	float		calc;

	calc = (dist * 0.1 * cl->mlist.darklvl);
	calc = (calc >= 255) ? (255) : (calc);
	calc = (calc < 1) ? (1) : (calc);
	rgb.r = (rgb.r > 255) ? (255) : (rgb.r);
	rgb.g = (rgb.g > 255) ? (255) : (rgb.g);
	rgb.b = (rgb.b > 255) ? (255) : (rgb.b);
	rgb.r = (rgb.r < 0) ? (0) : (rgb.r);
	rgb.g = (rgb.g < 0) ? (0) : (rgb.g);
	rgb.b = (rgb.b < 0) ? (0) : (rgb.b);
	res = 0;
	if (calc <= 1)
		res += ((rgb.r << 16) + (rgb.g << 8) + rgb.b);
	else
		res += (((uint8_t)(rgb.r / calc) << 16) + ((uint8_t)(rgb.g / calc) << 8)
			+ (uint8_t)(rgb.b / calc));
	return (res);
}