Advertisement
AviEzerzer

rgbcolor

Dec 5th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. /**
  2.  * RGBColor class defines object instances and method to be used by RGBColor objects.
  3.  *
  4.  * @author (Avi Ezerzer)
  5.  * @version (3.25 05/12/14)
  6.  */
  7. public class RGBColor
  8. {
  9.     private int _red;
  10.     private int _green;
  11.     private int _blue;
  12.     /**
  13.      * get method for RGB
  14.      *  retrieve the red component value of the RGB color set.
  15.      */
  16.     public int getRed()
  17.     {
  18.        return _red;
  19.     }
  20.     /**
  21.      *  get method for RGB
  22.      *  retrieve the green component value of the RGB color set.
  23.      */
  24.     public int getGreen()
  25.     {
  26.        return _green;
  27.     }
  28.      /**
  29.      *  get method for RGB
  30.      *  retrieve the blue component value of the RGB color set.
  31.      */
  32.     public int getBlue()
  33.     {
  34.         return _blue;
  35.     }
  36.     /**
  37.      *  set method for RGB
  38.      *  override the red color of an RGB color set only if intensity range is valid.
  39.      *  Applies only if intensity range is valid.
  40.      */
  41.     public void setRed(int num)
  42.     {
  43.         if (num >= 0 && num <= 255)
  44.             _red = num;
  45.     }
  46.     /**
  47.      *  set method for RGB
  48.      *  override the green color of an RGB color set only if intensity range is valid.
  49.      *  Applies only if intensity range is valid.
  50.      */
  51.     public void setGreen(int num)
  52.     {
  53.         if (num >= 0 && num <= 255)
  54.             _green = num;
  55.     }
  56.     /**
  57.      *  set method for RGB
  58.      *  override the blue color of an RGB color set.
  59.      *  Applies only if intensity range is valid.
  60.      */
  61.     public void setBlue(int num)
  62.     {
  63.         if (num >= 0 && num <= 255)
  64.             _blue = num;
  65.     }
  66.     /**
  67.      *  Constructs and RGBColor instance using the red,green,blue parameters.
  68.      *  Assuming intensity of colors is within legal range.
  69.      *  Otherwise,a black RGBColor(0,0,0) is created using the public RGBColor() calling the init()
  70.      *  method.
  71.      */
  72.     public RGBColor(int red,int green,int blue)
  73.     {
  74.        if (_red >= 0 && _red <= 255 && _green >= 0 && _green <= 255 && _blue >= 0 && _blue <= 255)
  75.        {
  76.         _red = red;
  77.         _green = green;
  78.         _blue = blue;
  79.        }
  80.         else
  81.        {
  82.         init();
  83.        }
  84.     }
  85.      /**
  86.       * Method set to create a default black instance.(0,0,0)
  87.       * intermediate means for matching exercise requirments.
  88.      */
  89.     public void init()
  90.     {
  91.         _red = 0;
  92.         _green = 0;
  93.         _blue = 0;
  94.     }
  95.     /**
  96.      * constructs a black instance(0,0,0)
  97.      * of RGBColor as default using the init() method.
  98.      */
  99.     public RGBColor()
  100.     {
  101.         init();
  102.     }
  103.     /**
  104.      * construct a copy of an RGBColor instance.
  105.     */
  106.     public RGBColor (RGBColor other)
  107.     {
  108.         this(other.getRed(),other.getGreen(),other.getBlue());
  109.     }
  110.     /**
  111.      * generate a string instance
  112.      * using the color components.
  113.      */
  114.     public String toString()
  115.     {
  116.         return "("+_red+","+_green+","+_blue+")";
  117.     }
  118.     /**
  119.      * checks wether the RGBColor colors given match the colors of RGBColor other
  120.      */
  121.     public boolean equals(RGBColor other)
  122.     {
  123.         return (other.getRed()==_red && other.getGreen() == _green && other.getBlue() == _blue);
  124.     }
  125.     /**
  126.      * create a color mixture which is the division of the sum of RGBColor and other.
  127.      */
  128.     public void mix(RGBColor other)
  129.     {
  130.         _red   = other.getRed()*_red/2;
  131.         _green = other.getGreen()*_green/2;
  132.         _blue  = other.getBlue()*_blue/2;
  133.     }
  134.     /**
  135.      * Returns the grayscale value of this RGBColor.
  136.      * The grayscale value is defined as 30% the red value + 59% of the green value and 11% of the blue value.
  137.      */
  138.     public double convertToGrayscale()
  139.     {
  140.         return _red*0.3+_green*0.59+_blue*0.11 ;
  141.     }
  142.     /**
  143.      * Inverts the color of this RGBColor replacing each component value with its counterpart relative to 255.
  144.      * example:RGB values of [0,1,2] becomes [255,254,253].
  145.      */
  146.     public void invert()
  147.     {
  148.         _red   = 255-_red;
  149.         _green = 255-_green;
  150.         _blue  = 255-_blue;
  151.     }
  152.    
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement