Advertisement
Zenn_

Java ColorRGB class

Nov 29th, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. /**
  2.  * @author Lars Gajewski @zennxd /u/Zennxd
  3.  * Custom Color class.
  4.  */
  5.  
  6. public class ColorRGB {
  7.     public int r,g,b;
  8.    
  9.     //constructors
  10.     /**
  11.      * Creates a new Color class with the default color RED(255,  0,  0).
  12.      */
  13.     public ColorRGB(){
  14.         r = 255;
  15.         g = 0;
  16.         b = 0;
  17.     }
  18.     /**
  19.      * Creates a new Color class with custom color values.<br>
  20.      * On creation, it checks if passed parameters are within the valid bounds.
  21.      * @param r Amount of red in the color.
  22.      * @param g Amount of green in the color.
  23.      * @param b Amount of red in the color.
  24.      * @exception IllegalArgumentException one or more of the values is less than 0 or more than 255.
  25.      */
  26.     public ColorRGB(int r,int g,int b){
  27.         this.r = r;
  28.         this.g = g;
  29.         this.b = b;
  30.         checkValidity();
  31.     }
  32.     /**
  33.      * Creates a new Color class with a predefined color.
  34.      * @prefabColor The color that the color values will create.
  35.      * @see pc
  36.      */
  37.     public ColorRGB(pc prefabColor){
  38.         this.r = prefabColor.r;
  39.         this.g = prefabColor.g;
  40.         this.b = prefabColor.b;
  41.         checkValidity();
  42.     }
  43.    
  44.     //methods
  45.     /**
  46.      * Changes the color values of the class.
  47.      * @param r Amount of red in the color.
  48.      * @param g Amount of green in the color.
  49.      * @param b Amount of blue in the color.
  50.      */
  51.     public void changeColor(int r, int g, int b){
  52.         this.r = r;
  53.         this.g = g;
  54.         this.b = b;
  55.         checkValidity();
  56.     }
  57.     /**
  58.      * Checks if the color values are within valid bounds.
  59.      * @exception IllegalArgumentException They're not.
  60.      * @see changeColor
  61.      * @see Constructors
  62.      */
  63.     public void checkValidity(){
  64.         int[] colorValues = new int[]{r,g,b};
  65.         char[] colorValueNames = new char[]{'r','g','b'};
  66.        
  67.         for(int i = 0; i < 3; i++){
  68.             if(colorValues[i] < 0 || colorValues[i] > 255){
  69.               IllegalArgumentException e = new IllegalArgumentException
  70.               ("Color not valid. Color value " + colorValueNames[i] + " must be between 0 and 255."
  71.                       + "current value:" + colorValues[i] + ".");
  72.               throw e;
  73.             }
  74.         }
  75.     }
  76.     public void PrintColorValues(){
  77.         System.out.println("Color Values of color " + this + ":");
  78.         System.out.println("    R: " + r);
  79.         System.out.println("    G: " + g);
  80.         System.out.println("    B: " + b);
  81.     }
  82.    
  83.     //Custom Color enum: prefabColor
  84.     public enum pc{
  85.         RED     (255, 0  , 0  ),
  86.         GREEN   (0  , 255, 0  ),
  87.         BLUE    (0  , 0  , 255),
  88.         WHITE   (255, 255, 255),
  89.         BLACK   (0  , 0  , 0  ),
  90.         YELLOW  (255, 255, 0  ),
  91.         PURPLE  (255, 0  , 255),
  92.         ORANGE  (255, 140, 0  ),      
  93.         CYAN    (0  , 255, 255);
  94.        
  95.         private final int r;
  96.         private final int g;
  97.         private final int b;
  98.        
  99.         pc(int r, int g, int b){
  100.             this.r = r;
  101.             this.g = g;
  102.             this.b = b;
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement