Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * RGBColor class defines object instances and method to be used by RGBColor objects.
- *
- * @author (Avi Ezerzer)
- * @version (3.25 05/12/14)
- */
- public class RGBColor
- {
- private int _red;
- private int _green;
- private int _blue;
- /**
- * get method for RGB
- * retrieve the red component value of the RGB color set.
- */
- public int getRed()
- {
- return _red;
- }
- /**
- * get method for RGB
- * retrieve the green component value of the RGB color set.
- */
- public int getGreen()
- {
- return _green;
- }
- /**
- * get method for RGB
- * retrieve the blue component value of the RGB color set.
- */
- public int getBlue()
- {
- return _blue;
- }
- /**
- * set method for RGB
- * override the red color of an RGB color set only if intensity range is valid.
- * Applies only if intensity range is valid.
- */
- public void setRed(int num)
- {
- if (num >= 0 && num <= 255)
- _red = num;
- }
- /**
- * set method for RGB
- * override the green color of an RGB color set only if intensity range is valid.
- * Applies only if intensity range is valid.
- */
- public void setGreen(int num)
- {
- if (num >= 0 && num <= 255)
- _green = num;
- }
- /**
- * set method for RGB
- * override the blue color of an RGB color set.
- * Applies only if intensity range is valid.
- */
- public void setBlue(int num)
- {
- if (num >= 0 && num <= 255)
- _blue = num;
- }
- /**
- * Constructs and RGBColor instance using the red,green,blue parameters.
- * Assuming intensity of colors is within legal range.
- * Otherwise,a black RGBColor(0,0,0) is created using the public RGBColor() calling the init()
- * method.
- */
- public RGBColor(int red,int green,int blue)
- {
- if (_red >= 0 && _red <= 255 && _green >= 0 && _green <= 255 && _blue >= 0 && _blue <= 255)
- {
- _red = red;
- _green = green;
- _blue = blue;
- }
- else
- {
- init();
- }
- }
- /**
- * Method set to create a default black instance.(0,0,0)
- * intermediate means for matching exercise requirments.
- */
- public void init()
- {
- _red = 0;
- _green = 0;
- _blue = 0;
- }
- /**
- * constructs a black instance(0,0,0)
- * of RGBColor as default using the init() method.
- */
- public RGBColor()
- {
- init();
- }
- /**
- * construct a copy of an RGBColor instance.
- */
- public RGBColor (RGBColor other)
- {
- this(other.getRed(),other.getGreen(),other.getBlue());
- }
- /**
- * generate a string instance
- * using the color components.
- */
- public String toString()
- {
- return "("+_red+","+_green+","+_blue+")";
- }
- /**
- * checks wether the RGBColor colors given match the colors of RGBColor other
- */
- public boolean equals(RGBColor other)
- {
- return (other.getRed()==_red && other.getGreen() == _green && other.getBlue() == _blue);
- }
- /**
- * create a color mixture which is the division of the sum of RGBColor and other.
- */
- public void mix(RGBColor other)
- {
- _red = other.getRed()*_red/2;
- _green = other.getGreen()*_green/2;
- _blue = other.getBlue()*_blue/2;
- }
- /**
- * Returns the grayscale value of this RGBColor.
- * The grayscale value is defined as 30% the red value + 59% of the green value and 11% of the blue value.
- */
- public double convertToGrayscale()
- {
- return _red*0.3+_green*0.59+_blue*0.11 ;
- }
- /**
- * Inverts the color of this RGBColor replacing each component value with its counterpart relative to 255.
- * example:RGB values of [0,1,2] becomes [255,254,253].
- */
- public void invert()
- {
- _red = 255-_red;
- _green = 255-_green;
- _blue = 255-_blue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement