Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Lars Gajewski @zennxd /u/Zennxd
- * Custom Color class.
- */
- public class ColorRGB {
- public int r,g,b;
- //constructors
- /**
- * Creates a new Color class with the default color RED(255, 0, 0).
- */
- public ColorRGB(){
- r = 255;
- g = 0;
- b = 0;
- }
- /**
- * Creates a new Color class with custom color values.<br>
- * On creation, it checks if passed parameters are within the valid bounds.
- * @param r Amount of red in the color.
- * @param g Amount of green in the color.
- * @param b Amount of red in the color.
- * @exception IllegalArgumentException one or more of the values is less than 0 or more than 255.
- */
- public ColorRGB(int r,int g,int b){
- this.r = r;
- this.g = g;
- this.b = b;
- checkValidity();
- }
- /**
- * Creates a new Color class with a predefined color.
- * @prefabColor The color that the color values will create.
- * @see pc
- */
- public ColorRGB(pc prefabColor){
- this.r = prefabColor.r;
- this.g = prefabColor.g;
- this.b = prefabColor.b;
- checkValidity();
- }
- //methods
- /**
- * Changes the color values of the class.
- * @param r Amount of red in the color.
- * @param g Amount of green in the color.
- * @param b Amount of blue in the color.
- */
- public void changeColor(int r, int g, int b){
- this.r = r;
- this.g = g;
- this.b = b;
- checkValidity();
- }
- /**
- * Checks if the color values are within valid bounds.
- * @exception IllegalArgumentException They're not.
- * @see changeColor
- * @see Constructors
- */
- public void checkValidity(){
- int[] colorValues = new int[]{r,g,b};
- char[] colorValueNames = new char[]{'r','g','b'};
- for(int i = 0; i < 3; i++){
- if(colorValues[i] < 0 || colorValues[i] > 255){
- IllegalArgumentException e = new IllegalArgumentException
- ("Color not valid. Color value " + colorValueNames[i] + " must be between 0 and 255."
- + "current value:" + colorValues[i] + ".");
- throw e;
- }
- }
- }
- public void PrintColorValues(){
- System.out.println("Color Values of color " + this + ":");
- System.out.println(" R: " + r);
- System.out.println(" G: " + g);
- System.out.println(" B: " + b);
- }
- //Custom Color enum: prefabColor
- public enum pc{
- RED (255, 0 , 0 ),
- GREEN (0 , 255, 0 ),
- BLUE (0 , 0 , 255),
- WHITE (255, 255, 255),
- BLACK (0 , 0 , 0 ),
- YELLOW (255, 255, 0 ),
- PURPLE (255, 0 , 255),
- ORANGE (255, 140, 0 ),
- CYAN (0 , 255, 255);
- private final int r;
- private final int g;
- private final int b;
- pc(int r, int g, int b){
- this.r = r;
- this.g = g;
- this.b = b;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement