Question

C ++ Exercises (TV)

Program 2: BACK AWAY FROM THE TV!  If you sat close enough to an old TV, you could see individual (R)ed, (G)reen and (B)lue (or RGB) “phosphors” that could represent just about any color you could imagine (using “additive color model”).  When these three-color components are at their maximum values, the resulting color is white from a distance (which is amazing – look at your screen with a magnifying glass!).  When all are off, the color results in black.  If red and green are fully on, you’d get a shade of yellow; red and blue on would result in purple, and so on.  For computers, each color component is usually represented by one byte (8 bits), and there are 256 different values (0-255) for each.  To find the “inverse” of a color (like double-clicking your iFone® button), you subtract the RGB values from 255.  The “luminance” (or brightness) of the color = (0.2126*R + 0.7152*G + 0.0722*B).
 For this program, you need to design (pseudocode) and implement (source code) a Color class that has R, G and B attributes (which can be ints).  The constructor should take three parameters representing the initial color of (R:254, B:2, G:100).  You should include 6 setter methods to increase and decrease each component (e.g. increaseRed), not to exceed 255 or be less than 0.  You should include a toString() that returns a string representing the current values for each component as well as the luminance.  Finally, you should include a method that calculates and prints the inverse color.  
 Next, create a “driver” (or main) that creates a default color, prints its values to the screen, and enables the user to increase/decrease values as well as print the inverse.  It should behave like below.
 Sample run:
 R:254 G:2 B:100 L:62.6508 Do you want to: 1) Increase Red, 2) Decrease Red 3) Increase Green, 4) Decrease Green 5) Increase Blue, 6) Decrease Blue 7) Print the inverse or 8) Quit 1 R:255 G:2 B:100 L:62.8634 Do you want to: 1) Increase Red, 2) Decrease Red 3) Increase Green, 4) Decrease Green 5) Increase Blue, 6) Decrease Blue 7) Print the inverse or 8) Quit 1 R:255 G:2 B:100 L:62.8634 Do you want to: 1) Increase Red, 2) Decrease Red 3) Increase Green, 4) Decrease Green 5) Increase Blue, 6) Decrease Blue 7) Print the inverse or 8) Quit 7 Inverse is R:0 G:253 B:155 R:255 G:2 B:100 L:62.8634 Do you want to: 1) Increase Red, 2) Decrease Red


3) Increase Green, 4) Decrease Green 5) Increase Blue, 6) Decrease Blue 7) Print the inverse or 8) Quit 8 R:255 G:2 B:100 L:62.8634
 
 

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include
using namespace std;

class color{
public:
int r,g,b;
color(int r=254,int b=2,int g=100)
{
this->r = r;
this->g = g;
this->b = b;
}
void increaseRed()
{
if(this->r < 255)
this->r++;
}
void increaseGreen()
{
if(this->g < 255)
this->g++;
}
void increaseBlue()
{
if(this->b < 255)
this->b++;
}
void decreaseRed()
{
if(this->r > 0)
this->r--;
}
void decreaseGreen()
{
if(this->g > 0)
this->g--;
}
void decreaseBlue()
{
if(this->b>0)
this->b--;
}

string toString()
{
double luminance = (0.2126*this->r + 0.7152*this->g + 0.0722*this->b);

ostringstream str;
str << "RED : " << this->r <<" ; Green : " << this->g <<" ; Blue : " << this->b <<" ; Luminanace : " << luminance ;

return str.str();

}

void Inverse()
{
ostringstream str;
str << "RED : " << 255 - this->r <<" ; Green : " << 255 - this->g <<" ; Blue : " << 255 - this->b ;

cout< }
};

int main()
{
color clr;
cout <

int n=1;

while(n!=0)
{
cout<<"Enter Your choice\n1. Show Color \n2. Increase intensity \n3. Decrease intensity \n4. Inverse Display\n0. Exit\n";
cin>>n;
switch(n)
{
case 1: cout< case 2: { cout<<"Which Color \n1.Red \n2.Green\n3.Blue\n";
int c; cin>>c;
switch(c){
case 1: clr.increaseRed(); break;
case 2: clr.increaseGreen(); break;
case 3: clr.increaseBlue(); break;
default : cout<<"Please enter either 1 or 2 or 3"< }
break;
}
case 3: { cout<<"Which Color \n1.Red \n2.Green\n3.Blue\n";
int c; cin>>c;
switch(c){
case 1: clr.decreaseRed(); break;
case 2: clr.decreaseGreen(); break;
case 3: clr.decreaseBlue(); break;
default : cout<<"Please enter either 1 or 2 or 3"< }
break;
}
case 4: clr.Inverse(); break;
case 0: n=0; break;
default : cout<<"Please enter any number from 0 to 5"< }
}
clr.Inverse();
return 0;
}

//let me know if you find any difficulty.

Add a comment
Know the answer?
Add Answer to:
C ++ Exercises (TV)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C++!! Program 2: BACK AWAY FROM THE TV! If you sat close enough to an...

    IN C++!! Program 2: BACK AWAY FROM THE TV! If you sat close enough to an old TV, you could see individual (R)ed, (G)reen and (B)lue (or RGB) “phosphors” that could represent just about any color you could imagine (using “additive color model”). When these three color components are at their maximum values, the resulting color is white from a distance (which is amazing – look at your screen with a magnifying glass!). When all are off, the color results...

  • In computer systems color is represented by RGB format (Red. Green and Blue color components) where...

    In computer systems color is represented by RGB format (Red. Green and Blue color components) where each component can hold a value from 0 to 255. Implement all functions and operators given in the following header file: # include using namespace std; class public://Default constructor//Constructor with parameters//Copy constructor//Getters//Setters//a function that increment all components//a print function to print all components//a function to add two private: int red; int green; int blue; Test your class with an appropriate main ()

  • 7.5 The R, G, and B component images of an RGB image have the horizontal intensity profiles shown...

    7.5 The R, G, and B component images of an RGB image have the horizontal intensity profiles shown in the following diagram. What color would a person see in the middle column of this image? 1.0 Red Green 0.5 N/2 Position N/2 Position Blue · 0.5 N-1 N/2 Position 0 7.5 The R, G, and B component images of an RGB image have the horizontal intensity profiles shown in the following diagram. What color would a person see in the...

  • We’re sure you've seen old black-and-white (actually, grayscale) photos that, over time, have gained a yellowish...

    We’re sure you've seen old black-and-white (actually, grayscale) photos that, over time, have gained a yellowish tint. We can mimic this effect by creating sepia-tinted images. There are several different ways to sepia-tint an image. In one of the simplest techniques, the image is first converted to grayscale, and then each pixel's red and blue components are adjusted, leaving the green component unchanged. Here's the algorithm: · First, we convert the image to grayscale, because old photographic prints were grayscale....

  • Please write a code in python! Define and test a function named posterize. This function expects...

    Please write a code in python! Define and test a function named posterize. This function expects an image and a tuple of RGB values as arguments. The function modifies the image like the blackAndWhite function, but it uses the given RGB values instead of black. images.py import tkinter import os, os.path tk = tkinter _root = None class ImageView(tk.Canvas): def __init__(self, image, title = "New Image", autoflush=False): master = tk.Toplevel(_root) master.protocol("WM_DELETE_WINDOW", self.close) tk.Canvas.__init__(self, master, width = image.getWidth(), height = image.getHeight())...

  • Task In this challenge you parse RGB colors represented by strings. The formats are primarily used...

    Task In this challenge you parse RGB colors represented by strings. The formats are primarily used in HTML and CSS. Your task is to implement a function which takes a color as a string and returns the parsed color as a map (see Examples). Input represents one of the following: color The input string 1.6-digit hexadecimal "#RRGGBB" - Each pair of digits represents a value of the channel in hexadecimal: 00 to FF 2.3-digit hexadecimal "#RGB" - Each digit represents...

  • lab3RGB.c file: #include <stdio.h> #define AlphaValue 100 int main() { int r, g,b; unsigned int rgb_pack...

    lab3RGB.c file: #include <stdio.h> #define AlphaValue 100 int main() { int r, g,b; unsigned int rgb_pack; int r_unpack, g_unpack,b_unpack; int alpha = AlphaValue; printf("enter R value (0~255): "); scanf("%d",&r); printf("enter G value (0~255): "); scanf("%d",&g); printf("enter B value (0~255): "); scanf("%d",&b); while(! (r<0 || g<0 || b <0) ) { printf("A: %d\t", alpha); printBinary(alpha); printf("\n"); printf("R: %d\t", r); printBinary(r); printf("\n"); printf("G: %d\t", g); printBinary(g); printf("\n"); printf("B: %d\t", b); printBinary(b); printf("\n"); /* do the packing */ //printf("\nPacked: value %d\t", rgb_pack); printBinary(rgb_pack);printf("\n");...

  • WRITE A C++ PROGRAM TO ANSWER THE QUESTIONS BELOW. You are provided with an image in...

    WRITE A C++ PROGRAM TO ANSWER THE QUESTIONS BELOW. You are provided with an image in a new file format called JBC Here is an example of the format: 2 3 100 50 40 200 66 56 12 45 65 23 67 210 0 255 100 45 32 123 The first line means that this is a 2 (width) by 3 (height) image of pixels. Then, each line represents the color of a pixel. The numbers are from 0 to...

  • 4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels contai...

    Please design the function in version 3 of python 4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list that represents the portion of the original image that is specified by the other four parameters. The extracted portion of the image should consist of the pixels that fall in the intersection of the rows of pixels that begin with row rmin...

  • 16. What do the following two code fragments do given integers a and b? a-a +...

    16. What do the following two code fragments do given integers a and b? a-a + b b-a b just print the code you wrote with comments RGBA color format. Some of Java's classes (Bufferedlmage, PixelGrabber) use a special encoding called RGBA to store the color of each pixel. The format consists of tour integers, representing the red, green, and blue intensities from 0 (not present) to 255 (fully used), and also the alpha transparency value from O (transparent) to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT