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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define ETAPE 2 /* choisis au hasard */
typedef struct
{
unsigned char r; /* red */
unsigned char g; /* green */
unsigned char b; /* blue */
unsigned char dummy; /* alignment */
} color;
int calculerCouleur (color const *color1, color const *color2,
color ** tabColor, int etape);
int main (void)
{
int ret = EXIT_FAILURE;
color *tabColor = NULL; /* tableau contenant les couleurs intermédiaires */
color color1 = { 153, 255, 0 };
color color2 = { 204, 102, 204 };
if (calculerCouleur (&color1, &color2, &tabColor, ETAPE))
{
int i;
for (i = 0; i < ETAPE + 2; i++)
{
printf ("rgb(%hhu,%hhu,%hhu)\n", tabColor[i].r, tabColor[i].g,
tabColor[i].b);
}
ret = EXIT_SUCCESS;
}
return ret;
}
int calculerCouleur (color const *color1, color const *color2,
color ** tabColor, int step)
{
int ret = 0;
color pattern;
step++;
pattern.r =
(unsigned char) floor ((color2->r - color1->r) / (double) step + 0.5);
pattern.g =
(unsigned char) floor ((color2->g - color1->g) / (double) step + 0.5);
pattern.b =
(unsigned char) floor ((color2->b - color1->b) / (double) step + 0.5);
*tabColor = malloc ((step + 1) * sizeof **tabColor);
if (*tabColor != NULL)
{
color *tab = *tabColor;
int i = 0;
tab[0] = *color1;
tab[step] = *color2;
for (i = 1; i < step; i++)
{
tab[i].r = (unsigned char) (color1->r + pattern.r * i);
tab[i].g = (unsigned char) (color1->g + pattern.g * i);
tab[i].b = (unsigned char) (color1->b + pattern.b * i);
}
ret = 1;
}
return ret;
} |
Partager