En returnant just un long on peut obtenir directement la valeur RGB du bail h
unsigned long createRGB(int r, int g, int b)
{
return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* to_hexa(int number) {
int result = -1;
int index = 1;
char* hexa_conversion = NULL;
hexa_conversion = malloc(sizeof(char)*2);
char hexa[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
if(number <= 0) {
return "00";
}
if(number >= 255) {
return "FF";
}
while(result != 0) {
result = number / 16;
hexa_conversion[index] = hexa[number%16];
if(result == 0 && index == 1) {
hexa_conversion[0] = hexa[0];
return hexa_conversion;
}
number = result;
index--;
}
return hexa_conversion;
}
char* rgb(int r, int g, int b, char *output)
{
printf("%s", output);
strcat(output, to_hexa(r));
strcat(output, to_hexa(g));
strcat(output, to_hexa(b));
return output;
}