2.8k views
3 votes
Computers represent color by combining the sub-colors red, green, and blue (rgb). Each sub-color's value can range from 0 to 255. Thus (255, 0, 0) is bright red, (130, 0, 130) is a medium purple, (0, 0, 0) is black, (255, 255, 255) is white, and (40, 40, 40) is a dark gray. (130, 50, 130) is a faded purple, due to the (50, 50, 50) gray part. (In other words, equal amounts of red, green, blue yield gray). Given values for red, green, and blue, remove the gray part.

Ex: If the input is 130 50 130, the output is: 80 0 80

Required:
Find the smallest value, and then subtract it from all three values, thus removing the gray.

User Mvherweg
by
3.9k points

1 Answer

2 votes

Answer:

The programming language is not stated; However, I'll answer the question using C++ programming language,

This program uses comments for explanations

Program starts here

#include<iostream>

using namespace std;

int main()

{

//Declare Array

int colorcode[3],graycode[3];

//Prompt user for color code

cout<<"Enter Color Code (One on a line)"<<endl<<"Inout Range is 0 to 255"<<endl;

for(int i = 0;i<3;i++)

{

ccode:

cout<<"Enter Color Code "<<i+1<<": ";

cin>>colorcode[i];

if(colorcode[i]<0 || colorcode[i]>255)

{

cout<<"Input Range is 0 to 255"<<'\\';

goto ccode;

}

}

//Initialize smallest to colorcode[0]

int smallest = colorcode[0];

//Determine smallest

for(int i = 0;i<3;i++)

{

if(colorcode[i]<smallest)

{

smallest = colorcode[i];

}

}

//Determine Graycode

for(int i = 0;i<3;i++)

{

graycode[i] = colorcode[i] - smallest;

}

//Display Result:

cout<<"Color Code: "<<"(";

for(int i = 0;i<3;i++)

{

if(i!=2)

{

cout<<colorcode[i]<<",";

}

else

{

cout<<colorcode[i]<<")"<<endl;

}

}

cout<<"Gray Code: "<<"(";

for(int i = 0;i<3;i++)

{

if(i!=2)

{

cout<<graycode[i]<<",";

}

else

{

cout<<graycode[i]<<")"<<endl;

}

}

return 0;

}

User Limos
by
4.7k points