6 Replies - 3081 Views - Last Post: 29 October 2005 - 04:11 AM Rate Topic: -----

#1 The_Lone_Wolf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 14-February 05

Color Class Help

Posted 27 October 2005 - 07:57 AM

This is the scenario and what points i gotta do and below is what i have attempted to do so far:

A colour can be modelled as an RGB triplet with three integer values, each in the range 0 to 255 which correspond to the specified red, green, and blue components in that colour. The higher the value of one of these values, the brighter that component is. The colour white corresponds to the RGB triplet (255, 255, 255) while black is specified by (0, 0, 0). Other principal colours and their corresponding RGB values are: red (255, 0, 0 ) ; blue ( 0, 0, 255 ) ; cyan (0, 255, 255) ; green (0, 255, 0) ; grey (128, 128, 128); magenta (255, 0, 255) and yellow (255, 255, 0). Any attempt to create a colour using a value outside the range is an error.

1) Define appropriate instance variables for the class Colour
2) Define a set of appropriate constructors for this class
3) Provide "constant" values for each of the nine principal colours listed above that will enable any application that needs to use these colours, say "yellow" to write Colour.yellow etc
4) Define appropriate accessor and setter methods for this class
5) Provide a boolean-valued operation that checks whether two Colour objects have the same value
6) Override the inherited String toString() method to provide an appropriate String representation of a Colour object
7) Provide a method mix() which takes two colours and "mixes" them to produce a new colour whose RGB values are obtained by averaging the individual colour components, for example mixing (20, 30, 40) with (180, 60, 95) produces the colour (100, 45, 67)
8) Provide a method brighter() which creates a new Colour that is a brighter version of this Colour - this should be achieved by increasing each of the current colour's RGB values by 42% up to a maximum of 255
9)Provide a method darker() which creates a new Colour that is a darker version of this Colour - this should be achieved by decreasing each of the current colour's RGB values by 30%.

I have covered points 1 to 5 (hopefully) but need help for 6 to9.



class Colour
{
    private static final int MAX = 255;
    private static final int MIN = 0;
  
    private int red;
    private int green;
    private int blue;

    public Colour()
    {
	red = 125; 
	green = 55;
	blue = 66;
    }
   public Colour( int r1, int g2, int b3 )
   {
      red = r1;
      green = g2;
      blue = b3;
   }

   public Colour( int c )
   {
      red = c;
      green = c;
      blue = c;
   }


   public int getRed()
   {
      return red;
   }


 public int getGreen()
   {
      return green;
   }

 public int getBlue()
   {
      return blue;
   }

    public Colour(int r1, int g2, int b3)
    {

    this.r1 = r1;
    this.g2 = g2;
    this.b3 = b3;
    }

    public boolean equals(Colour n)
    {

return ( (this.r1 == c.n1) && (this.g2 == c.g2) && (this.b3 == c.b3) ) ||       ( (this.r1 == c.r1) && (this.g2 == c.g2) && (this.b3 == c.b3));
    }

    public String toString()
    {

	return "Red
    }


  



can you tell me if so far this is correct and on the write track?

Is This A Good Question/Topic? 0
  • +

Replies To: Color Class Help

#2 The_Lone_Wolf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 14-February 05

Re: Color Class Help

Posted 28 October 2005 - 03:12 AM

Well can anyone help?
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Color Class Help

Posted 28 October 2005 - 05:57 AM

Sure we can...have you attempted the other methods? If so, what was the problem you encountered?

Lighter and Darker for example, merely adjust the values in the red, green, and blue variables....

Something like:
public Lighter()
   {

   this.red = this.red - (this.red*030);
   this.green = this.green - (this.green *030);
   this.blue = this.blue - (this.blue *030);
   }


Of course, the darker function will add the values.

the mix method will take two instances of color, and combine them by adding the like components and taking the average.
Was This Post Helpful? 0
  • +
  • -

#4 The_Lone_Wolf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 14-February 05

Re: Color Class Help

Posted 28 October 2005 - 07:52 AM

Also the code that i posted covers points 1 to 5 that i tried to do. Could you check that coding and tell me whether its right so far. Thanks for the light and darker one and also how do i do that string method. which is point 6?

This post has been edited by The_Lone_Wolf: 28 October 2005 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Color Class Help

Posted 28 October 2005 - 09:40 AM

Well, unfortunately, I'm not near a compiler right now...to see what is right and wrong, simply compile and run the program...otherwise I may have time to run it tonight...
Was This Post Helpful? 0
  • +
  • -

#6 The_Lone_Wolf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 14-February 05

Re: Color Class Help

Posted 28 October 2005 - 10:35 AM

I wont be able to compile it until monday. There is a server problem with the uni's comps and none of the tools i have will work cos they need to access the unis server . Also i forgot to add this. This is the test program. Hopefully this is correct too.


class TestColour
{
    public static void main (String[] args)
    {
   	 
   	 
    
	Colour c = new Colour();
	int red = c.getRed();
	int green = c.getGreen();
	int blue  = c.getBlue();

	System.out.println(red);
  System.out.println(green);
	System.out.println(blue);
   
   
   
   /*
    Colour c  = new Colour(20, 4, 50);
	Colour c1 = new Colour();
	Colour c2 = new Colour(201, 140, 30);
	
	Colour Paint = c1.MIX(c2);
	Colour Paint = new Colour ();

    System.out.println("Red:   " + Paint.getRed() );
    
    System.out.println(Paint.toString() );
   */
    }
    
}



Was This Post Helpful? 0
  • +
  • -

#7 The_Lone_Wolf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 14-February 05

Re: Color Class Help

Posted 29 October 2005 - 04:11 AM

The first code i provided does it cover points 1 to 5 of what is asks or is there anything missing. Once i know i can get to work on the rest of the points!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1