Code:
#include <stdio.h>
#define RED 0
#define GREEN 1
#define BLUE 2
#define xsize 256
#define ysize 128
#define zsize 3
void main()
{
int myArray[xsize][ysize][zsize];
float r[256],g[256],b[256]; /* Arrays*/
float r1,r2,g1,g2,b1,b2,r3,g3,b3; /* Declaring 1st red value */
int red,green,blue=0;
float counter; /* Holds current value of x */
float n; /* Number of steps */
int x,y,z; /* Loop control variable on width */
int x1,x2,y1,y2;
float gradient = 0;
FILE *pfile = NULL; /* Declares the file pointer */
for (x=0;x<256;x++)
{
for (y=0;y<128;y++)
{
for (z=0;z<3;z++)
{
myArray[x][y][z]=0;
}
}
}
printf ("Input values between 0 and 255\n");
do
{
printf ("\nInput first value: ");
scanf ("%f %f %f", &r1, &g1, &b1);
}
while (r1<0||r1>255||g1<0||g1>255||b1<0||b1>255);
do
{
printf ("\nInput last value: ");
scanf ("%f %f %f", &r2, &g2, &b2);
}
while (r2<0||r2>255||g2<0||g2>255||b2<0||b2>255);
line:
do
{
printf("\nInput colour components of line: ");
scanf("%f %f %f", &r3, &g3, &b3);
}
while (r3<0||r3>255||g3<0||g3>255||b3<0||b3>255);
do
{
printf("\nInput first x co-ordinate then last e.g 20 140: ");
scanf("%i %i", &x1, &x2);
}
while (x1<0||x1>255||x2<0||x2>255);
do
{
printf("\nInput first y co-ordinate then last e.g 20 98: ");
scanf("%i %i", &y1, &y2);
}
while (y1<0||y1>255||y2<0||y2>255);
gradient = (float)(y2-y1)/(float)(x2-x1);
printf("\nGradient: %f", gradient);
printf ("\n\nThe colour gradient has been produced.\nTo view it, open the .ppm file in the current directory with Infranview.\n\n");
pfile = fopen("Gradients.ppm","w"); /* Opens the location of the saved file */
fprintf (pfile, "P3\n #Gradients.ppm\n256 128\n255\n");
x = 0; /* Loop control variable */
while (x <256)
{
counter = (int)x;
n = counter/255;
r[x] = (r1+((n)*(r2-r1)));
g[x] = (g1+((n)*(g2-g1)));
b[x] = (b1+((n)*(b2-b1)));
x = x + 1;
}
for(x = x1; x<=x2; x++)
{
y = (gradient*(x-x1)+y1);
myArray[x][y][0] = 0;
myArray[x][y][1] = 0;
myArray[x][y][2] = 222;
}
y=0;
while (y < 128) /* Loop control for the Height */
{
x = 0;
while (x <256) /* Loop control for the Width */
{
fprintf(pfile, ("%3.0f %3.0f %3.0f "),r[x], g[x], b[x]); /* Write the arrays into the file Gradients.ppm */
x++;
}
y++;
}
// y=0;
// while (y < 128) /* Loop control for the Height */
// {
// x = 0;
// while (x <256) /* Loop control for the Width */
// {
// fprintf(pfile,"%i ",myArray[x][y][RED]);
// fprintf(pfile,"%i ",myArray[x][y][GREEN]);
// fprintf(pfile,"%i ",myArray[x][y][BLUE]);
// x++;
// }
// y++;
// }
fclose(pfile); /* Closes the file */
printf("\n\nType close and hit enter to exit.\n");
scanf("%*s"); /* Prevents the command window from closing */
}